mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-17 07:26:47 +00:00
HTTPCLIENT-1351: added method to HttpClientContext to obtain redirect locations
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1486956 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6fad24aef2
commit
26ee6689f3
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents an iterable collection of {@link java.net.URI} locations.
|
||||||
|
*
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public interface URICollection extends Iterable<URI> {
|
||||||
|
|
||||||
|
int getCount();
|
||||||
|
|
||||||
|
boolean isEmpty();
|
||||||
|
|
||||||
|
boolean contains(URI uri);
|
||||||
|
|
||||||
|
Set<URI> getUnique();
|
||||||
|
|
||||||
|
List<URI> getAll();
|
||||||
|
|
||||||
|
}
|
@ -27,14 +27,13 @@
|
|||||||
|
|
||||||
package org.apache.http.client.protocol;
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import org.apache.http.annotation.NotThreadSafe;
|
import org.apache.http.annotation.NotThreadSafe;
|
||||||
import org.apache.http.auth.AuthSchemeProvider;
|
import org.apache.http.auth.AuthSchemeProvider;
|
||||||
import org.apache.http.auth.AuthState;
|
import org.apache.http.auth.AuthState;
|
||||||
import org.apache.http.client.AuthCache;
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.CookieStore;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
|
import org.apache.http.client.URICollection;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.config.Lookup;
|
import org.apache.http.config.Lookup;
|
||||||
import org.apache.http.conn.routing.HttpRoute;
|
import org.apache.http.conn.routing.HttpRoute;
|
||||||
@ -56,18 +55,19 @@
|
|||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
public class HttpClientContext extends HttpCoreContext {
|
public class HttpClientContext extends HttpCoreContext {
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute name of a {@link URI} object that represents
|
|
||||||
* request URI location (relative or absolute) of the last request target.
|
|
||||||
*/
|
|
||||||
public static final String HTTP_LOCATION = "http.location";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public static final String HTTP_ROUTE = "http.route";
|
public static final String HTTP_ROUTE = "http.route";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attribute name of a {@link org.apache.http.client.URICollection} object that
|
||||||
|
* represents a collection of all redirect locations received in the process
|
||||||
|
* of request execution.
|
||||||
|
*/
|
||||||
|
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 CookieSpecProvider} registry.
|
* the actual {@link CookieSpecProvider} registry.
|
||||||
@ -160,14 +160,14 @@ public HttpClientContext() {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI getHttpLocation() {
|
|
||||||
return getAttribute(HTTP_LOCATION, URI.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RouteInfo getHttpRoute() {
|
public RouteInfo getHttpRoute() {
|
||||||
return getAttribute(HTTP_ROUTE, HttpRoute.class);
|
return getAttribute(HTTP_ROUTE, HttpRoute.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public URICollection getRedirectLocations() {
|
||||||
|
return getAttribute(REDIRECT_LOCATIONS, URICollection.class);
|
||||||
|
}
|
||||||
|
|
||||||
public CookieStore getCookieStore() {
|
public CookieStore getCookieStore() {
|
||||||
return getAttribute(COOKIE_STORE, CookieStore.class);
|
return getAttribute(COOKIE_STORE, CookieStore.class);
|
||||||
}
|
}
|
||||||
|
@ -74,6 +74,10 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
|||||||
|
|
||||||
private final Log log = LogFactory.getLog(getClass());
|
private final Log log = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
|
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
|
||||||
|
|
||||||
public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
|
public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
|
||||||
@ -160,10 +164,10 @@ public URI getLocationURI(
|
|||||||
}
|
}
|
||||||
|
|
||||||
RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
|
RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
|
||||||
REDIRECT_LOCATIONS);
|
HttpClientContext.REDIRECT_LOCATIONS);
|
||||||
if (redirectLocations == null) {
|
if (redirectLocations == null) {
|
||||||
redirectLocations = new RedirectLocations();
|
redirectLocations = new RedirectLocations();
|
||||||
context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
|
context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
|
||||||
}
|
}
|
||||||
if (!config.isCircularRedirectsAllowed()) {
|
if (!config.isCircularRedirectsAllowed()) {
|
||||||
if (redirectLocations.contains(uri)) {
|
if (redirectLocations.contains(uri)) {
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.http.annotation.NotThreadSafe;
|
import org.apache.http.annotation.NotThreadSafe;
|
||||||
|
import org.apache.http.client.URICollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents a collection of {@link URI}s used as redirect locations.
|
* This class represents a collection of {@link URI}s used as redirect locations.
|
||||||
@ -42,7 +43,7 @@
|
|||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
@NotThreadSafe // HashSet is not synch.
|
@NotThreadSafe // HashSet is not synch.
|
||||||
public class RedirectLocations {
|
public class RedirectLocations implements URICollection {
|
||||||
|
|
||||||
private final Set<URI> unique;
|
private final Set<URI> unique;
|
||||||
private final List<URI> all;
|
private final List<URI> all;
|
||||||
@ -96,4 +97,32 @@ public List<URI> getAll() {
|
|||||||
return new ArrayList<URI>(this.all);
|
return new ArrayList<URI>(this.all);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public Set<URI> getUnique() {
|
||||||
|
return new HashSet<URI>(this.unique);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public Iterator<URI> iterator() {
|
||||||
|
return getAll().iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public int getCount() {
|
||||||
|
return this.all.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return this.all.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -74,12 +74,10 @@ public ProtocolExec(final ClientExecChain requestExecutor, final HttpProcessor h
|
|||||||
|
|
||||||
private void rewriteRequestURI(
|
private void rewriteRequestURI(
|
||||||
final HttpRequestWrapper request,
|
final HttpRequestWrapper request,
|
||||||
final HttpRoute route,
|
final HttpRoute route) throws ProtocolException {
|
||||||
final HttpClientContext context) throws ProtocolException {
|
|
||||||
try {
|
try {
|
||||||
URI uri = request.getURI();
|
URI uri = request.getURI();
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
context.setAttribute(HttpClientContext.HTTP_LOCATION, uri);
|
|
||||||
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()) {
|
||||||
@ -129,7 +127,7 @@ public CloseableHttpResponse execute(final HttpRoute route, final HttpRequestWra
|
|||||||
request.setURI(uri);
|
request.setURI(uri);
|
||||||
|
|
||||||
// Re-write request URI if needed
|
// Re-write request URI if needed
|
||||||
rewriteRequestURI(request, route, context);
|
rewriteRequestURI(request, route);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -258,9 +258,6 @@ public void testAbsoluteRequestURIWithFragment() throws Exception {
|
|||||||
|
|
||||||
final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
|
final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
|
||||||
Assert.assertEquals("/stuff", request.getRequestLine().getUri());
|
Assert.assertEquals("/stuff", request.getRequestLine().getUri());
|
||||||
|
|
||||||
final URI location = (URI) context.getAttribute(HttpClientContext.HTTP_LOCATION);
|
|
||||||
Assert.assertEquals(uri, location);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
package org.apache.http.impl.client.integration;
|
package org.apache.http.impl.client.integration;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
@ -41,10 +42,12 @@
|
|||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.CookieStore;
|
||||||
import org.apache.http.client.RedirectException;
|
import org.apache.http.client.RedirectException;
|
||||||
|
import org.apache.http.client.URICollection;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.client.protocol.HttpClientContext;
|
import org.apache.http.client.protocol.HttpClientContext;
|
||||||
|
import org.apache.http.client.utils.URIUtils;
|
||||||
import org.apache.http.cookie.SM;
|
import org.apache.http.cookie.SM;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.BasicCookieStore;
|
import org.apache.http.impl.client.BasicCookieStore;
|
||||||
@ -222,6 +225,9 @@ public void testBasicRedirect300() throws Exception {
|
|||||||
|
|
||||||
Assert.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
|
Assert.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
|
||||||
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
|
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
|
||||||
|
|
||||||
|
URICollection redirects = context.getRedirectLocations();
|
||||||
|
Assert.assertNull(redirects);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -243,6 +249,13 @@ public void testBasicRedirect301() throws Exception {
|
|||||||
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
|
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
|
||||||
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
|
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
|
||||||
Assert.assertEquals(target, host);
|
Assert.assertEquals(target, host);
|
||||||
|
|
||||||
|
URICollection redirects = context.getRedirectLocations();
|
||||||
|
Assert.assertNotNull(redirects);
|
||||||
|
Assert.assertEquals(1, redirects.getCount());
|
||||||
|
|
||||||
|
URI redirect = URIUtils.rewriteURI(new URI("/newlocation/"), target);
|
||||||
|
Assert.assertTrue(redirects.contains(redirect));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user