diff --git a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java index d011b0983..1bbf3ee44 100644 --- a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java +++ b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java @@ -277,9 +277,15 @@ public class DefaultClientRequestDirector HttpRoute route = roureq.getRoute(); - if (managedConn == null || !managedConn.isOpen()) { + // Allocate connection if needed + if (managedConn == null) { managedConn = allocateConnection(route); } + // Reopen connection if needed + if (!managedConn.isOpen()) { + managedConn.open(route, context, params); + } + try { establishRoute(route, context); } catch (TunnelRefusedException ex) { @@ -369,10 +375,15 @@ public class DefaultClientRequestDirector if (followup == null) { done = true; } else { - // Make sure the response body is fully consumed, if present - HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); + if (this.reuseStrategy.keepAlive(response, context)) { + LOG.debug("Connection kept alive"); + // Make sure the response body is fully consumed, if present + HttpEntity entity = response.getEntity(); + if (entity != null) { + entity.consumeContent(); + } + } else { + managedConn.close(); } // check if we can use the same connection for the followup if ((managedConn != null) && @@ -381,6 +392,7 @@ public class DefaultClientRequestDirector //@@@ need to consume response body first? //@@@ or let that be done in handleResponse(...)? connManager.releaseConnection(managedConn); + managedConn = null; } roureq = followup; } @@ -799,10 +811,18 @@ public class DefaultClientRequestDirector final AuthState authState, final HttpHost host, final HttpState state) { + + String hostname = host.getHostName(); + int port = host.getPort(); + if (port < 0) { + Scheme scheme = connManager.getSchemeRegistry().getScheme(host); + port = scheme.getDefaultPort(); + } + AuthScheme authScheme = authState.getAuthScheme(); AuthScope authScope = new AuthScope( - host.getHostName(), - host.getPort(), + hostname, + port, authScheme.getRealm(), authScheme.getSchemeName()); diff --git a/src/java/org/apache/http/impl/conn/DefaultClientConnection.java b/src/java/org/apache/http/impl/conn/DefaultClientConnection.java index acd7e222c..14bcd628a 100644 --- a/src/java/org/apache/http/impl/conn/DefaultClientConnection.java +++ b/src/java/org/apache/http/impl/conn/DefaultClientConnection.java @@ -67,6 +67,8 @@ public class DefaultClientConnection extends SocketHttpClientConnection private static final Log HEADERS_LOG = LogFactory.getLog("org.apache.http.conn.headers"); private static final Log WIRE_LOG = LogFactory.getLog("org.apache.http.conn.wire"); + private static final Log LOG = LogFactory.getLog(DefaultClientConnection.class); + /** The unconnected socket between announce and open. */ private volatile Socket announcedSocket; @@ -116,9 +118,9 @@ public class DefaultClientConnection extends SocketHttpClientConnection * * @throws IOException in case of a problem */ - public void shutdown() - throws IOException { - + public void shutdown() throws IOException { + LOG.debug("Connection shut down"); + Socket sock = announcedSocket; // copy volatile attribute if (sock != null) sock.close(); @@ -127,6 +129,12 @@ public class DefaultClientConnection extends SocketHttpClientConnection } // shutdown + + public void close() throws IOException { + LOG.debug("Connection closed"); + super.close(); + } + protected HttpDataReceiver createHttpDataReceiver( final HttpParams params) throws IOException {