Skip to content

Commit

Permalink
fix: Correctly handling chunked response streams with gzip (#990)
Browse files Browse the repository at this point in the history
Previous implementation doesn't properly cleanup the last chunk of a chunked response when the response is also gzipped. This in turns prevents proper connection reuse.

Fixes #367
  • Loading branch information
hiranya911 committed Mar 11, 2020
1 parent d3c42d3 commit 1ba2197
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Expand Up @@ -353,8 +353,13 @@ public InputStream getContent() throws IOException {
if (!returnRawInputStream && this.contentEncoding != null) {
String oontentencoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH);
if (CONTENT_ENCODING_GZIP.equals(oontentencoding) || CONTENT_ENCODING_XGZIP.equals(oontentencoding)) {
// Wrap the original stream in a ConsumingInputStream before passing it to
// GZIPInputStream. The GZIPInputStream leaves content unconsumed in the original
// stream (it almost always leaves the last chunk unconsumed in chunked responses).
// ConsumingInputStream ensures that any unconsumed bytes are read at close.
// GZIPInputStream.close() --> ConsumingInputStream.close() --> exhaust(ConsumingInputStream)
lowLevelResponseContent =
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
new GZIPInputStream(new ConsumingInputStream(lowLevelResponseContent));
}
}
// logging (wrap content with LoggingInputStream)
Expand Down
Expand Up @@ -567,6 +567,12 @@ private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding
) {
zipStream.write(dataToCompress);
zipStream.close();

// GZIPInputStream uses a default buffer of 512B. Add enough content to exceed this
// limit, so that some content will be left in the connection.
for (int i = 0; i < 1024; i++) {
byteStream.write('7');
}
mockBytes = byteStream.toByteArray();
}
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
Expand Down Expand Up @@ -594,6 +600,8 @@ public LowLevelHttpResponse execute() throws IOException {
assertFalse(output.isClosed());
assertEquals("abcd", response.parseAsString());
assertTrue(output.isClosed());
// The underlying stream should be fully consumed, even if gzip only returns some of it.
assertEquals(-1, output.read());
}
}

Expand Down

0 comments on commit 1ba2197

Please sign in to comment.