HTTPCLIENT-1209: Redirect URIs are now normalized

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1352845 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-06-22 11:05:10 +00:00
parent 665fccdac6
commit da16fdc973
3 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,9 @@
Changes since 4.2 Changes since 4.2
------------------- -------------------
* [HTTPCLIENT-1209] Redirect URIs are now normalized.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-1202] ResponseCachingPolicy should honor explicit cache-control * [HTTPCLIENT-1202] ResponseCachingPolicy should honor explicit cache-control
directives for other status codes directives for other status codes
Contributed by Jon Moore <jonm at apache.org> Contributed by Jon Moore <jonm at apache.org>

View File

@ -184,7 +184,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
*/ */
protected URI createLocationURI(final String location) throws ProtocolException { protected URI createLocationURI(final String location) throws ProtocolException {
try { try {
return new URI(location); return new URI(location).normalize();
} catch (URISyntaxException ex) { } catch (URISyntaxException ex) {
throw new ProtocolException("Invalid redirect URI: " + location, ex); throw new ProtocolException("Invalid redirect URI: " + location, ex);
} }

View File

@ -232,6 +232,19 @@ public class TestDefaultRedirectStrategy {
Assert.assertEquals(URI.create("http://localhost/stuff"), uri); Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
} }
@Test
public void testGetLocationUriNormalized() throws Exception {
DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "http://localhost/././stuff/../morestuff");
URI uri = redirectStrategy.getLocationURI(httpget, response, context);
Assert.assertEquals(URI.create("http://localhost/morestuff"), uri);
}
@Test(expected=ProtocolException.class) @Test(expected=ProtocolException.class)
public void testGetLocationUriRelativeLocationNotAllowed() throws Exception { public void testGetLocationUriRelativeLocationNotAllowed() throws Exception {
DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();