Skip to content

Commit dfacb8e

Browse files
author
Stephane Landelle
committed
Introduce acceptAnyCertificate config, defaulting to false, backport df6ed70, close #352
1 parent a894583 commit dfacb8e

14 files changed

+146
-277
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 52 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525

2626
import javax.net.ssl.HostnameVerifier;
2727
import javax.net.ssl.SSLContext;
28-
import javax.net.ssl.SSLEngine;
2928

30-
import java.security.GeneralSecurityException;
3129
import java.util.Collections;
3230
import java.util.LinkedList;
3331
import java.util.List;
@@ -68,7 +66,6 @@ public class AsyncHttpClientConfig {
6866
protected ExecutorService applicationThreadPool;
6967
protected ProxyServerSelector proxyServerSelector;
7068
protected SSLContext sslContext;
71-
protected SSLEngineFactory sslEngineFactory;
7269
protected AsyncHttpProviderConfig<?, ?> providerConfig;
7370
protected ConnectionsPool<?, ?> connectionsPool;
7471
protected Realm realm;
@@ -86,6 +83,7 @@ public class AsyncHttpClientConfig {
8683
protected boolean useRelativeURIsWithSSLProxies;
8784
protected int maxConnectionLifeTimeInMs;
8885
protected TimeConverter timeConverter;
86+
protected boolean acceptAnyCertificate;
8987

9088
protected AsyncHttpClientConfig() {
9189
}
@@ -106,7 +104,6 @@ private AsyncHttpClientConfig(int maxTotalConnections,
106104
ExecutorService applicationThreadPool,
107105
ProxyServerSelector proxyServerSelector,
108106
SSLContext sslContext,
109-
SSLEngineFactory sslEngineFactory,
110107
AsyncHttpProviderConfig<?, ?> providerConfig,
111108
ConnectionsPool<?, ?> connectionsPool, Realm realm,
112109
List<RequestFilter> requestFilters,
@@ -121,7 +118,8 @@ private AsyncHttpClientConfig(int maxTotalConnections,
121118
int ioThreadMultiplier,
122119
boolean strict302Handling,
123120
boolean useRelativeURIsWithSSLProxies,
124-
TimeConverter timeConverter) {
121+
TimeConverter timeConverter, //
122+
boolean acceptAnyCertificate) {
125123

126124
this.maxTotalConnections = maxTotalConnections;
127125
this.maxConnectionPerHost = maxConnectionPerHost;
@@ -137,7 +135,6 @@ private AsyncHttpClientConfig(int maxTotalConnections,
137135
this.userAgent = userAgent;
138136
this.allowPoolingConnection = keepAlive;
139137
this.sslContext = sslContext;
140-
this.sslEngineFactory = sslEngineFactory;
141138
this.providerConfig = providerConfig;
142139
this.connectionsPool = connectionsPool;
143140
this.realm = realm;
@@ -161,6 +158,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
161158
this.proxyServerSelector = proxyServerSelector;
162159
this.disableUrlEncodingForBoundedRequests = disableUrlEncodingForBoundedRequests;
163160
this.timeConverter = timeConverter;
161+
this.acceptAnyCertificate = acceptAnyCertificate;
164162
}
165163

166164
/**
@@ -310,28 +308,6 @@ public SSLContext getSSLContext() {
310308
return connectionsPool;
311309
}
312310

313-
/**
314-
* Return an instance of {@link SSLEngineFactory} used for SSL connection.
315-
*
316-
* @return an instance of {@link SSLEngineFactory} used for SSL connection.
317-
*/
318-
public SSLEngineFactory getSSLEngineFactory() {
319-
if (sslEngineFactory == null) {
320-
return new SSLEngineFactory() {
321-
public SSLEngine newSSLEngine() {
322-
if (sslContext != null) {
323-
SSLEngine sslEngine = sslContext.createSSLEngine();
324-
sslEngine.setUseClientMode(true);
325-
return sslEngine;
326-
} else {
327-
return null;
328-
}
329-
}
330-
};
331-
}
332-
return sslEngineFactory;
333-
}
334-
335311
/**
336312
* Return the {@link com.ning.http.client.AsyncHttpProviderConfig}
337313
*
@@ -491,12 +467,19 @@ public int getMaxConnectionLifeTimeInMs() {
491467
}
492468

493469
/**
494-
* @return 1.8.2
470+
* since 1.8.2
495471
*/
496472
public TimeConverter getTimeConverter() {
497473
return timeConverter;
498474
}
499475

476+
/**
477+
* since 1.9.0
478+
*/
479+
public boolean isAcceptAnyCertificate() {
480+
return acceptAnyCertificate;
481+
}
482+
500483
/**
501484
* Builder for an {@link AsyncHttpClient}
502485
*/
@@ -525,11 +508,11 @@ public static class Builder {
525508
private boolean removeQueryParamOnRedirect = defaultRemoveQueryParamOnRedirect();
526509
private boolean strict302Handling = defaultStrict302Handling();
527510
private HostnameVerifier hostnameVerifier = defaultHostnameVerifier();
511+
private boolean acceptAnyCertificate = defaultAcceptAnyCertificate();
528512

529513
private ExecutorService applicationThreadPool;
530514
private ProxyServerSelector proxyServerSelector = null;
531515
private SSLContext sslContext;
532-
private SSLEngineFactory sslEngineFactory;
533516
private AsyncHttpProviderConfig<?, ?> providerConfig;
534517
private ConnectionsPool<?, ?> connectionsPool;
535518
private Realm realm;
@@ -713,31 +696,13 @@ public Builder setProxyServer(ProxyServer proxyServer) {
713696
return this;
714697
}
715698

716-
/**
717-
* Set the {@link SSLEngineFactory} for secure connection.
718-
*
719-
* @param sslEngineFactory the {@link SSLEngineFactory} for secure connection
720-
* @return a {@link Builder}
721-
*/
722-
public Builder setSSLEngineFactory(SSLEngineFactory sslEngineFactory) {
723-
this.sslEngineFactory = sslEngineFactory;
724-
return this;
725-
}
726-
727699
/**
728700
* Set the {@link SSLContext} for secure connection.
729701
*
730702
* @param sslContext the {@link SSLContext} for secure connection
731703
* @return a {@link Builder}
732704
*/
733705
public Builder setSSLContext(final SSLContext sslContext) {
734-
this.sslEngineFactory = new SSLEngineFactory() {
735-
public SSLEngine newSSLEngine() throws GeneralSecurityException {
736-
SSLEngine sslEngine = sslContext.createSSLEngine();
737-
sslEngine.setUseClientMode(true);
738-
return sslEngine;
739-
}
740-
};
741706
this.sslContext = sslContext;
742707
return this;
743708
}
@@ -998,6 +963,11 @@ public Builder setTimeConverter(TimeConverter timeConverter) {
998963
return this;
999964
}
1000965

966+
public Builder setAcceptAnyCertificate(boolean acceptAnyCertificate) {
967+
this.acceptAnyCertificate = acceptAnyCertificate;
968+
return this;
969+
}
970+
1001971
/**
1002972
* Create a config builder with values taken from the given prototype configuration.
1003973
*
@@ -1018,7 +988,6 @@ public Builder(AsyncHttpClientConfig prototype) {
1018988
realm = prototype.getRealm();
1019989
requestTimeoutInMs = prototype.getRequestTimeoutInMs();
1020990
sslContext = prototype.getSSLContext();
1021-
sslEngineFactory = prototype.getSSLEngineFactory();
1022991
userAgent = prototype.getUserAgent();
1023992
followRedirect = prototype.isFollowRedirect();
1024993
compressionEnabled = prototype.isCompressionEnabled();
@@ -1041,6 +1010,7 @@ public Builder(AsyncHttpClientConfig prototype) {
10411010
hostnameVerifier = prototype.getHostnameVerifier();
10421011
strict302Handling = prototype.isStrict302Handling();
10431012
timeConverter = prototype.timeConverter;
1013+
acceptAnyCertificate = prototype.acceptAnyCertificate;
10441014
}
10451015

10461016
/**
@@ -1073,40 +1043,39 @@ public Thread newThread(Runnable r) {
10731043
proxyServerSelector = ProxyServerSelector.NO_PROXY_SELECTOR;
10741044
}
10751045

1076-
return new AsyncHttpClientConfig(maxTotalConnections,
1077-
maxConnectionPerHost,
1078-
connectionTimeOutInMs,
1079-
webSocketIdleTimeoutInMs,
1080-
idleConnectionInPoolTimeoutInMs,
1081-
idleConnectionTimeoutInMs,
1082-
requestTimeoutInMs,
1083-
maxConnectionLifeTimeInMs,
1084-
followRedirect,
1085-
maxDefaultRedirects,
1086-
compressionEnabled,
1087-
userAgent,
1088-
allowPoolingConnection,
1089-
applicationThreadPool,
1090-
proxyServerSelector,
1091-
sslContext,
1092-
sslEngineFactory,
1093-
providerConfig,
1094-
connectionsPool,
1095-
realm,
1096-
requestFilters,
1097-
responseFilters,
1098-
ioExceptionFilters,
1099-
requestCompressionLevel,
1100-
maxRequestRetry,
1101-
allowSslConnectionPool,
1102-
disableUrlEncodingForBoundedRequests,
1103-
removeQueryParamOnRedirect,
1104-
hostnameVerifier,
1105-
ioThreadMultiplier,
1106-
strict302Handling,
1107-
useRelativeURIsWithSSLProxies,
1108-
timeConverter);
1046+
return new AsyncHttpClientConfig(maxTotalConnections, //
1047+
maxConnectionPerHost, //
1048+
connectionTimeOutInMs, //
1049+
webSocketIdleTimeoutInMs, //
1050+
idleConnectionInPoolTimeoutInMs, //
1051+
idleConnectionTimeoutInMs, //
1052+
requestTimeoutInMs, //
1053+
maxConnectionLifeTimeInMs, //
1054+
followRedirect, //
1055+
maxDefaultRedirects, //
1056+
compressionEnabled, //
1057+
userAgent, //
1058+
allowPoolingConnection, //
1059+
applicationThreadPool, //
1060+
proxyServerSelector, //
1061+
sslContext, //
1062+
providerConfig, //
1063+
connectionsPool, //
1064+
realm, //
1065+
requestFilters, //
1066+
responseFilters, //
1067+
ioExceptionFilters, //
1068+
requestCompressionLevel, //
1069+
maxRequestRetry, //
1070+
allowSslConnectionPool, //
1071+
disableUrlEncodingForBoundedRequests, //
1072+
removeQueryParamOnRedirect, //
1073+
hostnameVerifier, //
1074+
ioThreadMultiplier, //
1075+
strict302Handling, //
1076+
useRelativeURIsWithSSLProxies, //
1077+
timeConverter, //
1078+
acceptAnyCertificate);
11091079
}
11101080
}
11111081
}
1112-

src/main/java/com/ning/http/client/AsyncHttpClientConfigBean.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void configureDefaults() {
6767
removeQueryParamOnRedirect = defaultRemoveQueryParamOnRedirect();
6868
strict302Handling = defaultStrict302Handling();
6969
hostnameVerifier = defaultHostnameVerifier();
70+
acceptAnyCertificate = defaultAcceptAnyCertificate();
7071

7172
if (defaultUseProxySelector()) {
7273
proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector();
@@ -173,11 +174,6 @@ public AsyncHttpClientConfigBean setSslContext(SSLContext sslContext) {
173174
return this;
174175
}
175176

176-
public AsyncHttpClientConfigBean setSslEngineFactory(SSLEngineFactory sslEngineFactory) {
177-
this.sslEngineFactory = sslEngineFactory;
178-
return this;
179-
}
180-
181177
public AsyncHttpClientConfigBean setProviderConfig(AsyncHttpProviderConfig<?, ?> providerConfig) {
182178
this.providerConfig = providerConfig;
183179
return this;
@@ -242,4 +238,9 @@ public AsyncHttpClientConfigBean setIoThreadMultiplier(int ioThreadMultiplier) {
242238
this.ioThreadMultiplier = ioThreadMultiplier;
243239
return this;
244240
}
241+
242+
public AsyncHttpClientConfigBean setAcceptAnyCertificate(boolean acceptAnyCertificate) {
243+
this.acceptAnyCertificate = acceptAnyCertificate;
244+
return this;
245+
}
245246
}

src/main/java/com/ning/http/client/AsyncHttpClientConfigDefaults.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@ public static boolean defaultRemoveQueryParamOnRedirect() {
121121
public static HostnameVerifier defaultHostnameVerifier() {
122122
return new DefaultHostnameVerifier();
123123
}
124+
125+
public static boolean defaultAcceptAnyCertificate() {
126+
return getBoolean(ASYNC_CLIENT + "acceptAnyCertificate", false);
127+
}
124128
}

src/main/java/com/ning/http/client/SSLEngineFactory.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/com/ning/http/client/SimpleAsyncHttpClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,6 @@ public Builder setExecutorService(ExecutorService applicationThreadPool) {
539539
return this;
540540
}
541541

542-
public Builder setSSLEngineFactory(SSLEngineFactory sslEngineFactory) {
543-
configBuilder.setSSLEngineFactory(sslEngineFactory);
544-
return this;
545-
}
546-
547542
public Builder setSSLContext(final SSLContext sslContext) {
548543
configBuilder.setSSLContext(sslContext);
549544
return this;
@@ -669,6 +664,11 @@ public Builder setProviderClass(String providerClass) {
669664
return this;
670665
}
671666

667+
public Builder setAcceptAnyCertificate(boolean acceptAnyCertificate) {
668+
configBuilder.setAcceptAnyCertificate(acceptAnyCertificate);
669+
return this;
670+
}
671+
672672
public SimpleAsyncHttpClient build() {
673673

674674
if (realmBuilder != null) {

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void onTimeout(Connection connection) {
383383
boolean defaultSecState = (context != null);
384384
if (context == null) {
385385
try {
386-
context = SslUtils.getSSLContext();
386+
context = SslUtils.getInstance().getSSLContext(clientConfig.isAcceptAnyCertificate());
387387
} catch (Exception e) {
388388
throw new IllegalStateException(e);
389389
}

src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private HttpURLConnection createUrlConnection(Request request) throws IOExceptio
186186
SSLContext sslContext = config.getSSLContext();
187187
if (sslContext == null) {
188188
try {
189-
sslContext = SslUtils.getSSLContext();
189+
sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate());
190190
} catch (NoSuchAlgorithmException e) {
191191
throw new IOException(e.getMessage());
192192
} catch (GeneralSecurityException e) {

0 commit comments

Comments
 (0)