Fixed bug in connection management code. Connection keep-alive strategy now gets correctly applied prior to executing a follow-up request

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@538889 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-05-17 12:36:28 +00:00
parent 5ccb548a59
commit f136fddcca
2 changed files with 38 additions and 10 deletions

View File

@ -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());

View File

@ -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,8 +118,8 @@ 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)
@ -128,6 +130,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 {
HttpDataReceiver receiver = super.createHttpDataReceiver(params);