From 9a534e7e8d62bafba1a875b3c928b329ace88382 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 11 May 2007 09:30:13 +0000 Subject: [PATCH] Implemented request URI rewriting (depending on whether the request is being routed via a proxy or not) git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@537144 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/http/auth/AuthScheme.java | 2 +- src/java/org/apache/http/auth/AuthScope.java | 2 +- .../auth/UsernamePasswordCredentials.java | 2 +- .../http/impl/client/AbstractHttpClient.java | 2 +- .../client/DefaultClientRequestDirector.java | 58 +++++++++++++++++-- .../http/impl/client/DefaultHttpClient.java | 2 +- 6 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/java/org/apache/http/auth/AuthScheme.java b/src/java/org/apache/http/auth/AuthScheme.java index 5b8d76445..24d7c272a 100644 --- a/src/java/org/apache/http/auth/AuthScheme.java +++ b/src/java/org/apache/http/auth/AuthScheme.java @@ -62,7 +62,7 @@ import org.apache.http.HttpRequest; * challenge-response exchanges *

* - * @author Oleg Kalnichevski + * @author Oleg Kalnichevski * @author Adrian Sutton * * @since 4.0 diff --git a/src/java/org/apache/http/auth/AuthScope.java b/src/java/org/apache/http/auth/AuthScope.java index 58b4564dc..9042cfa4f 100644 --- a/src/java/org/apache/http/auth/AuthScope.java +++ b/src/java/org/apache/http/auth/AuthScope.java @@ -37,7 +37,7 @@ import org.apache.http.util.LangUtils; * a port number, a realm name and an authentication scheme name which * {@link Credentials Credentials} apply to. * - * @author Oleg Kalnichevski + * @author Oleg Kalnichevski * @author Adrian Sutton * * @since 3.0 diff --git a/src/java/org/apache/http/auth/UsernamePasswordCredentials.java b/src/java/org/apache/http/auth/UsernamePasswordCredentials.java index 3ee24339a..9498bce69 100644 --- a/src/java/org/apache/http/auth/UsernamePasswordCredentials.java +++ b/src/java/org/apache/http/auth/UsernamePasswordCredentials.java @@ -38,7 +38,7 @@ import org.apache.http.util.LangUtils; * @author Remy Maucherat * @author Sean C. Sullivan * @author Mike Bowler - * @author Oleg Kalnichevski + * @author Oleg Kalnichevski * * @version $Revision$ $Date$ * diff --git a/src/java/org/apache/http/impl/client/AbstractHttpClient.java b/src/java/org/apache/http/impl/client/AbstractHttpClient.java index c26886506..b4016d8d8 100644 --- a/src/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/src/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -64,7 +64,7 @@ import org.apache.http.protocol.HttpResponseInterceptorList; * Convenience base class for HTTP client implementations. * * @author Roland Weber - * + * @author Oleg Kalnichevski * * * @version $Revision$ diff --git a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java index f32d65e5b..a9a2a7b8f 100644 --- a/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java +++ b/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java @@ -32,6 +32,7 @@ package org.apache.http.impl.client; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import org.apache.commons.logging.Log; @@ -40,6 +41,7 @@ import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpException; +import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; @@ -69,7 +71,7 @@ import org.apache.http.protocol.HttpRequestExecutor; * This class replaces the HttpMethodDirector in HttpClient 3. * * @author Roland Weber - * + * @author Oleg Kalnichevski * * * @version $Revision$ @@ -145,8 +147,8 @@ public class DefaultClientRequestDirector } - private RequestWrapper wrapRequest(final RoutedRequest roureq) throws ProtocolException { - HttpRequest request = roureq.getRequest(); + private RequestWrapper wrapRequest( + final HttpRequest request) throws ProtocolException { try { if (request instanceof HttpEntityEnclosingRequest) { return new EntityEnclosingRequestWrapper( @@ -162,21 +164,65 @@ public class DefaultClientRequestDirector } + private void rewriteRequestURI( + final RequestWrapper request, + final HttpRoute route) throws ProtocolException { + try { + + URI uri = request.getURI(); + if (route.getProxyHost() != null && !route.isTunnelled()) { + // Make sure the request URI is absolute + if (!uri.isAbsolute()) { + HttpHost target = route.getTargetHost(); + uri = new URI( + target.getSchemeName(), + null, + target.getHostName(), + target.getPort(), + uri.getPath(), + uri.getQuery(), + uri.getFragment()); + request.setURI(uri); + } + } else { + // Make sure the request URI is relative + if (uri.isAbsolute()) { + uri = new URI(null, null, null, -1, + uri.getPath(), + uri.getQuery(), + uri.getFragment()); + request.setURI(uri); + } + } + + } catch (URISyntaxException ex) { + throw new ProtocolException("Invalid URI: " + + request.getRequestLine().getUri(), ex); + } + } + + // non-javadoc, see interface ClientRequestDirector public HttpResponse execute(RoutedRequest roureq, HttpContext context) throws HttpException, IOException { - RequestWrapper request = wrapRequest(roureq); + RequestWrapper request = wrapRequest(roureq.getRequest()); HttpResponse response = null; boolean done = false; try { int execCount = 0; while (!done) { + + HttpRoute route = roureq.getRoute(); + + // Re-write request URI if needed + rewriteRequestURI(request, route); + if (managedConn == null) { - managedConn = allocateConnection(roureq.getRoute()); + managedConn = allocateConnection(route); } - establishRoute(roureq.getRoute(), context); + establishRoute(route, context); context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, roureq.getRoute().getTargetHost()); diff --git a/src/java/org/apache/http/impl/client/DefaultHttpClient.java b/src/java/org/apache/http/impl/client/DefaultHttpClient.java index 2cc9c85af..21f80f45e 100644 --- a/src/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/src/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -82,7 +82,7 @@ import org.apache.http.protocol.SyncHttpExecutionContext; * This class replaces HttpClient in HttpClient 3. * * @author Roland Weber - * + * @author Oleg Kalnichevski * * * @version $Revision$