hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1763890 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
Date Sat, 08 Oct 2016 11:13:00 GMT
Author: olegk
Date: Sat Oct  8 11:13:00 2016
New Revision: 1763890

URL: http://svn.apache.org/viewvc?rev=1763890&view=rev
Log:
HTTPCORE-434: Handling of HEAD responses without payload headers

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java?rev=1763890&r1=1763889&r2=1763890&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
Sat Oct  8 11:13:00 2016
@@ -40,7 +40,6 @@ import org.apache.hc.core5.http.HttpResp
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.HttpVersion;
 import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.message.BasicHeaderIterator;
 import org.apache.hc.core5.http.message.BasicTokenIterator;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.util.Args;
@@ -81,14 +80,11 @@ public class DefaultConnectionReuseStrat
         Args.notNull(response, "HTTP response");
 
         if (request != null) {
-            final Header[] connHeaders = request.getHeaders(HttpHeaders.CONNECTION);
-            if (connHeaders.length != 0) {
-                final Iterator<String> ti = new BasicTokenIterator(new BasicHeaderIterator(connHeaders,
null));
-                while (ti.hasNext()) {
-                    final String token = ti.next();
-                    if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
-                        return false;
-                    }
+            final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION));
+            while (ti.hasNext()) {
+                final String token = ti.next();
+                if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
+                    return false;
                 }
             }
         }
@@ -101,7 +97,7 @@ public class DefaultConnectionReuseStrat
                 return false;
             }
         } else {
-            if (canResponseHaveBody(response) && response.containsHeaders(HttpHeaders.CONTENT_LENGTH)
!= 1) {
+            if (canResponseHaveBody(request, response) && response.containsHeaders(HttpHeaders.CONTENT_LENGTH)
!= 1) {
                 return false;
             }
         }
@@ -109,15 +105,15 @@ public class DefaultConnectionReuseStrat
         // Check for the "Connection" header. If that is absent, check for
         // the "Proxy-Connection" header. The latter is an unspecified and
         // broken but unfortunately common extension of HTTP.
-        Header[] connHeaders = response.getHeaders(HttpHeaders.CONNECTION);
-        if (connHeaders.length == 0) {
-            connHeaders = response.getHeaders("Proxy-Connection");
+        Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION);
+        if (!headerIterator.hasNext()) {
+            headerIterator = response.headerIterator("Proxy-Connection");
         }
 
         final ProtocolVersion ver = context.getProtocolVersion();
-        if (connHeaders.length != 0) {
+        if (headerIterator.hasNext()) {
             if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
-                final Iterator<String> it = new BasicTokenIterator(new BasicHeaderIterator(connHeaders,
null));
+                final Iterator<String> it = new BasicTokenIterator(headerIterator);
                 while (it.hasNext()) {
                     final String token = it.next();
                     if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
@@ -126,7 +122,7 @@ public class DefaultConnectionReuseStrat
                 }
                 return true;
             }
-            final Iterator<String> it = new BasicTokenIterator(new BasicHeaderIterator(connHeaders,
null));
+            final Iterator<String> it = new BasicTokenIterator(headerIterator);
             while (it.hasNext()) {
                 final String token = it.next();
                 if (HeaderElements.KEEP_ALIVE.equalsIgnoreCase(token)) {
@@ -138,7 +134,10 @@ public class DefaultConnectionReuseStrat
         return ver.greaterEquals(HttpVersion.HTTP_1_1);
     }
 
-    private boolean canResponseHaveBody(final HttpResponse response) {
+    private boolean canResponseHaveBody(final HttpRequest request, final HttpResponse response)
{
+        if (request != null && request.getMethod().equalsIgnoreCase("HEAD")) {
+            return false;
+        }
         final int status = response.getCode();
         return status >= HttpStatus.SC_SUCCESS
             && status != HttpStatus.SC_NO_CONTENT

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java?rev=1763890&r1=1763889&r2=1763890&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
Sat Oct  8 11:13:00 2016
@@ -28,12 +28,12 @@
 package org.apache.hc.core5.http.impl;
 
 import org.apache.hc.core5.http.ConnectionReuseStrategy;
-import org.apache.hc.core5.http.ClassicHttpRequest;
-import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.HttpVersion;
-import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
-import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.message.BasicHttpRequest;
+import org.apache.hc.core5.http.message.BasicHttpResponse;
 import org.apache.hc.core5.http.protocol.BasicHttpContext;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.junit.Assert;
@@ -62,19 +62,19 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testNoContentLengthResponseHttp1_0() throws Exception {
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
     }
 
     @Test
     public void testNoContentLengthResponseHttp1_1() throws Exception {
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
     }
 
     @Test
     public void testChunkedContent() throws Exception {
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         Assert.assertTrue(reuseStrategy.keepAlive(null, response, context));
     }
@@ -82,7 +82,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testIgnoreInvalidKeepAlive() throws Exception {
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Connection", "keep-alive");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
     }
@@ -90,7 +90,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testExplicitClose() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "close");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -100,7 +100,7 @@ public class TestDefaultConnectionReuseS
     public void testExplicitKeepAlive() throws Exception {
         // Use HTTP 1.0
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         response.addHeader("Connection", "keep-alive");
 
@@ -110,14 +110,14 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testHTTP10Default() throws Exception {
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
     }
 
     @Test
     public void testHTTP11Default() throws Exception {
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         Assert.assertTrue(reuseStrategy.keepAlive(null, response, context));
     }
@@ -126,7 +126,7 @@ public class TestDefaultConnectionReuseS
     public void testBrokenConnectionDirective1() throws Exception {
         // Use HTTP 1.0
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         response.addHeader("Connection", "keep--alive");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -136,7 +136,7 @@ public class TestDefaultConnectionReuseS
     public void testBrokenConnectionDirective2() throws Exception {
         // Use HTTP 1.0
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         response.addHeader("Connection", null);
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -145,7 +145,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens1() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "yadda, cLOSe, dumdy");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -154,7 +154,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens2() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "yadda, kEEP-alive, dumdy");
         Assert.assertTrue(reuseStrategy.keepAlive(null, response, context));
@@ -163,7 +163,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens3() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "yadda, keep-alive, close, dumdy");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -172,7 +172,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens4() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "yadda, close, dumdy");
         response.addHeader("Proxy-Connection", "keep-alive");
@@ -183,7 +183,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens5() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "yadda, dumdy");
         response.addHeader("Proxy-Connection", "close");
@@ -196,7 +196,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testConnectionTokens6() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "");
         response.addHeader("Proxy-Connection", "close");
@@ -208,7 +208,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testMultipleContentLength() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Content-Length", "10");
         response.addHeader("Content-Length", "11");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
@@ -217,7 +217,7 @@ public class TestDefaultConnectionReuseS
     @Test
     public void testNoContentResponse() throws Exception {
         // Use HTTP 1.1
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT,
"No Content");
+        final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NO_CONTENT, "No
Content");
         Assert.assertTrue(reuseStrategy.keepAlive(null, response, context));
     }
 
