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:
parent
3665ee0d37
commit
29ffce391c
|
@ -1,6 +1,9 @@
|
||||||
Changes since release 4.3 BETA1
|
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
|
* [HTTPCLIENT-1344] Userinfo credentials in URI should not default to preemptive BASIC
|
||||||
authentication.
|
authentication.
|
||||||
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
||||||
|
|
|
@ -54,6 +54,12 @@ import org.apache.http.protocol.HttpCoreContext;
|
||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
public class HttpClientContext extends HttpCoreContext {
|
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}
|
* Attribute name of a {@link org.apache.http.conn.routing.RouteInfo}
|
||||||
* object that represents the actual connection route.
|
* 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
|
* Attribute name of a {@link org.apache.http.config.Lookup} object that represents
|
||||||
* the actual {@link ConnectionSocketFactory} registry.
|
* the actual {@link ConnectionSocketFactory} registry.
|
||||||
*
|
|
||||||
* @since 4.3
|
|
||||||
*/
|
*/
|
||||||
public static final String SOCKET_FACTORY_REGISTRY = "http.socket-factory-registry";
|
public static final String SOCKET_FACTORY_REGISTRY = "http.socket-factory-registry";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that
|
* Attribute name of a {@link org.apache.http.client.config.RequestConfig} object that
|
||||||
* represents the actual request configuration.
|
* represents the actual request configuration.
|
||||||
*
|
|
||||||
* @since 4.3
|
|
||||||
*/
|
*/
|
||||||
public static final String REQUEST_CONFIG = "http.request-config";
|
public static final String REQUEST_CONFIG = "http.request-config";
|
||||||
|
|
||||||
|
@ -156,6 +158,10 @@ public class HttpClientContext extends HttpCoreContext {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUriFragemnt() {
|
||||||
|
return getAttribute(URI_FRAGMENT, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
public RouteInfo getHttpRoute() {
|
public RouteInfo getHttpRoute() {
|
||||||
return getAttribute(HTTP_ROUTE, HttpRoute.class);
|
return getAttribute(HTTP_ROUTE, HttpRoute.class);
|
||||||
}
|
}
|
||||||
|
|
|
@ -419,6 +419,20 @@ public class URIBuilder {
|
||||||
return this;
|
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() {
|
public String getScheme() {
|
||||||
return this.scheme;
|
return this.scheme;
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,6 @@ import org.apache.http.client.protocol.HttpClientContext;
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.client.utils.URIUtils;
|
import org.apache.http.client.utils.URIUtils;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
import org.apache.http.protocol.HttpCoreContext;
|
|
||||||
import org.apache.http.util.Args;
|
import org.apache.http.util.Args;
|
||||||
import org.apache.http.util.Asserts;
|
import org.apache.http.util.Asserts;
|
||||||
import org.apache.http.util.TextUtils;
|
import org.apache.http.util.TextUtils;
|
||||||
|
@ -150,17 +149,17 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
||||||
+ uri + "' not allowed");
|
+ uri + "' not allowed");
|
||||||
}
|
}
|
||||||
// Adjust location URI
|
// Adjust location URI
|
||||||
final HttpHost target = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
|
final HttpHost target = clientContext.getTargetHost();
|
||||||
Asserts.notNull(target, "Target host");
|
Asserts.notNull(target, "Target host");
|
||||||
final URI requestURI = new URI(request.getRequestLine().getUri());
|
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);
|
uri = URIUtils.resolve(absoluteRequestURI, uri);
|
||||||
}
|
}
|
||||||
} catch (final URISyntaxException ex) {
|
} catch (final URISyntaxException ex) {
|
||||||
throw new ProtocolException(ex.getMessage(), ex);
|
throw new ProtocolException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
|
RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
|
||||||
REDIRECT_LOCATIONS);
|
REDIRECT_LOCATIONS);
|
||||||
if (redirectLocations == null) {
|
if (redirectLocations == null) {
|
||||||
redirectLocations = new RedirectLocations();
|
redirectLocations = new RedirectLocations();
|
||||||
|
@ -189,7 +188,6 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
||||||
if (TextUtils.isEmpty(path)) {
|
if (TextUtils.isEmpty(path)) {
|
||||||
b.setPath("/");
|
b.setPath("/");
|
||||||
}
|
}
|
||||||
b.setFragment(null);
|
|
||||||
return b.build();
|
return b.build();
|
||||||
} catch (final URISyntaxException ex) {
|
} catch (final URISyntaxException ex) {
|
||||||
throw new ProtocolException("Invalid redirect URI: " + location, ex);
|
throw new ProtocolException("Invalid redirect URI: " + location, ex);
|
||||||
|
|
|
@ -72,11 +72,17 @@ public class ProtocolExec implements ClientExecChain {
|
||||||
this.httpProcessor = httpProcessor;
|
this.httpProcessor = httpProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rewriteRequestURI(final HttpRequestWrapper request, final HttpRoute route)
|
private void rewriteRequestURI(
|
||||||
throws ProtocolException {
|
final HttpRequestWrapper request,
|
||||||
|
final HttpRoute route,
|
||||||
|
final HttpClientContext context) throws ProtocolException {
|
||||||
try {
|
try {
|
||||||
URI uri = request.getURI();
|
URI uri = request.getURI();
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
|
final String fragment = uri.getFragment();
|
||||||
|
if (fragment != null) {
|
||||||
|
context.setAttribute(HttpClientContext.URI_FRAGMENT, fragment);
|
||||||
|
}
|
||||||
if (route.getProxyHost() != null && !route.isTunnelled()) {
|
if (route.getProxyHost() != null && !route.isTunnelled()) {
|
||||||
// Make sure the request URI is absolute
|
// Make sure the request URI is absolute
|
||||||
if (!uri.isAbsolute()) {
|
if (!uri.isAbsolute()) {
|
||||||
|
@ -126,7 +132,7 @@ public class ProtocolExec implements ClientExecChain {
|
||||||
request.setURI(uri);
|
request.setURI(uri);
|
||||||
|
|
||||||
// Re-write request URI if needed
|
// Re-write request URI if needed
|
||||||
rewriteRequestURI(request, route);
|
rewriteRequestURI(request, route, context);
|
||||||
|
|
||||||
final HttpParams params = request.getParams();
|
final HttpParams params = request.getParams();
|
||||||
HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);
|
HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);
|
||||||
|
|
|
@ -222,7 +222,7 @@ public class TestDefaultRedirectStrategy {
|
||||||
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
|
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
|
||||||
response.addHeader("Location", "/stuff#fragment");
|
response.addHeader("Location", "/stuff#fragment");
|
||||||
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
|
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
|
@Test
|
||||||
|
@ -235,7 +235,7 @@ public class TestDefaultRedirectStrategy {
|
||||||
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
|
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
|
||||||
response.addHeader("Location", "http://localhost/stuff#fragment");
|
response.addHeader("Location", "http://localhost/stuff#fragment");
|
||||||
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
|
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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue