Skip to content

Commit c5b6857

Browse files
committed
HttpContentDecompressor is misplaced after upgrading pipeline for HTTP tunnelling, close #1583
1 parent c035ead commit c5b6857

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

Diff for: client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

+1-1
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(CHUNKED_WRITER_HANDLER, SSL_HANDLER, sslHandler);
348+
pipeline.addBefore(INFLATER_HANDLER, SSL_HANDLER, sslHandler);
349349
}
350350
pipeline.addAfter(SSL_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
351351

Diff for: client/src/test/java/org/asynchttpclient/proxy/HttpsProxyTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ public void testNoDirectRequestBodyWithProxy() throws Exception {
9999
}
100100
}
101101

102+
@Test
103+
public void testDecompressBodyWithProxy() throws Exception {
104+
AsyncHttpClientConfig config = config()
105+
.setFollowRedirect(true)
106+
.setProxyServer(proxyServer("localhost", port1).build())
107+
.setUseInsecureTrustManager(true)
108+
.build();
109+
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
110+
String body = "hello world";
111+
Response r = asyncHttpClient.executeRequest(post(getTargetUrl2())
112+
.setHeader("X-COMPRESS", "true")
113+
.setBody(body)).get();
114+
assertEquals(r.getStatusCode(), 200);
115+
assertEquals(r.getResponseBody(), body);
116+
}
117+
}
118+
102119
@Test
103120
public void testPooledConnectionsWithProxy() throws Exception {
104121

Diff for: client/src/test/java/org/asynchttpclient/test/EchoHandler.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import javax.servlet.http.Cookie;
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
26+
import java.io.ByteArrayOutputStream;
2627
import java.io.IOException;
2728
import java.nio.charset.StandardCharsets;
2829
import java.util.Enumeration;
30+
import java.util.zip.Deflater;
2931

30-
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
31-
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_MD5;
32+
import static io.netty.handler.codec.http.HttpHeaderNames.*;
33+
import static io.netty.handler.codec.http.HttpHeaderValues.CHUNKED;
34+
import static io.netty.handler.codec.http.HttpHeaderValues.DEFLATE;
3235

3336
public class EchoHandler extends AbstractHandler {
3437

@@ -115,18 +118,14 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
115118
}
116119
}
117120

118-
String requestBodyLength = httpRequest.getHeader("X-" + CONTENT_LENGTH);
121+
if (httpRequest.getHeader("X-COMPRESS") != null) {
122+
byte[] compressed = deflate(IOUtils.toByteArray(httpRequest.getInputStream()));
123+
httpResponse.addIntHeader(CONTENT_LENGTH.toString(), compressed.length);
124+
httpResponse.addHeader(CONTENT_ENCODING.toString(), DEFLATE.toString());
125+
httpResponse.getOutputStream().write(compressed, 0, compressed.length);
119126

120-
if (requestBodyLength != null) {
121-
byte[] requestBodyBytes = IOUtils.toByteArray(httpRequest.getInputStream());
122-
int total = requestBodyBytes.length;
123-
124-
httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total);
125-
String md5 = TestUtils.md5(requestBodyBytes, 0, total);
126-
httpResponse.addHeader(CONTENT_MD5.toString(), md5);
127-
128-
httpResponse.getOutputStream().write(requestBodyBytes, 0, total);
129127
} else {
128+
httpResponse.addHeader(TRANSFER_ENCODING.toString(), CHUNKED.toString());
130129
int size = 16384;
131130
if (httpRequest.getContentLength() > 0) {
132131
size = httpRequest.getContentLength();
@@ -148,4 +147,21 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
148147
// FIXME don't always close, depends on the test, cf ReactiveStreamsTest
149148
httpResponse.getOutputStream().close();
150149
}
150+
151+
private static byte[] deflate(byte[] input) throws IOException {
152+
Deflater compressor = new Deflater();
153+
compressor.setLevel(Deflater.BEST_COMPRESSION);
154+
155+
compressor.setInput(input);
156+
compressor.finish();
157+
158+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length)) {
159+
byte[] buf = new byte[1024];
160+
while (!compressor.finished()) {
161+
int count = compressor.deflate(buf);
162+
bos.write(buf, 0, count);
163+
}
164+
return bos.toByteArray();
165+
}
166+
}
151167
}

0 commit comments

Comments
 (0)