hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1084711 - in /httpcomponents/httpclient/branches/4.1.x/httpclient-cache: ./ src/main/java/org/apache/http/client/cache/ src/main/java/org/apache/http/impl/client/cache/ src/test/java/org/apache/http/impl/client/cache/
Date Wed, 23 Mar 2011 20:06:31 GMT
Author: jonm
Date: Wed Mar 23 20:06:31 2011
New Revision: 1084711

URL: http://svn.apache.org/viewvc?rev=1084711&view=rev
Log:
HTTPCLIENT-1073: In the event a 100 (Continue) response makes its way up
to the caching module, but the original request did not expect one, it's
acceptable for the caching module to throw an exception but it had better
consume the body of the 100 (Continue) response before doing so to ensure
proper release of resources. Encountering this would require the underlying
(non-caching) HttpClient to not handle the expect-continue handshake properly
*AND* for the origin server to send a 100-Continue response out of turn
*WITH* a response body. Highly unlikely, but at least this codepath is
covered properly now. (backport of r1084655 from trunk to 4.1.x branch)

Modified:
    httpcomponents/httpclient/branches/4.1.x/httpclient-cache/   (props changed)
    httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheStorage.java
  (props changed)
    httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCacheStorage.java
  (props changed)
    httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
    httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java

Propchange: httpcomponents/httpclient/branches/4.1.x/httpclient-cache/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Mar 23 20:06:31 2011
@@ -1,4 +1,4 @@
 /httpcomponents/httpclient/branches/4.0.x/httpclient-cache:950681-950688
 /httpcomponents/httpclient/branches/branch_4_1/httpclient-cache:755593-811107
 /httpcomponents/httpclient/branches/notice-plugin-test/httpclient-cache:1024348-1031454
-/httpcomponents/httpclient/trunk/httpclient-cache:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649
+/httpcomponents/httpclient/trunk/httpclient-cache:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649,1084655

Propchange: httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheStorage.java
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Mar 23 20:06:31 2011
@@ -1,3 +1,3 @@
 /httpcomponents/httpclient/branches/4.0.x/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java:950681-950688
 /httpcomponents/httpclient/branches/branch_4_1/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java:755593-811107
-/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheStorage.java:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649
+/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheStorage.java:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649,1084655

Propchange: httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCacheStorage.java
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Mar 23 20:06:31 2011
@@ -1,3 +1,3 @@
 /httpcomponents/httpclient/branches/4.0.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java:950681-950688
 /httpcomponents/httpclient/branches/branch_4_1/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java:755593-811107
-/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCacheStorage.java:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649
+/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCacheStorage.java:1080419,1080422,1080575,1084590,1084607,1084610-1084611,1084649,1084655

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java?rev=1084711&r1=1084710&r2=1084711&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
Wed Mar 23 20:06:31 2011
@@ -56,6 +56,8 @@ import org.apache.http.util.EntityUtils;
 @Immutable
 class ResponseProtocolCompliance {
 
+    private static final String UNEXPECTED_100_CONTINUE = "The incoming request did not contain
a "
+                    + "100-continue header, but the response was a Status 100, continue.";
     private static final String UNEXPECTED_PARTIAL_CONTENT = "partial content was returned
for a request that did not ask for it";
 
     /**
@@ -207,26 +209,18 @@ class ResponseProtocolCompliance {
     }
 
     private void requestDidNotExpect100ContinueButResponseIsOne(HttpRequest request,
-            HttpResponse response) throws ClientProtocolException {
+            HttpResponse response) throws IOException {
         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) {
             return;
         }
-
-        if (!requestWasWrapped(request)) {
-            return;
-        }
-
-        ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request);
-
-        if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) {
-            return;
-        }
-
-        if (originalRequestDidNotExpectContinue((RequestWrapper) request)) {
-            throw new ClientProtocolException("The incoming request did not contain a "
-                    + "100-continue header, but the response was a Status 100, continue.");
-
+        
+        HttpRequest originalRequest = requestWasWrapped(request) ? 
+                ((RequestWrapper)request).getOriginal() : request;
+        if (originalRequest instanceof HttpEntityEnclosingRequest) {
+            if (((HttpEntityEnclosingRequest)originalRequest).expectContinue()) return;
         }
+        consumeBody(response);
+        throw new ClientProtocolException(UNEXPECTED_100_CONTINUE);
     }
 
     private void transferEncodingIsNotReturnedTo1_0Client(HttpRequest request, HttpResponse
response) {
@@ -248,18 +242,6 @@ class ResponseProtocolCompliance {
         response.removeHeaders(HTTP.TRANSFER_ENCODING);
     }
 
-    private boolean originalRequestDidNotExpectContinue(RequestWrapper request) {
-
-        try {
-            HttpEntityEnclosingRequest original = (HttpEntityEnclosingRequest) request
-                    .getOriginal();
-
-            return !original.expectContinue();
-        } catch (ClassCastException ex) {
-            return false;
-        }
-    }
-
     private ProtocolVersion getOriginalRequestProtocol(RequestWrapper request) {
         return request.getOriginal().getProtocolVersion();
     }

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java?rev=1084711&r1=1084710&r2=1084711&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
Wed Mar 23 20:06:31 2011
@@ -31,6 +31,8 @@ import static org.junit.Assert.*;
 import java.io.ByteArrayInputStream;
 import java.util.Date;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
@@ -38,13 +40,14 @@ import org.apache.http.HttpVersion;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpHead;
+import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.cookie.DateUtils;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
 import org.apache.http.message.BasicHttpResponse;
 import org.junit.Before;
 import org.junit.Test;
 
-
 public class TestResponseProtocolCompliance {
 
     private ResponseProtocolCompliance impl;
@@ -125,4 +128,25 @@ public class TestResponseProtocolComplia
         }
         assertTrue(closed.set || bais.read() == -1);
     }
+    
+    @Test
+    public void consumesBodyOf100ContinueResponseIfItArrives() throws Exception {
+        HttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("POST", "/",
HttpVersion.HTTP_1_1);
+        int nbytes = 128;
+        req.setHeader("Content-Length","" + nbytes);
+        req.setHeader("Content-Type", "application/octet-stream");
+        HttpEntity postBody = new ByteArrayEntity(HttpTestUtils.getRandomBytes(nbytes));
+        req.setEntity(postBody);
+        
+        HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_CONTINUE,
"Continue");
+        final Flag closed = new Flag();
+        ByteArrayInputStream bais = makeTrackableBody(nbytes, closed);
+        resp.setEntity(new InputStreamEntity(bais, -1));
+        
+        try {
+            impl.ensureProtocolCompliance(req, resp);
+        } catch (ClientProtocolException expected) {
+        }
+        assertTrue(closed.set || bais.read() == -1);
+    }
 }



Mime
View raw message