From d3fee30940127e23ef415cd7db4132eff543bb76 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 13 May 2007 14:46:12 +0000 Subject: [PATCH] * Minor bug fixes * Reviewed HTTP CONNECT logic for compliance with Tunneling TCP based protocols through Web proxy servers git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@537590 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/conn/ssl/SSLSocketFactory.java | 2 +- .../client/DefaultClientRequestDirector.java | 38 ++++++++++++++----- .../impl/conn/DefaultClientConnection.java | 8 ++-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java b/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java index a2fd67a84..e58c35077 100644 --- a/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java +++ b/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java @@ -175,7 +175,7 @@ public class SSLSocketFactory implements SecureSocketFactory { } TrustManager[] trustmanagers = null; if (truststore != null) { - trustmanagers = createTrustManagers(keystore); + trustmanagers = createTrustManagers(truststore); } this.sslcontext = SSLContext.getInstance(algorithm); this.sslcontext.init(keymanagers, trustmanagers, random); diff --git a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java index c91d75862..8356c2203 100644 --- a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java +++ b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java @@ -69,10 +69,13 @@ import org.apache.http.conn.SchemeRegistry; import org.apache.http.message.BasicHttpRequest; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpExecutionContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; +import org.apache.http.util.CharArrayBuffer; /** * Default implementation of a client-side request director. @@ -481,17 +484,34 @@ public class DefaultClientRequestDirector */ protected HttpRequest createConnectRequest(HttpRoute route, HttpContext context) { - // see RFC 2817, section 5.2 - final String authority = - route.getTargetHost().getHostName() + ":" + - route.getTargetHost().getPort(); - - //@@@ do we need a more refined algorithm to choose the HTTP version? - //@@@ use a request factory provided by the caller/creator? + // see RFC 2817, section 5.2 and + // INTERNET-DRAFT: Tunneling TCP based protocols through + // Web proxy servers + + HttpHost target = route.getTargetHost(); + + String host = target.getHostName(); + int port = target.getPort(); + if (port < 0) { + SchemeRegistry schemeREgistry = connManager.getSchemeRegistry(); + Scheme scheme = schemeREgistry.getScheme(target.getSchemeName()); + port = scheme.getDefaultPort(); + } + + CharArrayBuffer buffer = new CharArrayBuffer(host.length() + 6); + buffer.append(host); + buffer.append(":"); + buffer.append(Integer.toString(port)); + + String authority = buffer.toString(); + HttpVersion ver = HttpProtocolParams.getVersion(params); HttpRequest req = new BasicHttpRequest - ("CONNECT", authority, HttpVersion.HTTP_1_1); + ("CONNECT", authority, ver); - req.addHeader("Host", authority); + String agent = HttpProtocolParams.getUserAgent(params); + if (agent != null) { + req.addHeader(HTTP.USER_AGENT, agent); + } //@@@ authenticate here, in caller, or in request interceptor? diff --git a/src/java/org/apache/http/impl/conn/DefaultClientConnection.java b/src/java/org/apache/http/impl/conn/DefaultClientConnection.java index bbde21396..acd7e222c 100644 --- a/src/java/org/apache/http/impl/conn/DefaultClientConnection.java +++ b/src/java/org/apache/http/impl/conn/DefaultClientConnection.java @@ -204,10 +204,10 @@ public class DefaultClientConnection extends SocketHttpClientConnection final HttpParams params) throws HttpException, IOException { HttpResponse response = super.receiveResponseHeader(params); if (HEADERS_LOG.isDebugEnabled()) { - HEADERS_LOG.debug(">> " + response.getStatusLine().toString()); + HEADERS_LOG.debug("<< " + response.getStatusLine().toString()); Header[] headers = response.getAllHeaders(); for (int i = 0; i < headers.length; i++) { - HEADERS_LOG.debug(">> " + headers[i].toString()); + HEADERS_LOG.debug("<< " + headers[i].toString()); } } return response; @@ -217,10 +217,10 @@ public class DefaultClientConnection extends SocketHttpClientConnection public void sendRequestHeader(HttpRequest request) throws HttpException, IOException { super.sendRequestHeader(request); if (HEADERS_LOG.isDebugEnabled()) { - HEADERS_LOG.debug("<< " + request.getRequestLine().toString()); + HEADERS_LOG.debug(">> " + request.getRequestLine().toString()); Header[] headers = request.getAllHeaders(); for (int i = 0; i < headers.length; i++) { - HEADERS_LOG.debug("<< " + headers[i].toString()); + HEADERS_LOG.debug(">> " + headers[i].toString()); } } }