[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpcomponents-commits
Subject:    httpcomponents-core git commit: Workaround for misbehaved servers that return HTTP 204 responses wit
From:       olegk () apache ! org
Date:       2018-01-20 18:18:47
Message-ID: 38597f7d01454c98a7d47a289e7f1b92 () git ! apache ! org
[Download RAW message or body]

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master 60a06cb93 -> ff381e315


Workaround for misbehaved servers that return HTTP 204 responses with a content

Closes #57


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/ff381e31
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/ff381e31
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/ff381e31

Branch: refs/heads/master
Commit: ff381e315a165047e6c2db9b1e749b47cad31405
Parents: 60a06cb
Author: alessandro.gherardi <alessandro.gherardi@schneider-electric.com>
Authored: Fri Jan 19 11:39:27 2018 -0700
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Sat Jan 20 19:09:56 2018 +0100

----------------------------------------------------------------------
 .../impl/DefaultConnectionReuseStrategy.java    | 21 ++++++++++++++++++
 .../TestDefaultConnectionReuseStrategy.java     | 23 ++++++++++++++++++++
 2 files changed, 44 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ff381e31/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
                
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java \
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
 index 27c2fe2..3b27f50 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
                
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java
 @@ -37,6 +37,7 @@ import org.apache.hc.core5.http.HeaderElements;
 import org.apache.hc.core5.http.HttpHeaders;
 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.ProtocolVersion;
 import org.apache.hc.core5.http.message.BasicTokenIterator;
@@ -89,6 +90,26 @@ public class DefaultConnectionReuseStrategy implements \
ConnectionReuseStrategy {  }
         }
 
+        // If a HTTP 204 No Content response contains a Content-length with value > \
0 or Transfer-Encoding header, +        // don't reuse the connection. This is to \
avoid getting out-of-sync if a misbehaved HTTP server +        // returns content as \
part of a HTTP 204 response. +        if (response.getCode() == \
HttpStatus.SC_NO_CONTENT) { +            final Header clh = \
response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); +            if (clh != null) {
+                try {
+                    final int contentLen = Integer.parseInt(clh.getValue());
+                    if (contentLen > 0) {
+                        return false;
+                    }
+                } catch (final NumberFormatException ex) {
+                    // fall through
+                }
+            }
+            if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
+                return false;
+            }
+        }
+
         // Check for a self-terminating entity. If the end of the entity will
         // be indicated by closing the connection, there is no keep-alive.
         final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ff381e31/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
                
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java \
b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
 index 7064044..fcc3bd7 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
                
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestDefaultConnectionReuseStrategy.java
 @@ -284,5 +284,28 @@ public class TestDefaultConnectionReuseStrategy {
         Assert.assertTrue(reuseStrategy.keepAlive(request, response, context));
     }
 
+    @Test
+    public void testHttp204ContentLengthGreaterThanZero() throws Exception {
+        final HttpResponse response = new BasicHttpResponse(204, "OK");
+        response.addHeader("Content-Length", "10");
+        response.addHeader("Connection", "keep-alive");
+        Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
+    }
+
+    @Test
+    public void testHttp204ContentLengthEqualToZero() throws Exception {
+        final HttpResponse response = new BasicHttpResponse(204, "OK");
+        response.addHeader("Content-Length", "0");
+        response.addHeader("Connection", "keep-alive");
+        Assert.assertTrue(reuseStrategy.keepAlive(null, response, context));
+    }
+
+    @Test
+    public void testHttp204ChunkedContent() throws Exception {
+        final HttpResponse response = new BasicHttpResponse(204, "OK");
+        response.addHeader("Transfer-Encoding", "chunked");
+        response.addHeader("Connection", "keep-alive");
+        Assert.assertFalse(reuseStrategy.keepAlive(null, response, context));
+    }
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic