diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 4480f8a8e..dd00f7f34 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,10 @@ Changes in trunk ------------------- +* [HTTPCLIENT-1291] Absolute request URIs without an explicitly specified path are rewritten + to have "/" path). + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-900] Don't enforce URI syntax for messages with an explicit target host. Contributed by Oleg Kalnichevski 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 02e5a8073..aff5c87ab 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 @@ -33,6 +33,7 @@ import java.util.Stack; import org.apache.http.HttpHost; import org.apache.http.annotation.Immutable; import org.apache.http.util.Args; +import org.apache.http.util.TextUtils; /** * A collection of utilities for {@link URI URIs}, to workaround @@ -112,7 +113,7 @@ public class URIUtils { * A convenience method for creating a new {@link URI} whose scheme, host * and port are taken from the target host, but whose path, query and * fragment are taken from the existing URI. The fragment is only used if - * dropFragment is false. + * dropFragment is false. The path is set to "/" if not explicitly specified. * * @param uri * Contains the path, query and fragment to use. @@ -142,6 +143,9 @@ public class URIUtils { if (dropFragment) { uribuilder.setFragment(null); } + if (TextUtils.isEmpty(uribuilder.getPath())) { + uribuilder.setPath("/"); + } return uribuilder.build(); } @@ -159,7 +163,8 @@ public class URIUtils { /** * A convenience method that creates a new {@link URI} whose scheme, host, port, path, * query are taken from the existing URI, dropping any fragment or user-information. - * The existing URI is returned unmodified if it has no fragment or user-information. + * The path is set to "/" if not explicitly specified. The existing URI is returned + * unmodified if it has no fragment or user-information and has a path. * * @param uri * original URI. @@ -168,8 +173,14 @@ public class URIUtils { */ public static URI rewriteURI(final URI uri) throws URISyntaxException { Args.notNull(uri, "URI"); - if (uri.getFragment() != null || uri.getUserInfo() != null) { - return new URIBuilder(uri).setFragment(null).setUserInfo(null).build(); + if (uri.getFragment() != null || uri.getUserInfo() != null + || TextUtils.isEmpty(uri.getPath())) { + URIBuilder uribuilder = new URIBuilder(uri); + uribuilder.setFragment(null).setUserInfo(null); + if (TextUtils.isEmpty(uribuilder.getPath())) { + uribuilder.setPath("/"); + } + return uribuilder.build(); } else { return uri; } 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 a87f05d28..0b5e169b8 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 @@ -55,6 +55,10 @@ public class TestURIUtils { URI.create("http://thishost/stuff#crap"), target, true).toString()); Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI( URI.create("http://thishost/stuff#crap"), target, false).toString()); + Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( + URI.create("http://thishost#crap"), target, true).toString()); + Assert.assertEquals("http://thathost/#crap", URIUtils.rewriteURI( + URI.create("http://thishost#crap"), target, false).toString()); Assert.assertEquals("/stuff/", URIUtils.rewriteURI( URI.create("http://thishost//////////////stuff/"), null).toString()); Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( @@ -63,6 +67,8 @@ public class TestURIUtils { URI.create("http://thathost/stuff#fragment")).toString()); Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI( URI.create("http://userinfo@thathost/stuff#fragment")).toString()); + Assert.assertEquals("http://thathost/", URIUtils.rewriteURI( + URI.create("http://thathost")).toString()); } @Test