From d2b34bce2053d8ac6a403424fa719bc99de3b439 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 12 Aug 2008 12:53:49 +0000 Subject: [PATCH] HTTPCLIENT-790: Protocol interceptors not called when executing CONNECT methods git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@685136 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 6 ++- .../client/protocol/RequestAddCookies.java | 5 ++ .../protocol/RequestDefaultHeaders.java | 6 +++ .../protocol/RequestTargetAuthentication.java | 5 ++ .../http/impl/client/DefaultHttpClient.java | 31 +++++++++++- .../impl/client/DefaultRequestDirector.java | 47 +++++++++---------- 6 files changed, 72 insertions(+), 28 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 72a596e2c..b2ecb8b9b 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,7 +1,11 @@ Changes since 4.0 Alpha 4 ------------------- -* [HTTPCLIENT-668] Do not use static loggers +* [HTTPCLIENT-790] Protocol interceptors are now correctly invoked when + executing CONNECT methods. + Contributed by Oleg Kalnichevski + +* [HTTPCLIENT-668] Do not use static loggers. Contributed by Oleg Kalnichevski * [HTTPCLIENT-781] Respect Keep-Alive header's timeout value. diff --git a/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java b/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java index dffb36f92..85f92c0db 100644 --- a/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java +++ b/module-client/src/main/java/org/apache/http/client/protocol/RequestAddCookies.java @@ -84,6 +84,11 @@ public class RequestAddCookies implements HttpRequestInterceptor { throw new IllegalArgumentException("HTTP context may not be null"); } + String method = request.getRequestLine().getMethod(); + if (method.equalsIgnoreCase("CONNECT")) { + return; + } + // Obtain cookie store CookieStore cookieStore = (CookieStore) context.getAttribute( ClientContext.COOKIE_STORE); diff --git a/module-client/src/main/java/org/apache/http/client/protocol/RequestDefaultHeaders.java b/module-client/src/main/java/org/apache/http/client/protocol/RequestDefaultHeaders.java index b228fc0d0..ef880c0fb 100644 --- a/module-client/src/main/java/org/apache/http/client/protocol/RequestDefaultHeaders.java +++ b/module-client/src/main/java/org/apache/http/client/protocol/RequestDefaultHeaders.java @@ -61,6 +61,12 @@ public class RequestDefaultHeaders implements HttpRequestInterceptor { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } + + String method = request.getRequestLine().getMethod(); + if (method.equalsIgnoreCase("CONNECT")) { + return; + } + // Add default headers Collection defHeaders = (Collection) request.getParams().getParameter( ClientPNames.DEFAULT_HEADERS); diff --git a/module-client/src/main/java/org/apache/http/client/protocol/RequestTargetAuthentication.java b/module-client/src/main/java/org/apache/http/client/protocol/RequestTargetAuthentication.java index ccd5d3bed..03acb1a34 100644 --- a/module-client/src/main/java/org/apache/http/client/protocol/RequestTargetAuthentication.java +++ b/module-client/src/main/java/org/apache/http/client/protocol/RequestTargetAuthentication.java @@ -69,6 +69,11 @@ public class RequestTargetAuthentication implements HttpRequestInterceptor { throw new IllegalArgumentException("HTTP context may not be null"); } + String method = request.getRequestLine().getMethod(); + if (method.equalsIgnoreCase("CONNECT")) { + return; + } + if (request.containsHeader(AUTH.WWW_AUTH_RESP)) { return; } diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index b7ad3ebb0..3df6ccb3c 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -31,7 +31,12 @@ package org.apache.http.impl.client; +import java.io.IOException; + import org.apache.http.ConnectionReuseStrategy; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpVersion; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; @@ -266,9 +271,9 @@ public class DefaultHttpClient extends AbstractHttpClient { httpproc.addInterceptor(new RequestDefaultHeaders()); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); - httpproc.addInterceptor(new RequestTargetHost()); + httpproc.addInterceptor(new IgnoreConnectMethod(new RequestTargetHost())); // Recommended protocol interceptors - httpproc.addInterceptor(new RequestConnControl()); + httpproc.addInterceptor(new IgnoreConnectMethod(new RequestConnControl())); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); // HTTP state management interceptors @@ -329,4 +334,26 @@ public class DefaultHttpClient extends AbstractHttpClient { return new DefaultUserTokenHandler(); } + // FIXME: remove this class when protocol interceptors in HttpCore + // are updated to ignore CONNECT methods + static class IgnoreConnectMethod implements HttpRequestInterceptor { + + private final HttpRequestInterceptor interceptor; + + public IgnoreConnectMethod(final HttpRequestInterceptor interceptor) { + super(); + this.interceptor = interceptor; + } + + public void process( + final HttpRequest request, + final HttpContext context) throws HttpException, IOException { + String method = request.getRequestLine().getMethod(); + if (!method.equalsIgnoreCase("CONNECT")) { + this.interceptor.process(request, context); + } + } + + } + } // class DefaultHttpClient diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index 7816d0d7a..bb80bfa8b 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -88,7 +88,6 @@ import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.ExecutionContext; -import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; @@ -385,13 +384,12 @@ public class DefaultRequestDirector implements RequestDirector { targetAuthState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, proxyAuthState); - - // Run request protocol interceptors - requestExec.preProcess(wrapper, httpProcessor, context); - context.setAttribute(ExecutionContext.HTTP_REQUEST, wrapper); + // Run request protocol interceptors + requestExec.preProcess(wrapper, httpProcessor, context); + boolean retrying = true; while (retrying) { // Increment total exec count (with redirects) @@ -680,29 +678,28 @@ public class DefaultRequestDirector implements RequestDirector { } HttpRequest connect = createConnectRequest(route, context); + connect.setParams(this.params); - String agent = HttpProtocolParams.getUserAgent(params); - if (agent != null) { - connect.addHeader(HTTP.USER_AGENT, agent); - } - connect.addHeader(HTTP.TARGET_HOST, target.toHostString()); + // Populate the execution context + context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, + target); + context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, + proxy); + context.setAttribute(ExecutionContext.HTTP_CONNECTION, + managedConn); + context.setAttribute(ClientContext.TARGET_AUTH_STATE, + targetAuthState); + context.setAttribute(ClientContext.PROXY_AUTH_STATE, + proxyAuthState); + context.setAttribute(ExecutionContext.HTTP_REQUEST, + connect); - AuthScheme authScheme = this.proxyAuthState.getAuthScheme(); - AuthScope authScope = this.proxyAuthState.getAuthScope(); - Credentials creds = this.proxyAuthState.getCredentials(); - if (creds != null) { - if (authScope != null || !authScheme.isConnectionBased()) { - try { - connect.addHeader(authScheme.authenticate(creds, connect)); - } catch (AuthenticationException ex) { - if (this.log.isErrorEnabled()) { - this.log.error("Proxy authentication error: " + ex.getMessage()); - } - } - } - } + this.requestExec.preProcess(connect, this.httpProcessor, context); - response = requestExec.execute(connect, this.managedConn, context); + response = this.requestExec.execute(connect, this.managedConn, context); + + response.setParams(this.params); + this.requestExec.postProcess(response, this.httpProcessor, context); int status = response.getStatusLine().getStatusCode(); if (status < 200) {