You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/10/29 14:57:16 UTC

svn commit: r589630 - in /jakarta/httpcomponents/httpclient/trunk: RELEASE_NOTES.txt module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java

Author: olegk
Date: Mon Oct 29 06:57:15 2007
New Revision: 589630

URL: http://svn.apache.org/viewvc?rev=589630&view=rev
Log:
HTTPCLIENT-698: Resolve non-absolute redirect URIs relative to the request URI

Contributed by Johannes Koch <johannes.koch at fit.fraunhofer.de>
Reviewed by Oleg Kalnichevski


Modified:
    jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java

Modified: jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=589630&r1=589629&r2=589630&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Mon Oct 29 06:57:15 2007
@@ -1,5 +1,9 @@
 Changes since release 4.0 Alpha 1
 
+* [HTTPCLIENT-698] Resolve non-absolute redirect URIs relative to 
+  the request URI
+  Contributed by Johannes Koch <johannes.koch at fit.fraunhofer.de>
+
 * [HTTPCLIENT-697] Throw a more intelligible exception when connection
   to a remote host cannot be established.
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java?rev=589630&r1=589629&r2=589630&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java Mon Oct 29 06:57:15 2007
@@ -40,6 +40,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.Header;
 import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.ProtocolException;
@@ -131,15 +132,21 @@
                 throw new IllegalStateException("Target host not available " +
                         "in the HTTP context");
             }
+            
+            HttpRequest request = (HttpRequest) context.getAttribute(
+                    ExecutionContext.HTTP_REQUEST);
+            
             try {
-                uri = new URI(
+                URI requestURI = new URI(request.getRequestLine().getUri());
+                URI absoluteRequestURI = new URI(
                         target.getSchemeName(),
                         null,
                         target.getHostName(),
                         target.getPort(),
-                        uri.getPath(),
-                        uri.getQuery(),
-                        uri.getFragment());
+                        requestURI.getPath(),
+                        requestURI.getQuery(),
+                        requestURI.getFragment());
+                uri = absoluteRequestURI.resolve(uri); 
             } catch (URISyntaxException ex) {
                 throw new ProtocolException(ex.getMessage(), ex);
             }

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java?rev=589630&r1=589629&r2=589630&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java Mon Oct 29 06:57:15 2007
@@ -158,28 +158,53 @@
 
     private class RelativeRedirectService implements HttpRequestHandler {
         
-            public RelativeRedirectService() {
-                super();
+        public RelativeRedirectService() {
+            super();
+        }
+
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            String uri = request.getRequestLine().getUri();
+            if (uri.equals("/oldlocation/")) {
+                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
+                response.addHeader(new BasicHeader("Location", "/relativelocation/"));
+            } else if (uri.equals("/relativelocation/")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
             }
+        }
+    }
+
+    private class RelativeRedirectService2 implements HttpRequestHandler {
+        
+        public RelativeRedirectService2() {
+            super();
+        }
 
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
-                String uri = request.getRequestLine().getUri();
-                if (uri.equals("/oldlocation/")) {
-                    response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
-                    response.addHeader(new BasicHeader("Location", "/relativelocation/"));
-                } else if (uri.equals("/relativelocation/")) {
-                    response.setStatusLine(ver, HttpStatus.SC_OK);
-                    StringEntity entity = new StringEntity("Successful redirect");
-                    response.setEntity(entity);
-                } else {
-                    response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
-                }
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            String uri = request.getRequestLine().getUri();
+            if (uri.equals("/test/oldlocation")) {
+                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
+                response.addHeader(new BasicHeader("Location", "relativelocation"));
+            } else if (uri.equals("/test/relativelocation")) {
+                response.setStatusLine(ver, HttpStatus.SC_OK);
+                StringEntity entity = new StringEntity("Successful redirect");
+                response.setEntity(entity);
+            } else {
+                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
             }
         }
+    }
 
     private class BogusRedirectService implements HttpRequestHandler {
         private String url;
@@ -486,6 +511,36 @@
 
         assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
         assertEquals("/relativelocation/", reqWrapper.getRequestLine().getUri());
+        assertEquals(host, targetHost.getHostName());
+        assertEquals(port, targetHost.getPort());
+    }
+
+    public void testRelativeRedirect2() throws Exception {
+        int port = this.localServer.getServicePort();
+        String host = "localhost";
+        this.localServer.register("*", new RelativeRedirectService2());
+
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpContext context = client.getDefaultContext();
+
+        client.getParams().setBooleanParameter(
+                ClientPNames.REJECT_RELATIVE_REDIRECT, false);
+        HttpGet httpget = new HttpGet("/test/oldlocation");
+
+        RoutedRequest request = new RoutedRequest.Impl(httpget, getDefaultRoute()); 
+        HttpResponse response = client.execute(request, context);
+        HttpEntity e = response.getEntity();
+        if (e != null) {
+            e.consumeContent();
+        }
+        
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+        HttpHost targetHost = (HttpHost) context.getAttribute(
+                ExecutionContext.HTTP_TARGET_HOST);
+
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        assertEquals("/test/relativelocation", reqWrapper.getRequestLine().getUri());
         assertEquals(host, targetHost.getHostName());
         assertEquals(port, targetHost.getPort());
     }