Skip to content

Commit a8e2bf7

Browse files
committed
New options for configuring SSLSession cache, close AsyncHttpClient#837
sslSessionCacheSize + sslSessionTimeout
1 parent 719fdb7 commit a8e2bf7

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public class AsyncHttpClientConfig {
7878
protected int ioThreadMultiplier;
7979
protected String[] enabledProtocols;
8080
protected String[] enabledCipherSuites;
81+
protected Integer sslSessionCacheSize;
82+
protected Integer sslSessionTimeout;
8183
protected AsyncHttpProviderConfig<?, ?> providerConfig;
8284

8385
protected AsyncHttpClientConfig() {
@@ -113,6 +115,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
113115
int ioThreadMultiplier, //
114116
String[] enabledProtocols,//
115117
String[] enabledCipherSuites,//
118+
Integer sslSessionCacheSize,//
119+
Integer sslSessionTimeout,//
116120
AsyncHttpProviderConfig<?, ?> providerConfig) {
117121

118122
this.connectTimeout = connectTimeout;
@@ -145,6 +149,8 @@ private AsyncHttpClientConfig(int connectTimeout,//
145149
this.ioThreadMultiplier = ioThreadMultiplier;
146150
this.enabledProtocols = enabledProtocols;
147151
this.enabledCipherSuites = enabledCipherSuites;
152+
this.sslSessionCacheSize = sslSessionCacheSize;
153+
this.sslSessionTimeout = sslSessionCacheSize;
148154
this.providerConfig = providerConfig;
149155
}
150156

@@ -452,6 +458,20 @@ public String[] getEnabledCipherSuites() {
452458
return enabledCipherSuites;
453459
}
454460

461+
/**
462+
* since 1.9.13
463+
*/
464+
public Integer getSslSessionCacheSize() {
465+
return sslSessionCacheSize;
466+
}
467+
468+
/**
469+
* since 1.9.13
470+
*/
471+
public Integer getSslSessionTimeout() {
472+
return sslSessionTimeout;
473+
}
474+
455475
/**
456476
* Builder for an {@link AsyncHttpClient}
457477
*/
@@ -488,6 +508,8 @@ public static class Builder {
488508
private int ioThreadMultiplier = defaultIoThreadMultiplier();
489509
private String[] enabledProtocols;
490510
private String[] enabledCipherSuites;
511+
private Integer sslSessionCacheSize = defaultSslSessionCacheSize();
512+
private Integer sslSessionTimeout = defaultSslSessionTimeout();
491513
private AsyncHttpProviderConfig<?, ?> providerConfig;
492514

493515
public Builder() {
@@ -900,6 +922,16 @@ public Builder setEnabledCipherSuites(String[] enabledCipherSuites) {
900922
return this;
901923
}
902924

925+
public Builder setSslSessionCacheSize(Integer sslSessionCacheSize) {
926+
this.sslSessionCacheSize = sslSessionCacheSize;
927+
return this;
928+
}
929+
930+
public Builder setSslSessionTimeout(Integer sslSessionTimeout) {
931+
this.sslSessionTimeout = sslSessionTimeout;
932+
return this;
933+
}
934+
903935
/**
904936
* Create a config builder with values taken from the given prototype configuration.
905937
*
@@ -940,6 +972,8 @@ public Builder(AsyncHttpClientConfig prototype) {
940972
strict302Handling = prototype.isStrict302Handling();
941973
enabledProtocols = prototype.enabledProtocols;
942974
enabledCipherSuites = prototype.enabledCipherSuites;
975+
sslSessionCacheSize = prototype.sslSessionCacheSize;
976+
sslSessionTimeout = prototype.sslSessionTimeout;
943977
acceptAnyCertificate = prototype.acceptAnyCertificate;
944978
}
945979

@@ -1001,6 +1035,8 @@ public Thread newThread(Runnable r) {
10011035
ioThreadMultiplier, //
10021036
enabledProtocols, //
10031037
enabledCipherSuites, //
1038+
sslSessionCacheSize, //
1039+
sslSessionTimeout, //
10041040
providerConfig);
10051041
}
10061042
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,12 @@ public static boolean defaultDisableUrlEncodingForBoundRequests() {
108108
public static boolean defaultAcceptAnyCertificate() {
109109
return getBoolean(ASYNC_CLIENT + "acceptAnyCertificate", false);
110110
}
111+
112+
public static Integer defaultSslSessionCacheSize() {
113+
return Integer.getInteger(ASYNC_CLIENT + "sslSessionCacheSize");
114+
}
115+
116+
public static Integer defaultSslSessionTimeout() {
117+
return Integer.getInteger(ASYNC_CLIENT + "sslSessionTimeout");
118+
}
111119
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ public DefaultSSLEngineFactory(AsyncHttpClientConfig config) {
4444

4545
@Override
4646
public SSLEngine newSSLEngine(String peerHost, int peerPort) throws GeneralSecurityException {
47-
SSLContext sslContext = config.getSSLContext();
48-
49-
if (sslContext == null)
50-
sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate());
51-
47+
SSLContext sslContext = SslUtils.getInstance().getSSLContext(config);
48+
5249
SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort);
5350
if (!config.isAcceptAnyCertificate()) {
5451
SSLParameters params = sslEngine.getSSLParameters();

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import java.net.UnknownHostException;
6969
import java.nio.ByteBuffer;
7070
import java.security.GeneralSecurityException;
71-
import java.security.NoSuchAlgorithmException;
7271
import java.util.Map;
7372
import java.util.concurrent.Callable;
7473
import java.util.concurrent.TimeoutException;
@@ -183,15 +182,11 @@ private HttpURLConnection createUrlConnection(Request request) throws IOExceptio
183182

184183
if (request.getUri().getScheme().equals("https")) {
185184
HttpsURLConnection secure = (HttpsURLConnection) urlConnection;
186-
SSLContext sslContext = config.getSSLContext();
187-
if (sslContext == null) {
188-
try {
189-
sslContext = SslUtils.getInstance().getSSLContext(config.isAcceptAnyCertificate());
190-
} catch (NoSuchAlgorithmException e) {
191-
throw new IOException(e.getMessage());
192-
} catch (GeneralSecurityException e) {
193-
throw new IOException(e.getMessage());
194-
}
185+
SSLContext sslContext;
186+
try {
187+
sslContext = SslUtils.getInstance().getSSLContext(config);
188+
} catch (GeneralSecurityException e) {
189+
throw new IOException(e.getMessage());
195190
}
196191
secure.setSSLSocketFactory(sslContext.getSocketFactory());
197192
secure.setHostnameVerifier(config.getHostnameVerifier());

src/main/java/com/ning/http/util/SslUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.ning.http.util;
1717

18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
1820
import javax.net.ssl.SSLContext;
1921
import javax.net.ssl.TrustManager;
2022
import javax.net.ssl.X509TrustManager;
@@ -61,7 +63,16 @@ public static SslUtils getInstance() {
6163
return SingletonHolder.instance;
6264
}
6365

64-
public SSLContext getSSLContext(boolean acceptAnyCertificate) throws GeneralSecurityException {
65-
return acceptAnyCertificate ? looseTrustManagerSSLContext : SSLContext.getDefault();
66+
public SSLContext getSSLContext(AsyncHttpClientConfig config) throws GeneralSecurityException {
67+
SSLContext sslContext = config.getSSLContext();
68+
69+
if (sslContext != null) {
70+
sslContext = config.isAcceptAnyCertificate() ? looseTrustManagerSSLContext : SSLContext.getDefault();
71+
if (config.getSslSessionCacheSize() != null)
72+
sslContext.getClientSessionContext().setSessionCacheSize(config.getSslSessionCacheSize());
73+
if (config.getSslSessionTimeout() != null)
74+
sslContext.getClientSessionContext().setSessionTimeout(config.getSslSessionTimeout());
75+
}
76+
return sslContext;
6677
}
6778
}

0 commit comments

Comments
 (0)