HTTPCLIENT-1960: URIBuilder incorrect handling of multiple leading slashes in path component

This commit is contained in:
Oleg Kalnichevski 2019-01-12 17:02:13 +01:00
parent 7824d98d0c
commit 123bd993cf
2 changed files with 15 additions and 3 deletions

View File

@ -83,8 +83,20 @@ public class URIUtils {
if (dropFragment) { if (dropFragment) {
uribuilder.setFragment(null); uribuilder.setFragment(null);
} }
if (TextUtils.isEmpty(uribuilder.getPath())) { final String path = uribuilder.getPath();
if (TextUtils.isEmpty(path)) {
uribuilder.setPath("/"); 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(); return uribuilder.build();
} }

View File

@ -51,8 +51,8 @@ public class TestURIUtils {
URI.create("http://thishost/stuff"), null).toString()); URI.create("http://thishost/stuff"), null).toString());
Assert.assertEquals("/", URIUtils.rewriteURI( Assert.assertEquals("/", URIUtils.rewriteURI(
URI.create("http://thishost//"), null).toString()); URI.create("http://thishost//"), null).toString());
Assert.assertEquals("/stuff///morestuff", URIUtils.rewriteURI( Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI(
URI.create("http://thishost//stuff///morestuff"), null).toString()); URI.create("http://thishost//stuff/morestuff"), null).toString());
Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
URI.create("http://thishost/stuff#crap"), target, true).toString()); URI.create("http://thishost/stuff#crap"), target, true).toString());
Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(