You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/12/17 17:12:18 UTC

svn commit: r1422997 - in /httpcomponents/httpclient/trunk: RELEASE_NOTES.txt httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java

Author: olegk
Date: Mon Dec 17 16:12:18 2012
New Revision: 1422997

URL: http://svn.apache.org/viewvc?rev=1422997&view=rev
Log:
HTTPCLIENT-1281: GzipDecompressingEntity does not release InputStream when an IOException occurs while reading the Gzip header
Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net>

Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1422997&r1=1422996&r2=1422997&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Mon Dec 17 16:12:18 2012
@@ -1,6 +1,10 @@
 Changes in trunk
 -------------------
 
+* [HTTPCLIENT-1281] GzipDecompressingEntity does not release InputStream when an IOException occurs 
+  while reading the Gzip header.
+  Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net> 
+
 * [HTTPCLIENT-1277] Caching client sends a 304 to an unconditional request. 
   Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net> 
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java?rev=1422997&r1=1422996&r2=1422997&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java Mon Dec 17 16:12:18 2012
@@ -31,6 +31,7 @@ import java.io.OutputStream;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.HttpEntityWrapper;
+import org.apache.http.util.EntityUtils;
 
 /**
  * Common base class for decompressing {@link HttpEntity} implementations.
@@ -67,13 +68,18 @@ abstract class DecompressingEntity exten
      */
     @Override
     public InputStream getContent() throws IOException {
-        if (wrappedEntity.isStreaming()) {
-            if (content == null) {
-                content = getDecompressingInputStream(wrappedEntity.getContent());
+        try {
+            if (wrappedEntity.isStreaming()) {
+                if (content == null) {
+                    content = getDecompressingInputStream(wrappedEntity.getContent());
+                }
+                return content;
+            } else {
+                return getDecompressingInputStream(wrappedEntity.getContent());
             }
-            return content;
-        } else {
-            return getDecompressingInputStream(wrappedEntity.getContent());
+        } catch (IOException e) {
+            EntityUtils.consume(wrappedEntity);
+            throw e;
         }
     }
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java?rev=1422997&r1=1422996&r2=1422997&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestGZip.java Mon Dec 17 16:12:18 2012
@@ -28,12 +28,17 @@
 package org.apache.http.client.entity;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import junit.framework.Assert;
 
 import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.ContentType;
+import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.util.EntityUtils;
 import org.junit.Test;
@@ -63,4 +68,30 @@ public class TestGZip {
         Assert.assertEquals("some kind of text", EntityUtils.toString(gunzipe, Consts.ASCII));
     }
 
+    @Test
+    public void testGzipDecompressingEntityDoesNotCrashInConstructorAndLeaveInputStreamOpen()
+            throws Exception {
+        final AtomicBoolean inputStreamIsClosed = new AtomicBoolean(false);
+        HttpEntity in = new InputStreamEntity(new InputStream() {
+            @Override
+            public int read() throws IOException {
+                throw new IOException("An exception occurred");
+            }
+
+            @Override
+            public void close() throws IOException {
+                inputStreamIsClosed.set(true);
+            }
+
+        }, 123);
+        GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(in);
+        try {
+            gunzipe.getContent();
+        } catch (IOException e) {
+            // As I cannot get the content, GzipDecompressingEntity is supposed
+            // to have released everything
+            Assert.assertTrue(inputStreamIsClosed.get());
+        }
+    }
+
 }