HTTPCLIENT-1291: Absolute request URIs without an explicitly specified path are rewritten to have / path

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1430008 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-01-07 21:06:35 +00:00
parent 4232febf63
commit 6ceddc02e0
3 changed files with 25 additions and 4 deletions

View File

@ -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 <olegk at apache.org>
* [HTTPCLIENT-900] Don't enforce URI syntax for messages with an explicit target host.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -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;
}

View File

@ -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