Skip to content

Commit 3737fb2

Browse files
committed
Don't try to write proxified request until SslHandler has handshaked, close AsyncHttpClient#1559
Motivation: We're trying to write the proxyfied request as soon as we've installed the SslHandler. Behavior seems broken and handshake times out for large request payloads. Modification: Delay request until Sslhandler has handshaked. Result: No more handshake timeouts. Note that we can still have remotely closed exceptions if remote peer closed sockets while we're uploading.
1 parent 6e4c1a6 commit 3737fb2

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,18 @@ private SslHandler createSslHandler(String peerHost, int peerPort) {
334334
return sslHandler;
335335
}
336336

337-
public void updatePipelineForHttpTunneling(ChannelPipeline pipeline, Uri requestUri) {
337+
public Future<Channel> updatePipelineForHttpTunneling(ChannelPipeline pipeline, Uri requestUri) {
338+
339+
Future<Channel> whenHanshaked = null;
340+
338341
if (pipeline.get(HTTP_CLIENT_CODEC) != null)
339342
pipeline.remove(HTTP_CLIENT_CODEC);
340343

341344
if (requestUri.isSecured()) {
342345
if (!isSslHandlerConfigured(pipeline)) {
343-
pipeline.addBefore(AHC_HTTP_HANDLER, SSL_HANDLER, createSslHandler(requestUri.getHost(), requestUri.getExplicitPort()));
346+
SslHandler sslHandler = createSslHandler(requestUri.getHost(), requestUri.getExplicitPort());
347+
whenHanshaked = sslHandler.handshakeFuture();
348+
pipeline.addBefore(AHC_HTTP_HANDLER, SSL_HANDLER, sslHandler);
344349
}
345350
pipeline.addAfter(SSL_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
346351

@@ -352,6 +357,7 @@ public void updatePipelineForHttpTunneling(ChannelPipeline pipeline, Uri request
352357
pipeline.addAfter(AHC_HTTP_HANDLER, AHC_WS_HANDLER, wsHandler);
353358
pipeline.remove(AHC_HTTP_HANDLER);
354359
}
360+
return whenHanshaked;
355361
}
356362

357363
public SslHandler addSslHandler(ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler) {

client/src/main/java/org/asynchttpclient/netty/handler/intercept/ConnectSuccessInterceptor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.asynchttpclient.netty.handler.intercept;
1515

1616
import io.netty.channel.Channel;
17+
import io.netty.util.concurrent.Future;
1718
import org.asynchttpclient.Request;
1819
import org.asynchttpclient.RequestBuilder;
1920
import org.asynchttpclient.netty.NettyResponseFuture;
@@ -47,10 +48,16 @@ public boolean exitAfterHandlingConnect(Channel channel,
4748
Uri requestUri = request.getUri();
4849
LOGGER.debug("Connecting to proxy {} for scheme {}", proxyServer, requestUri.getScheme());
4950

50-
channelManager.updatePipelineForHttpTunneling(channel.pipeline(), requestUri);
51+
Future<Channel> whenHandshaked = channelManager.updatePipelineForHttpTunneling(channel.pipeline(), requestUri);
52+
5153
future.setReuseChannel(true);
5254
future.setConnectAllowed(false);
53-
requestSender.drainChannelAndExecuteNextRequest(channel, future, new RequestBuilder(future.getTargetRequest()).build());
55+
Request targetRequest = new RequestBuilder(future.getTargetRequest()).build();
56+
if (whenHandshaked == null) {
57+
requestSender.drainChannelAndExecuteNextRequest(channel, future, targetRequest);
58+
} else {
59+
requestSender.drainChannelAndExecuteNextRequest(channel, future, targetRequest, whenHandshaked);
60+
}
5461

5562
return true;
5663
}

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ private void scheduleReadTimeout(NettyResponseFuture<?> nettyResponseFuture) {
460460

461461
public void abort(Channel channel, NettyResponseFuture<?> future, Throwable t) {
462462

463-
if (channel != null) {
463+
if (channel != null && channel.isActive()) {
464464
channelManager.closeChannel(channel);
465465
}
466466

@@ -604,7 +604,8 @@ public boolean isClosed() {
604604
return clientState.isClosed();
605605
}
606606

607-
public void drainChannelAndExecuteNextRequest(final Channel channel, final NettyResponseFuture<?> future,
607+
public void drainChannelAndExecuteNextRequest(final Channel channel,
608+
final NettyResponseFuture<?> future,
608609
Request nextRequest) {
609610
Channels.setAttribute(channel, new OnLastHttpContentCallback(future) {
610611
@Override
@@ -613,4 +614,24 @@ public void call() {
613614
}
614615
});
615616
}
617+
618+
public void drainChannelAndExecuteNextRequest(final Channel channel,
619+
final NettyResponseFuture<?> future,
620+
Request nextRequest,
621+
Future<Channel> whenHandshaked) {
622+
Channels.setAttribute(channel, new OnLastHttpContentCallback(future) {
623+
@Override
624+
public void call() {
625+
whenHandshaked.addListener(f -> {
626+
if (f.isSuccess()) {
627+
sendNextRequest(nextRequest, future);
628+
} else {
629+
future.abort(f.cause());
630+
}
631+
}
632+
);
633+
}
634+
});
635+
}
636+
616637
}

0 commit comments

Comments
 (0)