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 2008/01/24 17:53:25 UTC

svn commit: r614925 - in /httpcomponents/httpclient/trunk: ./ module-client/src/main/java/org/apache/http/client/utils/ module-client/src/main/java/org/apache/http/impl/client/ module-client/src/test/java/org/apache/http/client/protocol/

Author: olegk
Date: Thu Jan 24 08:53:22 2008
New Revision: 614925

URL: http://svn.apache.org/viewvc?rev=614925&view=rev
Log:
HTTPCLIENT-730: Fixed rewriting of URIs containing escaped characters

Contributed by Sam Berlin <sberlin at gmail.com> and Oleg Kalnichevski 

Added:
    httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java   (with props)
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=614925&r1=614924&r2=614925&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Thu Jan 24 08:53:22 2008
@@ -1,6 +1,10 @@
 Changes since 4.0 Alpha 2
 -------------------
 
+* [HTTPCLIENT-730] Fixed rewriting of URIs containing escaped characters
+  Contributed by Sam Berlin <sberlin at gmail.com> and 
+  Oleg Kalnichevski <olegk at apache.org>
+ 
 * [HTTPCLIENT-667] Added 'Meta' cookie specification that selects a cookie policy 
   depending on the format of the cookie(s). It is no longer necessary to know 
   beforehand what kind of HTTP cookie support the target host provides. 

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java?rev=614925&r1=614924&r2=614925&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java Thu Jan 24 08:53:22 2008
@@ -30,8 +30,11 @@
 package org.apache.http.client.utils;
 
 import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
 
 import org.apache.commons.codec.net.URLCodec;
+import org.apache.http.HttpHost;
 import org.apache.http.NameValuePair;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -121,6 +124,75 @@
             }
         }
         return buf.toString();
