HTTPCLIENT-939: 302 without redirect location should not cause a protocol exception
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@943247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
240df7363b
commit
7381110b46
|
@ -47,6 +47,7 @@ import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.auth.AuthSchemeRegistry;
|
import org.apache.http.auth.AuthSchemeRegistry;
|
||||||
import org.apache.http.client.AuthenticationHandler;
|
import org.apache.http.client.AuthenticationHandler;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
|
import org.apache.http.client.RedirectHandler;
|
||||||
import org.apache.http.client.RedirectStrategy;
|
import org.apache.http.client.RedirectStrategy;
|
||||||
import org.apache.http.client.RequestDirector;
|
import org.apache.http.client.RequestDirector;
|
||||||
import org.apache.http.client.ResponseHandler;
|
import org.apache.http.client.ResponseHandler;
|
||||||
|
@ -153,6 +154,7 @@ import org.apache.http.protocol.ImmutableHttpProcessor;
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public abstract class AbstractHttpClient implements HttpClient {
|
public abstract class AbstractHttpClient implements HttpClient {
|
||||||
|
|
||||||
private final Log log = LogFactory.getLog(getClass());
|
private final Log log = LogFactory.getLog(getClass());
|
||||||
|
@ -269,7 +271,7 @@ public abstract class AbstractHttpClient implements HttpClient {
|
||||||
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
protected abstract org.apache.http.client.RedirectHandler createRedirectHandler();
|
protected abstract RedirectHandler createRedirectHandler();
|
||||||
|
|
||||||
|
|
||||||
protected abstract AuthenticationHandler createTargetAuthenticationHandler();
|
protected abstract AuthenticationHandler createTargetAuthenticationHandler();
|
||||||
|
@ -391,7 +393,7 @@ public abstract class AbstractHttpClient implements HttpClient {
|
||||||
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public synchronized final org.apache.http.client.RedirectHandler getRedirectHandler() {
|
public synchronized final RedirectHandler getRedirectHandler() {
|
||||||
return createRedirectHandler();
|
return createRedirectHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,10 +406,9 @@ public abstract class AbstractHttpClient implements HttpClient {
|
||||||
/**
|
/**
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public synchronized final RedirectStrategy getRedirectStrategy() {
|
public synchronized final RedirectStrategy getRedirectStrategy() {
|
||||||
if (redirectStrategy == null) {
|
if (redirectStrategy == null) {
|
||||||
redirectStrategy = new DefaultRedirectStrategyAdaptor(createRedirectHandler());
|
redirectStrategy = new DefaultRedirectStrategy();
|
||||||
}
|
}
|
||||||
return redirectStrategy;
|
return redirectStrategy;
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,11 +76,14 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
String method = request.getRequestLine().getMethod();
|
||||||
|
Header locationHeader = response.getFirstHeader("location");
|
||||||
switch (statusCode) {
|
switch (statusCode) {
|
||||||
case HttpStatus.SC_MOVED_TEMPORARILY:
|
case HttpStatus.SC_MOVED_TEMPORARILY:
|
||||||
|
return (method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|
||||||
|
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME)) && locationHeader != null;
|
||||||
case HttpStatus.SC_MOVED_PERMANENTLY:
|
case HttpStatus.SC_MOVED_PERMANENTLY:
|
||||||
case HttpStatus.SC_TEMPORARY_REDIRECT:
|
case HttpStatus.SC_TEMPORARY_REDIRECT:
|
||||||
String method = request.getRequestLine().getMethod();
|
|
||||||
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|
||||||
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
|
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
|
||||||
case HttpStatus.SC_SEE_OTHER:
|
case HttpStatus.SC_SEE_OTHER:
|
||||||
|
|
|
@ -303,6 +303,44 @@ public class TestRedirects extends BasicServerTestBase {
|
||||||
Assert.assertEquals(port, targetHost.getPort());
|
Assert.assertEquals(port, targetHost.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBasicRedirect302NoLocation() throws Exception {
|
||||||
|
InetSocketAddress address = this.localServer.getServiceAddress();
|
||||||
|
int port = address.getPort();
|
||||||
|
String host = address.getHostName();
|
||||||
|
this.localServer.register("*", new HttpRequestHandler() {
|
||||||
|
|
||||||
|
public void handle(
|
||||||
|
final HttpRequest request,
|
||||||
|
final HttpResponse response,
|
||||||
|
final HttpContext context) throws HttpException, IOException {
|
||||||
|
response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
DefaultHttpClient client = new DefaultHttpClient();
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpGet httpget = new HttpGet("/oldlocation/");
|
||||||
|
|
||||||
|
HttpResponse response = client.execute(getServerHttp(), httpget, context);
|
||||||
|
HttpEntity e = response.getEntity();
|
||||||
|
if (e != null) {
|
||||||
|
e.consumeContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
|
||||||
|
ExecutionContext.HTTP_REQUEST);
|
||||||
|
HttpHost targetHost = (HttpHost) context.getAttribute(
|
||||||
|
ExecutionContext.HTTP_TARGET_HOST);
|
||||||
|
|
||||||
|
Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
|
||||||
|
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
|
||||||
|
Assert.assertEquals(host, targetHost.getHostName());
|
||||||
|
Assert.assertEquals(port, targetHost.getPort());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBasicRedirect303() throws Exception {
|
public void testBasicRedirect303() throws Exception {
|
||||||
InetSocketAddress address = this.localServer.getServiceAddress();
|
InetSocketAddress address = this.localServer.getServiceAddress();
|
||||||
|
|
Loading…
Reference in New Issue