From ba748bf20c5061d005bbb1044d8e908164fa95c8 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 3 Apr 2010 18:16:40 +0000 Subject: [PATCH] HTTPCLIENT-929: Request with two forward slashes for path fails git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@930558 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/http/client/utils/URIUtils.java | 25 +++++++++++-- .../http/client/utils/TestURIUtils.java | 35 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java index dc56efd1e..978986d5f 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java @@ -134,7 +134,7 @@ public static URI rewriteURI( target.getSchemeName(), target.getHostName(), target.getPort(), - uri.getRawPath(), + normalizePath(uri.getRawPath()), uri.getRawQuery(), dropFragment ? null : uri.getRawFragment()); } else { @@ -142,12 +142,33 @@ public static URI rewriteURI( null, null, -1, - uri.getRawPath(), + normalizePath(uri.getRawPath()), uri.getRawQuery(), dropFragment ? null : uri.getRawFragment()); } } + private static String normalizePath(final String path) { + if (path == null) { + return null; + } + StringBuilder buffer = new StringBuilder(path.length()); + boolean gotslash = false; + for (int i = 0; i < path.length(); i++) { + char ch = path.charAt(i); + if (ch == '/') { + if (!gotslash) { + buffer.append(ch); + gotslash = true; + } + } else { + buffer.append(ch); + gotslash = false; + } + } + return buffer.toString(); + } + /** * A convenience method for * {@link URIUtils#rewriteURI(URI, HttpHost, boolean)} that always keeps the diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java index 50425ea1e..684ab9d3e 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java @@ -27,6 +27,8 @@ import java.net.URI; +import org.apache.http.HttpHost; + import junit.framework.Assert; import junit.framework.Test; import junit.framework.TestCase; @@ -53,6 +55,39 @@ public static Test suite() { return new TestSuite(TestURIUtils.class); } + public void testRewite00() throws Exception { + URI uri = URI.create("http://thishost/stuff"); + HttpHost target = new HttpHost("thathost", -1); + Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(uri, target).toString()); + } + + public void testRewite01() throws Exception { + URI uri = URI.create("http://thishost/stuff"); + Assert.assertEquals("/stuff", URIUtils.rewriteURI(uri, null).toString()); + } + + public void testRewite02() throws Exception { + URI uri = URI.create("http://thishost//"); + Assert.assertEquals("/", URIUtils.rewriteURI(uri, null).toString()); + } + + public void testRewite03() throws Exception { + URI uri = URI.create("http://thishost//stuff///morestuff"); + Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI(uri, null).toString()); + } + + public void testRewite04() throws Exception { + URI uri = URI.create("http://thishost/stuff#crap"); + HttpHost target = new HttpHost("thathost", -1); + Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(uri, target, true).toString()); + } + + public void testRewite05() throws Exception { + URI uri = URI.create("http://thishost/stuff#crap"); + HttpHost target = new HttpHost("thathost", -1); + Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(uri, target, false).toString()); + } + public void testResolve00() { Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString()); }