+    }
+    
+    public static URI createURI(
+            final String scheme,
+            final String host,
+            int port,
+            final String path,
+            final String query,
+            final String fragment) throws URISyntaxException {
+        
+        StringBuilder buffer = new StringBuilder();
+        if (host != null) {
+            if (scheme != null) {
+                buffer.append(scheme);
+                buffer.append("://");
+            }
+            buffer.append(host);
+            if (port > 0) {
+                buffer.append(":");
+                buffer.append(port);
+            }
+        }
+        if (path == null || !path.startsWith("/")) {
+            buffer.append("/");
+        }
+        if (path != null) {
+            buffer.append(path);
+        }
+        if (query != null) {
+            buffer.append("?");
+            buffer.append(query);
+        }
+        if (fragment != null) {
+            buffer.append("#");
+            buffer.append(fragment);
+        }
+        return new URI(buffer.toString());
+    }
+
+    public static URI rewriteURI(
+            final URI uri, 
+            final HttpHost target,
+            boolean dropFragment) throws URISyntaxException {
+        if (uri == null) {
+            throw new IllegalArgumentException("URI may nor be null");
+        }
+        if (target != null) {
+            return URLUtils.createURI(
+                    target.getSchemeName(), 
+                    target.getHostName(), 
+                    target.getPort(), 
+                    uri.getRawPath(), 
+                    uri.getRawQuery(), 
+                    dropFragment ? null : uri.getRawFragment());
+        } else {
+            return URLUtils.createURI(
+                    null, 
+                    null, 
+                    -1, 
+                    uri.getRawPath(), 
+                    uri.getRawQuery(), 
+                    dropFragment ? null : uri.getRawFragment());
+        }
+    }
+    
+    public static URI rewriteURI(
+            final URI uri, 
+            final HttpHost target) throws URISyntaxException {
+        return rewriteURI(uri, target, false);
     }
     
     /**

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?rev=614925&r1=614924&r2=614925&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java Thu Jan 24 08:53:22 2008
@@ -66,6 +66,7 @@
 import org.apache.http.client.params.ClientPNames;
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.client.protocol.ClientContext;
+import org.apache.http.client.utils.URLUtils;
 import org.apache.http.conn.BasicManagedEntity;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ConnectionPoolTimeoutException;
@@ -246,23 +247,13 @@
                 // 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());
+                    uri = URLUtils.rewriteURI(uri, target);
                     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());
+                    uri = URLUtils.rewriteURI(uri, null);
                     request.setURI(uri);
                 }
             }

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java?rev=614925&r1=614924&r2=614925&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java Thu Jan 24 08:53:22 2008
@@ -45,6 +45,7 @@
 import org.apache.http.client.CircularRedirectException;
 import org.apache.http.client.RedirectHandler;
 import org.apache.http.client.params.ClientPNames;
+import org.apache.http.client.utils.URLUtils;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.ExecutionContext;
@@ -136,14 +137,7 @@
             
             try {
                 URI requestURI = new URI(request.getRequestLine().getUri());
-                URI absoluteRequestURI = new URI(
-                        target.getSchemeName(),
-                        null,
-                        target.getHostName(),
-                        target.getPort(),
-                        requestURI.getPath(),
-                        requestURI.getQuery(),
-                        null);
+                URI absoluteRequestURI = URLUtils.rewriteURI(requestURI, target, true);
                 uri = absoluteRequestURI.resolve(uri); 
             } catch (URISyntaxException ex) {
                 throw new ProtocolException(ex.getMessage(), ex);

Added: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java?rev=614925&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java (added)
+++ httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java Thu Jan 24 08:53:22 2008
@@ -0,0 +1,157 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ * ====================================================================
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.client.protocol;
+
+import java.io.IOException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.ProtocolVersion;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+
+public class TestUriEscapes extends ServerTestBase {
+	
+    public TestUriEscapes(final String testName) throws IOException {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestUriEscapes.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestUriEscapes.class);
+    }
+    
+    private class UriListeningService implements HttpRequestHandler {
+        
+        private volatile String requestedUri;
+
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+            this.requestedUri = request.getRequestLine().getUri();
+            response.setStatusLine(ver, HttpStatus.SC_OK);
+            StringEntity entity = new StringEntity("Response Body");
+            response.setEntity(entity);
+        }
+        
+        public String getRequestedUri() {
+        	return requestedUri;
+        }
+    }
+    
+    private void doTest(String uri, boolean relative) throws Exception {
+    	int port = this.localServer.getServicePort();
+		String host = "localhost";
+	    UriListeningService listener = new UriListeningService();
+	    this.localServer.register("*", listener);
+	    
+        DefaultHttpClient client = new DefaultHttpClient(); 
+        HttpResponse response;
+
+        if(!relative) {
+		    String request = "http://" + host + ":" + port + uri;
+		    HttpGet httpget = new HttpGet(request); 
+		    response = client.execute(httpget);
+	        response.getEntity().consumeContent();  
+	    } else {
+	    	HttpHost target = new HttpHost(host, port);
+		    HttpGet httpget = new HttpGet(uri); 
+		    response = client.execute(target, httpget);
+	        response.getEntity().consumeContent();  
+	    }
+        
+        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());       
+        assertEquals(uri, listener.getRequestedUri());
+    }
+	
+    public void testEscapedAmpersandInQueryAbsolute() throws Exception {
+    	doTest("/path/a=b&c=%26d", false);
+    }
+    
+    public void testEscapedAmpersandInQueryRelative() throws Exception {
+    	doTest("/path/a=b&c=%26d", true);
+    }
+    
+    public void testPlusInPathAbsolute() throws Exception {
+    	doTest("/path+go", false);
+	}
+    
+    public void testPlusInPathRelative() throws Exception {
+    	doTest("/path+go", true);
+	}
+    
+    public void testEscapedSpaceInPathAbsolute() throws Exception {
+    	doTest("/path%20go?a=b&c=d", false);
+	}
+    
+    public void testEscapedSpaceInPathRelative() throws Exception {
+    	doTest("/path%20go?a=b&c=d", true);
+	}
+    
+    public void testEscapedAmpersandInPathAbsolute() throws Exception {
+    	doTest("/this%26that?a=b&c=d", false);
+	}
+    
+    public void testEscapedAmpersandInPathRelative() throws Exception {
+    	doTest("/this%26that?a=b&c=d", true);
+	}
+    
+    public void testEscapedSpaceInQueryAbsolute() throws Exception {
+    	doTest("/path?a=b&c=d%20e", false);
+    }
+    
+    public void testEscapedSpaceInQueryRelative() throws Exception {
+    	doTest("/path?a=b&c=d%20e", true);
+    }
+    
+    public void testPlusInQueryAbsolute() throws Exception {
+    	doTest("/path?a=b&c=d+e", false);
+    }
+    
+    public void testPlusInQueryRelative() throws Exception {
+    	doTest("/path?a=b&c=d+e", true);
+    }
+}

Propchange: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain