From ab61aea1fc1eb6edac179107be26653cecb067d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20de=20Le=C3=B3n=20Belloc?= Date: Fri, 29 Nov 2013 15:51:57 +0000 Subject: [PATCH] Do not open GZIPInputStream on a closed stream. Issue 358. --- .../okhttp/internal/http/HttpEngine.java | 8 ++++++- .../internal/http/HttpResponseCacheTest.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java index fee0e0159fe4..57ee5d9866e4 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java @@ -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) { + // 304 have no bodies - this will cause GZIPInputStream to throw an exception + responseBodyIn = transferStream; + } else { + responseBodyIn = new GZIPInputStream(transferStream); + } } else { responseBodyIn = transferStream; } diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/HttpResponseCacheTest.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/HttpResponseCacheTest.java index 89f31da204e9..9035218bea4b 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/HttpResponseCacheTest.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/HttpResponseCacheTest.java @@ -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(