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 2011/04/02 13:34:52 UTC

svn commit: r1088000 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/client/entity/ httpclient/src/test/java/org/apache/http/client/entity/ httpclient/src/test/java/org/apache/http/impl/client/

Author: olegk
Date: Sat Apr  2 11:34:52 2011
New Revision: 1088000

URL: http://svn.apache.org/viewvc?rev=1088000&view=rev
Log:
HTTPCLIENT-1075: Decompressing entities (DeflateDecompressingEntity, GzipDecompressingEntity) do not correctly handle content streaming

Contributed by James Abley <james.abley at gmail.com> 

Added:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java   (with props)
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/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1088000&r1=1087999&r2=1088000&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Sat Apr  2 11:34:52 2011
@@ -1,4 +1,16 @@
-Changes since 4.1
+Changes since 4.1.1
+
+* [HTTPCLIENT-1075] Decompressing entities (DeflateDecompressingEntity, GzipDecompressingEntity) 
+  do not correctly handle content streaming.
+  Contributed by James Abley <james.abley at gmail.com> 
+
+
+Release 4.1.1
+-------------------
+
+The HttpClient 4.1.1 is a bug fix release that addresses a number of issues reported since 
+release 4.1, including one critical security issue (HTTPCLIENT-1061). All users of HttpClient 4.0.x 
+and 4.1 are strongly encouraged to upgrade.
 
 * [HTTPCLIENT-1069] HttpHostConnectException not correctly retried for direct and non-tunnelled 
   proxy connections. 

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=1088000&r1=1087999&r2=1088000&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 Sat Apr  2 11:34:52 2011
@@ -45,6 +45,12 @@ abstract class DecompressingEntity exten
     private static final int BUFFER_SIZE = 1024 * 2;
 
     /**
+     * DecompressingEntities are not repeatable, so they will return the same
+     * InputStream instance when {@link #getContent()} is called.
+     */
+    private InputStream content;
+
+    /**
      * Creates a new {@link DecompressingEntity}.
      *
      * @param wrapped
@@ -54,6 +60,23 @@ abstract class DecompressingEntity exten
         super(wrapped);
     }
 
+    abstract InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public InputStream getContent() throws IOException {
+        if (wrappedEntity.isStreaming()) {
+            if (content == null) {
+                content = getDecompressingInputStream(wrappedEntity.getContent());
+            }
+            return content;
+        } else {
+            return getDecompressingInputStream(wrappedEntity.getContent());
+        }
+    }
+
     /**
      * {@inheritDoc}
      */

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java?rev=1088000&r1=1087999&r2=1088000&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java Sat Apr  2 11:34:52 2011
@@ -63,12 +63,14 @@ public class DeflateDecompressingEntity 
     }
 
     /**
-     * {@inheritDoc}
+     * Returns the non-null InputStream that should be returned to by all requests to
+     * {@link #getContent()}.
+     *
+     * @return a non-null InputStream
+     * @throws IOException if there was a problem
      */
     @Override
-    public InputStream getContent() throws IOException {
-        InputStream wrapped = this.wrappedEntity.getContent();
-
+    InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
         /*
          * A zlib stream will have a header.
          *

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java?rev=1088000&r1=1087999&r2=1088000&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java Sat Apr  2 11:34:52 2011
@@ -51,16 +51,9 @@ public class GzipDecompressingEntity ext
         super(entity);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public InputStream getContent() throws IOException, IllegalStateException {
-
-        // the wrapped entity's getContent() decides about repeatability
-        InputStream wrappedin = wrappedEntity.getContent();
-
-        return new GZIPInputStream(wrappedin);
+    InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
+        return new GZIPInputStream(wrapped);
     }
 
     /**

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java?rev=1088000&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java Sat Apr  2 11:34:52 2011
@@ -0,0 +1,109 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.client.entity;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CRC32;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestDecompressingEntity {
+
+    @Test
+    public void testNonStreaming() throws Exception {
+        CRC32 crc32 = new CRC32();
+        StringEntity wrapped = new StringEntity("1234567890", "ASCII");
+        ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
+        Assert.assertFalse(entity.isStreaming());
+        String s = EntityUtils.toString(entity);
+        Assert.assertEquals("1234567890", s);
+        Assert.assertEquals(639479525L, crc32.getValue());
+        InputStream in1 = entity.getContent();
+        InputStream in2 = entity.getContent();
+        Assert.assertTrue(in1 != in2);
+    }
+
+    @Test
+    public void testStreaming() throws Exception {
+        CRC32 crc32 = new CRC32();
+        ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes("ASCII"));
+        InputStreamEntity wrapped = new InputStreamEntity(in, -1);
+        ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
+        Assert.assertTrue(entity.isStreaming());
+        String s = EntityUtils.toString(entity);
+        Assert.assertEquals("1234567890", s);
+        Assert.assertEquals(639479525L, crc32.getValue());
+        InputStream in1 = entity.getContent();
+        InputStream in2 = entity.getContent();
+        Assert.assertTrue(in1 == in2);
+        EntityUtils.consume(entity);
+        EntityUtils.consume(entity);
+    }
+
+    @Test
+    public void testWriteToStream() throws Exception {
+        CRC32 crc32 = new CRC32();
+        StringEntity wrapped = new StringEntity("1234567890", "ASCII");
+        ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
+        Assert.assertFalse(entity.isStreaming());
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        entity.writeTo(out);
+
+        String s = new String(out.toByteArray(), "ASCII");
+        Assert.assertEquals("1234567890", s);
+        Assert.assertEquals(639479525L, crc32.getValue());
+    }
+
+    static class ChecksumEntity extends DecompressingEntity {
+
+        private final Checksum checksum;
+
+        public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
+            super(wrapped);
+            this.checksum = checksum;
+        }
+
+        @Override
+        InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
+            return new CheckedInputStream(wrapped, this.checksum);
+        }
+
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java?rev=1088000&r1=1087999&r2=1088000&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java Sat Apr  2 11:34:52 2011
@@ -313,6 +313,34 @@ public class TestContentCodings extends 
         client.getConnectionManager().shutdown();
     }
 
+    @Test
+    public void gzipResponsesWorkWithBasicResponseHandler() throws Exception {
+        final String entityText = "Hello, this is some plain text coming back.";
+
+        this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
+
+        DefaultHttpClient client = createHttpClient();
+        HttpGet request = new HttpGet("/some-resource");
+        String response = client.execute(getServerHttp(), request, new BasicResponseHandler());
+        Assert.assertEquals("The entity text is correctly transported", entityText, response);
+
+        client.getConnectionManager().shutdown();
+    }
+
+    @Test
+    public void deflateResponsesWorkWithBasicResponseHandler() throws Exception {
+        final String entityText = "Hello, this is some plain text coming back.";
+
+        this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, false));
+
+        DefaultHttpClient client = createHttpClient();
+        HttpGet request = new HttpGet("/some-resource");
+        String response = client.execute(getServerHttp(), request, new BasicResponseHandler());
+        Assert.assertEquals("The entity text is correctly transported", entityText, response);
+
+        client.getConnectionManager().shutdown();
+    }
+
     /**
      * Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream
      * Content-Coding.