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 2018/01/20 18:17:41 UTC
httpcomponents-core git commit: Workaround for misbehaved servers
that return HTTP 204 responses with a content
Repository: httpcomponents-core
Updated Branches:
refs/heads/4.4.x ca7a02382 -> ed4172be6
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/ed4172be
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/ed4172be
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/ed4172be
Branch: refs/heads/4.4.x
Commit: ed4172be69fe00db26ced66cb8160e40ef84dd75
Parents: ca7a023
Author: alessandro.gherardi <al...@schneider-electric.com>
Authored: Fri Jan 19 11:39:27 2018 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Jan 20 19:09:34 2018 +0100
----------------------------------------------------------------------
.../impl/DefaultConnectionReuseStrategy.java | 22 +++++++++++++++++++
.../TestDefaultConnectionReuseStrategy.java | 23 ++++++++++++++++++++
2 files changed, 45 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ed4172be/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java b/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
index 7a81d45..790f3d1 100644
--- a/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
+++ b/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
@@ -80,6 +80,28 @@ public class DefaultConnectionReuseStrategy implements ConnectionReuseStrategy {
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
+ // If a HTTP 204 No Content response contains a Content-length with value > 0 or Transfer-Encoding,
+ // 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.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
+ final Header clh = response.getFirstHeader(HTTP.CONTENT_LEN);
+ if (clh != null) {
+ try {
+ final int contentLen = Integer.parseInt(clh.getValue());
+ if (contentLen > 0) {
+ return false;
+ }
+ } catch (final NumberFormatException ex) {
+ // fall through
+ }
+ }
+
+ final Header teh = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
+ if (teh != null) {
+ return false;
+ }
+ }
+
final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
if (request != null) {
try {
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/ed4172be/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java
----------------------------------------------------------------------
diff --git a/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java b/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java
index 44f89ce..6243adf 100644
--- a/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java
+++ b/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java
@@ -284,5 +284,28 @@ public class TestDefaultConnectionReuseStrategy {
Assert.assertTrue(reuseStrategy.keepAlive(response, context));
}
+ @Test
+ public void testHttp204ContentLengthGreaterThanZero() throws Exception {
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 204, "OK");
+ response.addHeader("Content-Length", "10");
+ response.addHeader("Connection", "keep-alive");
+ Assert.assertFalse(reuseStrategy.keepAlive(response, context));
+ }
+
+ @Test
+ public void testHttp204ContentLengthEqualToZero() throws Exception {
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 204, "OK");
+ response.addHeader("Content-Length", "0");
+ response.addHeader("Connection", "keep-alive");
+ Assert.assertTrue(reuseStrategy.keepAlive(response, context));
+ }
+
+ @Test
+ public void testHttp204ChunkedContent() throws Exception {
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 204, "OK");
+ response.addHeader("Transfer-Encoding", "chunked");
+ response.addHeader("Connection", "keep-alive");
+ Assert.assertFalse(reuseStrategy.keepAlive(response, context));
+ }
}