From 29ffce391c9426e208bab512de0dae24948c479e Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 17 May 2013 11:01:54 +0000 Subject: [PATCH] HTTPCLIENT-1351: Preserve last request URI fragment in the execution context git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1483736 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 3 +++ .../http/client/protocol/HttpClientContext.java | 14 ++++++++++---- .../org/apache/http/client/utils/URIBuilder.java | 14 ++++++++++++++ .../http/impl/client/DefaultRedirectStrategy.java | 8 +++----- .../apache/http/impl/execchain/ProtocolExec.java | 12 +++++++++--- .../impl/client/TestDefaultRedirectStrategy.java | 4 ++-- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 7c74217bc..937653540 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,9 @@ Changes since release 4.3 BETA1 ------------------- +* [HTTPCLIENT-1351] Preserve last request URI fragment in the execution context. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-1344] Userinfo credentials in URI should not default to preemptive BASIC authentication. Contributed by Oleg Kalnichevski diff --git a/httpclient/src/main/java/org/apache/http/client/protocol/HttpClientContext.java b/httpclient/src/main/java/org/apache/http/client/protocol/HttpClientContext.java index 05535cd3d..cb2a967de 100644 --- a/httpclient/src/main/java/org/apache/http/client/protocol/HttpClientContext.java +++ b/httpclient/src/main/java/org/apache/http/client/protocol/HttpClientContext.java @@ -54,6 +54,12 @@ import org.apache.http.protocol.HttpCoreContext; @NotThreadSafe public class HttpClientContext extends HttpCoreContext { + /** + * Attribute name of a {@link String} object that represents + * request URI fragment of the actual request. + */ + public static final String URI_FRAGMENT = "http.uri-fragment"; + /** * Attribute name of a {@link org.apache.http.conn.routing.RouteInfo} * object that represents the actual connection route. @@ -123,16 +129,12 @@ public class HttpClientContext extends HttpCoreContext { /** * Attribute name of a {@link org.apache.http.config.Lookup} object that represents * the actual {@link ConnectionSocketFactory} registry. - * - * @since 4.3 */ public static final String SOCKET_FACTORY_REGISTRY = "http.socket-factory-registry"; /** * Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that * represents the actual request configuration. - * - * @since 4.3 */ public static final String REQUEST_CONFIG = "http.request-config"; @@ -156,6 +158,10 @@ public class HttpClientContext extends HttpCoreContext { super(); } + public String getUriFragemnt() { + return getAttribute(URI_FRAGMENT, String.class); + } + public RouteInfo getHttpRoute() { return getAttribute(HTTP_ROUTE, HttpRoute.class); } diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java index 8c943e8e4..7982ad692 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java @@ -419,6 +419,20 @@ public class URIBuilder { return this; } + /** + * @since 4.3 + */ + public boolean isAbsolute() { + return this.scheme != null; + } + + /** + * @since 4.3 + */ + public boolean isOpaque() { + return this.path == null; + } + public String getScheme() { return this.scheme; } 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 fe46a2657..b6ab2a4f4 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 @@ -51,7 +51,6 @@ import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIUtils; import org.apache.http.protocol.HttpContext; -import org.apache.http.protocol.HttpCoreContext; import org.apache.http.util.Args; import org.apache.http.util.Asserts; import org.apache.http.util.TextUtils; @@ -150,17 +149,17 @@ public class DefaultRedirectStrategy implements RedirectStrategy { + uri + "' not allowed"); } // Adjust location URI - final HttpHost target = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); + final HttpHost target = clientContext.getTargetHost(); Asserts.notNull(target, "Target host"); final URI requestURI = new URI(request.getRequestLine().getUri()); - final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); + final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false); uri = URIUtils.resolve(absoluteRequestURI, uri); } } catch (final URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } - RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute( + RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute( REDIRECT_LOCATIONS); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); @@ -189,7 +188,6 @@ public class DefaultRedirectStrategy implements RedirectStrategy { if (TextUtils.isEmpty(path)) { b.setPath("/"); } - b.setFragment(null); return b.build(); } catch (final URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); diff --git a/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java b/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java index 9b2015274..1a113c4ab 100644 --- a/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java +++ b/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java @@ -72,11 +72,17 @@ public class ProtocolExec implements ClientExecChain { this.httpProcessor = httpProcessor; } - private void rewriteRequestURI(final HttpRequestWrapper request, final HttpRoute route) - throws ProtocolException { + private void rewriteRequestURI( + final HttpRequestWrapper request, + final HttpRoute route, + final HttpClientContext context) throws ProtocolException { try { URI uri = request.getURI(); if (uri != null) { + final String fragment = uri.getFragment(); + if (fragment != null) { + context.setAttribute(HttpClientContext.URI_FRAGMENT, fragment); + } if (route.getProxyHost() != null && !route.isTunnelled()) { // Make sure the request URI is absolute if (!uri.isAbsolute()) { @@ -126,7 +132,7 @@ public class ProtocolExec implements ClientExecChain { request.setURI(uri); // Re-write request URI if needed - rewriteRequestURI(request, route); + rewriteRequestURI(request, route, context); final HttpParams params = request.getParams(); HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST); 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 806cecb04..71dea454d 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 @@ -222,7 +222,7 @@ public class TestDefaultRedirectStrategy { HttpStatus.SC_MOVED_TEMPORARILY, "Redirect"); response.addHeader("Location", "/stuff#fragment"); final URI uri = redirectStrategy.getLocationURI(httpget, response, context); - Assert.assertEquals(URI.create("http://localhost/stuff"), uri); + Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri); } @Test @@ -235,7 +235,7 @@ public class TestDefaultRedirectStrategy { HttpStatus.SC_MOVED_TEMPORARILY, "Redirect"); response.addHeader("Location", "http://localhost/stuff#fragment"); final URI uri = redirectStrategy.getLocationURI(httpget, response, context); - Assert.assertEquals(URI.create("http://localhost/stuff"), uri); + Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri); } @Test