From 9a534e7e8d62bafba1a875b3c928b329ace88382 Mon Sep 17 00:00:00 2001
From: Oleg Kalnichevski
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$