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:38:24 UTC

svn commit: r1467586 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/conn/BasicManagedEntity.java test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java

Author: olegk
Date: Sat Apr 13 10:38:24 2013
New Revision: 1467586

URL: http://svn.apache.org/r1467586
Log:
Backported fix and test case for HTTPCLIENT-1340

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java?rev=1467586&r1=1467585&r2=1467586&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/BasicManagedEntity.java Sat Apr 13 10:38:24 2013
@@ -97,6 +97,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(final 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(final InputStream wrapped) throws IOException {
         try {
-            if (attemptReuse && (managedConn != null)) {
-                final boolean valid = managedConn.isOpen();
-                // this assumes that closing the stream will
-                // consume the remainder of the response body:
-                try {
-                    wrapped.close();
-                    managedConn.markReusable();
-                } catch (final SocketException ex) {
-                    if (valid) {
-                        throw ex;
+            if (managedConn != null) {
+                if (attemptReuse) {
+                    final boolean valid = managedConn.isOpen();
+                    // this assumes that closing the stream will
+                    // consume the remainder of the response body:
+                    try {
+                        wrapped.close();
+                        managedConn.markReusable();
+                    } catch (final SocketException ex) {
+                        if (valid) {
+                            throw ex;
+                        }
                     }
+                } else {
+                    managedConn.unmarkReusable();
                 }
             }
         } finally {

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java?rev=1467586&r1=1467585&r2=1467586&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java Sat Apr 13 10:38:24 2013
@@ -60,6 +60,7 @@ import org.apache.http.localserver.Basic
 import org.apache.http.localserver.LocalTestServer;
 import org.apache.http.localserver.RequestBasicAuth;
 import org.apache.http.localserver.ResponseBasicUnauthorized;
+import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExpectationVerifier;
 import org.apache.http.protocol.HttpProcessor;
@@ -547,4 +548,48 @@ 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 {
+            final String creds = (String) context.getAttribute("creds");
+            if (creds == null || !creds.equals("test:test")) {
+                response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+            } else {
+                response.setStatusCode(HttpStatus.SC_OK);
+                final 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());
+
+        final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials(AuthScope.ANY,
+                new UsernamePasswordCredentials("test", "test"));
+
+        this.httpclient = HttpClients.custom()
+                .setDefaultCredentialsProvider(credsProvider)
+                .build();
+
+        final HttpClientContext context = HttpClientContext.create();
+
+        final HttpHost targethost = getServerHttp();
+
+        for (int i = 0; i < 2; i++) {
+            final HttpGet httpget = new HttpGet("/");
+
+            final HttpResponse response = this.httpclient.execute(targethost, httpget, context);
+            EntityUtils.consume(response.getEntity());
+            Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        }
+    }
+
 }