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 2013/04/13 12:28:42 UTC

svn commit: r1467583 - in /httpcomponents/httpclient/branches/4.2.x: RELEASE_NOTES.txt httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java

Author: olegk
Date: Sat Apr 13 10:28:42 2013
New Revision: 1467583

URL: http://svn.apache.org/r1467583
Log:
HTTPCLIENT-1340: Connection re-used in a inconsistent state despite presence of 'Connection: close' response header immediately after a successful authentication exchange

Modified:
    httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java

Modified: httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt?rev=1467583&r1=1467582&r2=1467583&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt Sat Apr 13 10:28:42 2013
@@ -1,3 +1,10 @@
+Changes since Release 4.2.4
+-------------------
+
+* [HTTPCLIENT-1340] Connection re-used in a inconsistent state despite presence of 
+  'Connection: close' response header immediately after a successful authentication exchange. 
+
+
 Release 4.2.4
 -------------------
 

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java?rev=1467583&r1=1467582&r2=1467583&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java (original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java Sat Apr 13 10:28:42 2013
@@ -98,6 +98,8 @@ public class BasicManagedEntity extends 
                 // this will not trigger a callback from EofSensorInputStream
                 EntityUtils.consume(wrappedEntity);
                 managedConn.markReusable();
+            } else {
+                managedConn.unmarkReusable();
             }
         } finally {
             releaseManagedConnection();
@@ -135,11 +137,15 @@ public class BasicManagedEntity extends 
 
     public boolean eofDetected(InputStream wrapped) throws IOException {
         try {
-            if (attemptReuse && (managedConn != null)) {
-                // there may be some cleanup required, such as
-                // reading trailers after the response body:
-                wrapped.close();
-                managedConn.markReusable();
+            if (managedConn != null) {
+                if (attemptReuse) {
+                    // there may be some cleanup required, such as
+                    // reading trailers after the response body:
+                    wrapped.close();
+                    managedConn.markReusable();
+                } else {
+                    managedConn.unmarkReusable();
+                }
             }
         } finally {
             releaseManagedConnection();
@@ -149,17 +155,21 @@ public class BasicManagedEntity extends 
 
     public boolean streamClosed(InputStream wrapped) throws IOException {
         try {
-            if (attemptReuse && (managedConn != null)) {
-                boolean valid = managedConn.isOpen();
-                // this assumes that closing the stream will
-                // consume the remainder of the response body:
-                try {
-                    wrapped.close();
-                    managedConn.markReusable();
-                } catch (SocketException ex) {
-                    if (valid) {
-                        throw ex;
+            if (managedConn != null) {
+                if (attemptReuse) {
+                    boolean valid = managedConn.isOpen();
+                    // this assumes that closing the stream will
+                    // consume the remainder of the response body:
+                    try {
+                        wrapped.close();
+                        managedConn.markReusable();
+                    } catch (SocketException ex) {
+                        if (valid) {
+                            throw ex;
+                        }
                     }
+                } else {
+                    managedConn.unmarkReusable();
                 }
             }
         } finally {

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java?rev=1467583&r1=1467582&r2=1467583&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java (original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java Sat Apr 13 10:28:42 2013
@@ -58,6 +58,7 @@ import org.apache.http.localserver.Respo
 import org.apache.http.params.CoreProtocolPNames;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExpectationVerifier;
 import org.apache.http.protocol.HttpRequestHandler;
@@ -557,4 +558,50 @@ public class TestClientAuthentication ex
         EntityUtils.consume(entity);
     }
 
+    static class ClosingAuthHandler implements HttpRequestHandler {
+
+        public void handle(
+                final HttpRequest request,
+                final HttpResponse response,
+                final HttpContext context) throws HttpException, IOException {
+            String creds = (String) context.getAttribute("creds");
+            if (creds == null || !creds.equals("test:test")) {
+                response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+            } else {
+                response.setStatusCode(HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("success", Consts.ASCII);
+                response.setEntity(entity);
+                response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
+            }
+        }
+
+    }
+
+    @Test
+    public void testConnectionCloseAfterAuthenticationSuccess() throws Exception {
+        this.localServer.register("*", new ClosingAuthHandler());
+        this.localServer.start();
+
+        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials(AuthScope.ANY,
+                new UsernamePasswordCredentials("test", "test"));
+
+        TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy();
+
+        this.httpclient.setCredentialsProvider(credsProvider);
+        this.httpclient.setTargetAuthenticationStrategy(authStrategy);
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpHost targethost = getServerHttp();
+
+        for (int i = 0; i < 2; i++) {
+            HttpGet httpget = new HttpGet("/");
+
+            HttpResponse response = this.httpclient.execute(targethost, httpget, context);
+            EntityUtils.consume(response.getEntity());
+            Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        }
+    }
+
 }