Skip to content

Commit

Permalink
Do not open GZIPInputStream on a closed stream. Issue 358.
Browse files Browse the repository at this point in the history
  • Loading branch information
pablolb committed Nov 29, 2013
1 parent e138eb7 commit ab61aea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Expand Up @@ -465,7 +465,13 @@ private void initContentStream(InputStream transferStream) throws IOException {
// the content encoding.
responseHeaders.stripContentEncoding();
responseHeaders.stripContentLength();
responseBodyIn = new GZIPInputStream(transferStream);
int responseCode = responseHeaders.getHeaders().getResponseCode();
if (responseCode == HttpURLConnectionImpl.HTTP_NOT_MODIFIED) {

This comment has been minimized.

Copy link
@codefromthecrypt

codefromthecrypt Dec 3, 2013

it is true that 304 shouldn't have a body, but also any response to a HEAD, DELETE, and some other codes also do not. I wonder if there's a way to address this without special casing one of the many reasons that could result in an empty body..

// 304 have no bodies - this will cause GZIPInputStream to throw an exception
responseBodyIn = transferStream;
} else {
responseBodyIn = new GZIPInputStream(transferStream);
}
} else {
responseBodyIn = transferStream;
}
Expand Down
Expand Up @@ -975,6 +975,27 @@ private void assertNonIdentityEncodingCached(MockResponse response) throws Excep
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
assertEquals("DEFDEFDEF", readAscii(openConnection(server.getUrl("/"))));
}

@Test public void notModifiedSpecifiesEncodingWithConditionals() throws Exception {
String lastModified = formatDate(-2, TimeUnit.HOURS);
server.enqueue(new MockResponse()
.setBody(gzip("ABCABCABC".getBytes("UTF-8")))
.addHeader("Content-Encoding: gzip")
.addHeader("Last-Modified: " + lastModified)
.addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)
.addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse()
.setBody("DEFDEFDEF"));

server.play();
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
URLConnection connection = openConnection(server.getUrl("/"));
connection.setRequestProperty("If-Modified-Since", lastModified);
assertEquals("", readAscii(connection));
assertEquals("DEFDEFDEF", readAscii(openConnection(server.getUrl("/"))));
}

@Test public void expiresDateBeforeModifiedDate() throws Exception {
assertConditionallyCached(
Expand Down

0 comments on commit ab61aea

Please sign in to comment.