Skip to content

Commit a6d1cd2

Browse files
committed
Fix chunked request body handling with HTTPS proxy, close AsyncHttpClient#1571
Motivation: Sending a chunked request body (hence not direct) cause AHC to crash. Motivation: Fix SslHandler position that should be before the ChunkedWriteHandler. Result: Chunked request works with HTTPS proxy.
1 parent 4d03ad6 commit a6d1cd2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public Future<Channel> updatePipelineForHttpTunneling(ChannelPipeline pipeline,
345345
if (!isSslHandlerConfigured(pipeline)) {
346346
SslHandler sslHandler = createSslHandler(requestUri.getHost(), requestUri.getExplicitPort());
347347
whenHanshaked = sslHandler.handshakeFuture();
348-
pipeline.addBefore(AHC_HTTP_HANDLER, SSL_HANDLER, sslHandler);
348+
pipeline.addBefore(CHUNKED_WRITER_HANDLER, SSL_HANDLER, sslHandler);
349349
}
350350
pipeline.addAfter(SSL_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
351351

@@ -380,10 +380,11 @@ public SslHandler addSslHandler(ChannelPipeline pipeline, Uri uri, String virtua
380380
}
381381

382382
SslHandler sslHandler = createSslHandler(peerHost, peerPort);
383-
if (hasSocksProxyHandler)
383+
if (hasSocksProxyHandler) {
384384
pipeline.addAfter(SOCKS_HANDLER, SSL_HANDLER, sslHandler);
385-
else
385+
} else {
386386
pipeline.addFirst(SSL_HANDLER, sslHandler);
387+
}
387388
return sslHandler;
388389
}
389390

client/src/test/java/org/asynchttpclient/proxy/HttpsProxyTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.asynchttpclient.proxy;
1414

1515
import org.asynchttpclient.*;
16+
import org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator;
1617
import org.asynchttpclient.test.EchoHandler;
1718
import org.eclipse.jetty.proxy.ConnectHandler;
1819
import org.eclipse.jetty.server.Server;
@@ -23,6 +24,7 @@
2324
import org.testng.annotations.Test;
2425

2526
import static org.asynchttpclient.Dsl.*;
27+
import static org.asynchttpclient.test.TestUtils.LARGE_IMAGE_BYTES;
2628
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
2729
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
2830
import static org.testng.Assert.assertEquals;
@@ -84,6 +86,19 @@ public void testConfigProxy() throws Exception {
8486
}
8587
}
8688

89+
@Test
90+
public void testNoDirectRequestBodyWithProxy() throws Exception {
91+
AsyncHttpClientConfig config = config()
92+
.setFollowRedirect(true)
93+
.setProxyServer(proxyServer("localhost", port1).build())
94+
.setUseInsecureTrustManager(true)
95+
.build();
96+
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
97+
Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get();
98+
assertEquals(r.getStatusCode(), 200);
99+
}
100+
}
101+
87102
@Test
88103
public void testPooledConnectionsWithProxy() throws Exception {
89104

0 commit comments

Comments
 (0)