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
This commit is contained in:
parent
1f7f493c18
commit
d2b34bce20
|
@ -1,7 +1,11 @@
|
||||||
Changes since 4.0 Alpha 4
|
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 <olegk at apache.org>
|
||||||
|
|
||||||
|
* [HTTPCLIENT-668] Do not use static loggers.
|
||||||
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
||||||
|
|
||||||
* [HTTPCLIENT-781] Respect Keep-Alive header's timeout value.
|
* [HTTPCLIENT-781] Respect Keep-Alive header's timeout value.
|
||||||
|
|
|
@ -84,6 +84,11 @@ public class RequestAddCookies implements HttpRequestInterceptor {
|
||||||
throw new IllegalArgumentException("HTTP context may not be null");
|
throw new IllegalArgumentException("HTTP context may not be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String method = request.getRequestLine().getMethod();
|
||||||
|
if (method.equalsIgnoreCase("CONNECT")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Obtain cookie store
|
// Obtain cookie store
|
||||||
CookieStore cookieStore = (CookieStore) context.getAttribute(
|
CookieStore cookieStore = (CookieStore) context.getAttribute(
|
||||||
ClientContext.COOKIE_STORE);
|
ClientContext.COOKIE_STORE);
|
||||||
|
|
|
@ -61,6 +61,12 @@ public class RequestDefaultHeaders implements HttpRequestInterceptor {
|
||||||
if (request == null) {
|
if (request == null) {
|
||||||
throw new IllegalArgumentException("HTTP request may not be null");
|
throw new IllegalArgumentException("HTTP request may not be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String method = request.getRequestLine().getMethod();
|
||||||
|
if (method.equalsIgnoreCase("CONNECT")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Add default headers
|
// Add default headers
|
||||||
Collection<?> defHeaders = (Collection<?>) request.getParams().getParameter(
|
Collection<?> defHeaders = (Collection<?>) request.getParams().getParameter(
|
||||||
ClientPNames.DEFAULT_HEADERS);
|
ClientPNames.DEFAULT_HEADERS);
|
||||||
|
|
|
@ -69,6 +69,11 @@ public class RequestTargetAuthentication implements HttpRequestInterceptor {
|
||||||
throw new IllegalArgumentException("HTTP context may not be null");
|
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)) {
|
if (request.containsHeader(AUTH.WWW_AUTH_RESP)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,12 @@
|
||||||
|
|
||||||
package org.apache.http.impl.client;
|
package org.apache.http.impl.client;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.http.ConnectionReuseStrategy;
|
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.HttpVersion;
|
||||||
import org.apache.http.auth.AuthSchemeRegistry;
|
import org.apache.http.auth.AuthSchemeRegistry;
|
||||||
import org.apache.http.client.AuthenticationHandler;
|
import org.apache.http.client.AuthenticationHandler;
|
||||||
|
@ -266,9 +271,9 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
||||||
httpproc.addInterceptor(new RequestDefaultHeaders());
|
httpproc.addInterceptor(new RequestDefaultHeaders());
|
||||||
// Required protocol interceptors
|
// Required protocol interceptors
|
||||||
httpproc.addInterceptor(new RequestContent());
|
httpproc.addInterceptor(new RequestContent());
|
||||||
httpproc.addInterceptor(new RequestTargetHost());
|
httpproc.addInterceptor(new IgnoreConnectMethod(new RequestTargetHost()));
|
||||||
// Recommended protocol interceptors
|
// Recommended protocol interceptors
|
||||||
httpproc.addInterceptor(new RequestConnControl());
|
httpproc.addInterceptor(new IgnoreConnectMethod(new RequestConnControl()));
|
||||||
httpproc.addInterceptor(new RequestUserAgent());
|
httpproc.addInterceptor(new RequestUserAgent());
|
||||||
httpproc.addInterceptor(new RequestExpectContinue());
|
httpproc.addInterceptor(new RequestExpectContinue());
|
||||||
// HTTP state management interceptors
|
// HTTP state management interceptors
|
||||||
|
@ -329,4 +334,26 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
||||||
return new DefaultUserTokenHandler();
|
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
|
} // class DefaultHttpClient
|
||||||
|
|
|
@ -88,7 +88,6 @@ import org.apache.http.params.HttpConnectionParams;
|
||||||
import org.apache.http.params.HttpParams;
|
import org.apache.http.params.HttpParams;
|
||||||
import org.apache.http.params.HttpProtocolParams;
|
import org.apache.http.params.HttpProtocolParams;
|
||||||
import org.apache.http.protocol.ExecutionContext;
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
import org.apache.http.protocol.HTTP;
|
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
import org.apache.http.protocol.HttpProcessor;
|
import org.apache.http.protocol.HttpProcessor;
|
||||||
import org.apache.http.protocol.HttpRequestExecutor;
|
import org.apache.http.protocol.HttpRequestExecutor;
|
||||||
|
@ -385,13 +384,12 @@ public class DefaultRequestDirector implements RequestDirector {
|
||||||
targetAuthState);
|
targetAuthState);
|
||||||
context.setAttribute(ClientContext.PROXY_AUTH_STATE,
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE,
|
||||||
proxyAuthState);
|
proxyAuthState);
|
||||||
|
|
||||||
// Run request protocol interceptors
|
|
||||||
requestExec.preProcess(wrapper, httpProcessor, context);
|
|
||||||
|
|
||||||
context.setAttribute(ExecutionContext.HTTP_REQUEST,
|
context.setAttribute(ExecutionContext.HTTP_REQUEST,
|
||||||
wrapper);
|
wrapper);
|
||||||
|
|
||||||
|
// Run request protocol interceptors
|
||||||
|
requestExec.preProcess(wrapper, httpProcessor, context);
|
||||||
|
|
||||||
boolean retrying = true;
|
boolean retrying = true;
|
||||||
while (retrying) {
|
while (retrying) {
|
||||||
// Increment total exec count (with redirects)
|
// Increment total exec count (with redirects)
|
||||||
|
@ -680,29 +678,28 @@ public class DefaultRequestDirector implements RequestDirector {
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRequest connect = createConnectRequest(route, context);
|
HttpRequest connect = createConnectRequest(route, context);
|
||||||
|
connect.setParams(this.params);
|
||||||
|
|
||||||
String agent = HttpProtocolParams.getUserAgent(params);
|
// Populate the execution context
|
||||||
if (agent != null) {
|
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST,
|
||||||
connect.addHeader(HTTP.USER_AGENT, agent);
|
target);
|
||||||
}
|
context.setAttribute(ExecutionContext.HTTP_PROXY_HOST,
|
||||||
connect.addHeader(HTTP.TARGET_HOST, target.toHostString());
|
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();
|
this.requestExec.preProcess(connect, this.httpProcessor, context);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
int status = response.getStatusLine().getStatusCode();
|
||||||
if (status < 200) {
|
if (status < 200) {
|
||||||
|
|
Loading…
Reference in New Issue