hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1084607 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
Date Wed, 23 Mar 2011 14:48:25 GMT
Author: jonm
Date: Wed Mar 23 14:48:24 2011
New Revision: 1084607

URL: http://svn.apache.org/viewvc?rev=1084607&view=rev
Log:
HTTPCLIENT-1073: if caching module strips off a body for an origin response
that should not contain one, it should consume the body off the connection
as well to ensure resources are released.

Added:
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java?rev=1084607&r1=1084606&r2=1084607&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java
Wed Mar 23 14:48:24 2011
@@ -26,12 +26,14 @@
  */
 package org.apache.http.impl.client.cache;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
@@ -46,6 +48,7 @@ import org.apache.http.impl.cookie.DateP
 import org.apache.http.impl.cookie.DateUtils;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
 
 /**
  * @since 4.1
@@ -60,11 +63,13 @@ class ResponseProtocolCompliance {
      *
      * @param request The {@link HttpRequest} that generated an origin hit and response
      * @param response The {@link HttpResponse} from the origin server
-     * @throws ClientProtocolException when we are unable to 'convert' the response to a
compliant one
+     * @throws IOException 
      */
     public void ensureProtocolCompliance(HttpRequest request, HttpResponse response)
-            throws ClientProtocolException {
+            throws IOException {
         if (backendResponseMustNotHaveBody(request, response)) {
+            HttpEntity body = response.getEntity();
+            if (body != null) EntityUtils.consume(body);
             response.setEntity(null);
         }
 

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java?rev=1084607&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
Wed Mar 23 14:48:24 2011
@@ -0,0 +1,82 @@
+/*
+ * ====================================================================
+ * 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.impl.client.cache;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.util.Date;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.cookie.DateUtils;
+import org.apache.http.message.BasicHttpResponse;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class TestResponseProtocolCompliance {
+
+    private ResponseProtocolCompliance impl;
+
+    @Before
+    public void setUp() {
+        impl = new ResponseProtocolCompliance();
+    }
+    
+    @Test
+    public void consumesBodyIfOriginSendsOneInResponseToHEAD() throws Exception {
+        HttpRequest req = new HttpHead("http://foo.example.com/");
+        HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
+        resp.setHeader("Date", DateUtils.formatDate(new Date()));
+        resp.setHeader("Server", "MyServer/1.0");
+
+        int nbytes = 128;
+        resp.setHeader("Content-Length","" + nbytes);
+        byte[] buf = HttpTestUtils.getRandomBytes(nbytes);
+        final Flag closed = new Flag();
+        ByteArrayInputStream bais = new ByteArrayInputStream(buf) {
+            @Override
+            public void close() {
+                closed.set = true;
+            }
+        };
+        resp.setEntity(new InputStreamEntity(bais, -1));
+        
+        impl.ensureProtocolCompliance(req, resp);
+        assertNull(resp.getEntity());
+        assertTrue(closed.set || bais.read() == -1);
+    }
+    
+    private static class Flag {
+        public boolean set;
+    }
+}

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

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

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



Mime
View raw message