From da16fdc9736b4ffe14d28fe30656b7f6d68f2652 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 22 Jun 2012 11:05:10 +0000 Subject: [PATCH] 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 --- RELEASE_NOTES.txt | 4 ++++ .../http/impl/client/DefaultRedirectStrategy.java | 2 +- .../impl/client/TestDefaultRedirectStrategy.java | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 525668188..ac294a08c 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,5 +1,9 @@ Changes since 4.2 ------------------- + +* [HTTPCLIENT-1209] Redirect URIs are now normalized. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-1202] ResponseCachingPolicy should honor explicit cache-control directives for other status codes Contributed by Jon Moore diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java index f837d1d32..bdec33fc8 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java @@ -184,7 +184,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy { */ protected URI createLocationURI(final String location) throws ProtocolException { try { - return new URI(location); + return new URI(location).normalize(); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java index 79ce60473..63da9be55 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java @@ -232,6 +232,19 @@ public class TestDefaultRedirectStrategy { 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) public void testGetLocationUriRelativeLocationNotAllowed() throws Exception { DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();