@@ -225,16 +225,16 @@ public class TestDefaultConnectionReuseS
     public void testNoContentResponseHttp10() throws Exception {
         // Use HTTP 1.0
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT,
"No Content");
+        final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NO_CONTENT, "No
Content");
         Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
     }
 
     @Test
     public void testRequestExplicitClose() throws Exception {
-        final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
+        final HttpRequest request = new BasicHttpRequest("GET", "/");
         request.addHeader("Connection", "close");
 
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "keep-alive");
         Assert.assertFalse(reuseStrategy.keepAlive(request, response, context));
@@ -242,10 +242,10 @@ public class TestDefaultConnectionReuseS
 
     @Test
     public void testRequestNoExplicitClose() throws Exception {
-        final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
+        final HttpRequest request = new BasicHttpRequest("GET", "/");
         request.addHeader("Connection", "blah, blah, blah");
 
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "keep-alive");
         Assert.assertTrue(reuseStrategy.keepAlive(request, response, context));
@@ -253,16 +253,36 @@ public class TestDefaultConnectionReuseS
 
     @Test
     public void testRequestExplicitCloseMultipleTokens() throws Exception {
-        final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
+        final HttpRequest request = new BasicHttpRequest("GET", "/");
         request.addHeader("Connection", "blah, blah, blah");
         request.addHeader("Connection", "keep-alive");
         request.addHeader("Connection", "close");
 
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
         response.addHeader("Transfer-Encoding", "chunked");
         response.addHeader("Connection", "keep-alive");
         Assert.assertFalse(reuseStrategy.keepAlive(request, response, context));
     }
 
+    @Test
+    public void testRequestClose() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", "/");
+        request.addHeader("Connection", "close");
+
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
+        response.addHeader("Content-Length", "10");
+        response.addHeader("Connection", "keep-alive");
+
+        Assert.assertFalse(reuseStrategy.keepAlive(request, response, context));
+    }
+
+    @Test
+    public void testHeadRequestWithout() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("HEAD", "/");
+        final HttpResponse response = new BasicHttpResponse(200, "OK");
+
+        Assert.assertTrue(reuseStrategy.keepAlive(request, response, context));
+    }
+
 }
 



Mime
View raw message