Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly handling chunked response streams with gzip #990

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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