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
This commit is contained in:
Oleg Kalnichevski 2013-05-17 11:01:54 +00:00
parent 3665ee0d37
commit 29ffce391c
6 changed files with 41 additions and 14 deletions

View File

@ -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 <olegk at apache.org>
* [HTTPCLIENT-1344] Userinfo credentials in URI should not default to preemptive BASIC
authentication.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

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

View File

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

View File

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

View File

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

View File

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