diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java index 35c74de0ae..322ee239d7 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java @@ -171,15 +171,6 @@ private static String normalizePath(final String path, final boolean relative) { if (TextUtils.isBlank(s)) { return ""; } - int n = 0; - for (; n < s.length(); n++) { - if (s.charAt(n) != '/') { - break; - } - } - if (n > 1) { - s = s.substring(n - 1); - } if (!relative && !s.startsWith("/")) { s = "/" + s; } 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 7bb01c6e75..b06a952122 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 @@ -147,8 +147,20 @@ public static URI rewriteURI( if (dropFragment) { uribuilder.setFragment(null); } - if (TextUtils.isEmpty(uribuilder.getPath())) { + final String path = uribuilder.getPath(); + if (TextUtils.isEmpty(path)) { uribuilder.setPath("/"); + } else { + final StringBuilder buf = new StringBuilder(path.length()); + boolean foundSlash = false; + for (int i = 0; i < path.length(); i++) { + final char ch = path.charAt(i); + if (ch != '/' || !foundSlash) { + buf.append(ch); + } + foundSlash = ch == '/'; + } + uribuilder.setPath(buf.toString()); } return uribuilder.build(); } diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java index f96178b0ae..d9bdaa3587 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java @@ -36,6 +36,7 @@ import org.apache.http.Consts; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; +import org.hamcrest.CoreMatchers; import org.junit.Assert; import org.junit.Test; @@ -328,4 +329,23 @@ public void testRelativePathWithAuthority() throws Exception { Assert.assertEquals(new URI("http://somehost/./mypath"), uri); } + @Test + public void testMultipleLeadingPathSlashes() throws Exception { + final URI uri = new URIBuilder() + .setScheme("ftp") + .setHost("somehost") + .setPath("//blah//blah") + .build(); + Assert.assertThat(uri, CoreMatchers.equalTo(URI.create("ftp://somehost//blah//blah"))); + } + + @Test + public void testPathNoLeadingSlash() throws Exception { + final URI uri = new URIBuilder() + .setScheme("ftp") + .setPath("blah") + .build(); + Assert.assertThat(uri, CoreMatchers.equalTo(URI.create("ftp:/blah"))); + } + } 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 e33477fce8..035fad49b3 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 @@ -51,8 +51,8 @@ public void testRewrite() throws Exception { URI.create("http://thishost/stuff"), null).toString()); Assert.assertEquals("/", URIUtils.rewriteURI( URI.create("http://thishost//"), null).toString()); - Assert.assertEquals("/stuff///morestuff", URIUtils.rewriteURI( - URI.create("http://thishost//stuff///morestuff"), null).toString()); + Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI( + URI.create("http://thishost//stuff/morestuff"), null).toString()); Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( URI.create("http://thishost/stuff#crap"), target, true).toString()); Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(