Skip to content

Commit eda2bae

Browse files
author
Stephane Landelle
committed
Enable setting Netty SslHandler handshake timeout, close AsyncHttpClient#481
1 parent d264dae commit eda2bae

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.ning.http.util.AsyncHttpProviderUtils.DEFAULT_CHARSET;
3131
import static com.ning.http.util.MiscUtil.isNonEmpty;
3232
import static org.jboss.netty.channel.Channels.pipeline;
33+
import static org.jboss.netty.handler.ssl.SslHandler.getDefaultBufferPool;
3334

3435
import java.io.File;
3536
import java.io.FileInputStream;
@@ -97,6 +98,7 @@
9798
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
9899
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;
99100
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
101+
import org.jboss.netty.handler.ssl.ImmediateExecutor;
100102
import org.jboss.netty.handler.ssl.SslHandler;
101103
import org.jboss.netty.handler.stream.ChunkedFile;
102104
import org.jboss.netty.handler.stream.ChunkedWriteHandler;
@@ -129,7 +131,6 @@
129131
import com.ning.http.client.Request;
130132
import com.ning.http.client.RequestBuilder;
131133
import com.ning.http.client.Response;
132-
import com.ning.http.client.cookie.Cookie;
133134
import com.ning.http.client.cookie.CookieDecoder;
134135
import com.ning.http.client.cookie.CookieEncoder;
135136
import com.ning.http.client.filter.FilterContext;
@@ -215,6 +216,7 @@ public boolean remove(Object o) {
215216
private final Protocol webSocketProtocol = new WebSocketProtocol();
216217
private final boolean allowStopHashedWheelTimer;
217218
private final HashedWheelTimer hashedWheelTimer;
219+
private final long handshakeTimeoutInMillis;
218220

219221
private static boolean isNTLM(List<String> auth) {
220222
return isNonEmpty(auth) && auth.get(0).startsWith("NTLM");
@@ -262,6 +264,8 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
262264
hashedWheelTimer = allowStopHashedWheelTimer ? new HashedWheelTimer() : providerConfig.getHashedWheelTimer();
263265
hashedWheelTimer.start();
264266

267+
handshakeTimeoutInMillis = providerConfig.getHandshakeTimeoutInMillis();
268+
265269
plainBootstrap = new ClientBootstrap(socketChannelFactory);
266270
secureBootstrap = new ClientBootstrap(socketChannelFactory);
267271
webSocketBootstrap = new ClientBootstrap(socketChannelFactory);
@@ -367,7 +371,10 @@ public ChannelPipeline getPipeline() throws Exception {
367371
ChannelPipeline pipeline = pipeline();
368372

369373
try {
370-
pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
374+
SSLEngine sslEngine = createSSLEngine();
375+
SslHandler sslHandler = handshakeTimeoutInMillis > 0 ? new SslHandler(sslEngine, getDefaultBufferPool(), false, ImmediateExecutor.INSTANCE, hashedWheelTimer,
376+
handshakeTimeoutInMillis) : new SslHandler(sslEngine);
377+
pipeline.addLast(SSL_HANDLER, sslHandler);
371378
} catch (Throwable ex) {
372379
abort(cl.future(), ex);
373380
}
@@ -1060,8 +1067,8 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
10601067
}
10611068

10621069
ChannelFuture channelFuture;
1063-
ClientBootstrap bootstrap = (request.getUrl().startsWith(WEBSOCKET) && !useProxy) ?
1064-
(useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
1070+
ClientBootstrap bootstrap = (request.getUrl().startsWith(WEBSOCKET) && !useProxy) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap
1071+
: plainBootstrap);
10651072
bootstrap.setOption("connectTimeoutMillis", config.getConnectionTimeoutInMs());
10661073

10671074
// Do no enable this with win.

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<Str
8686
private boolean disableZeroCopy;
8787

8888
private HashedWheelTimer hashedWheelTimer;
89+
90+
private long handshakeTimeoutInMillis;
8991

9092
public NettyAsyncHttpProviderConfig() {
9193
properties.put(REUSE_ADDRESS, "false");
@@ -163,4 +165,12 @@ public HashedWheelTimer getHashedWheelTimer() {
163165
public void setHashedWheelTimer(HashedWheelTimer hashedWheelTimer) {
164166
this.hashedWheelTimer = hashedWheelTimer;
165167
}
168+
169+
public long getHandshakeTimeoutInMillis() {
170+
return handshakeTimeoutInMillis;
171+
}
172+
173+
public void setHandshakeTimeoutInMillis(long handshakeTimeoutInMillis) {
174+
this.handshakeTimeoutInMillis = handshakeTimeoutInMillis;
175+
}
166176
}

0 commit comments

Comments
 (0)