diff --git a/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index d7cf675a1..bb16301fe 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -47,6 +47,7 @@ import org.apache.http.HttpEntity; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.RedirectHandler; import org.apache.http.client.RedirectStrategy; import org.apache.http.client.RequestDirector; import org.apache.http.client.ResponseHandler; @@ -153,6 +154,7 @@ import org.apache.http.protocol.ImmutableHttpProcessor; * @since 4.0 */ @ThreadSafe +@SuppressWarnings("deprecation") public abstract class AbstractHttpClient implements HttpClient { private final Log log = LogFactory.getLog(getClass()); @@ -269,7 +271,7 @@ public abstract class AbstractHttpClient implements HttpClient { @Deprecated - protected abstract org.apache.http.client.RedirectHandler createRedirectHandler(); + protected abstract RedirectHandler createRedirectHandler(); protected abstract AuthenticationHandler createTargetAuthenticationHandler(); @@ -391,7 +393,7 @@ public abstract class AbstractHttpClient implements HttpClient { @Deprecated - public synchronized final org.apache.http.client.RedirectHandler getRedirectHandler() { + public synchronized final RedirectHandler getRedirectHandler() { return createRedirectHandler(); } @@ -404,10 +406,9 @@ public abstract class AbstractHttpClient implements HttpClient { /** * @since 4.1 */ - @SuppressWarnings("deprecation") public synchronized final RedirectStrategy getRedirectStrategy() { if (redirectStrategy == null) { - redirectStrategy = new DefaultRedirectStrategyAdaptor(createRedirectHandler()); + redirectStrategy = new DefaultRedirectStrategy(); } return redirectStrategy; } 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 0d51ae132..c41ee3870 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 @@ -76,11 +76,14 @@ public class DefaultRedirectStrategy implements RedirectStrategy { } int statusCode = response.getStatusLine().getStatusCode(); + String method = request.getRequestLine().getMethod(); + Header locationHeader = response.getFirstHeader("location"); switch (statusCode) { 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_TEMPORARY_REDIRECT: - String method = request.getRequestLine().getMethod(); return method.equalsIgnoreCase(HttpGet.METHOD_NAME) || method.equalsIgnoreCase(HttpHead.METHOD_NAME); case HttpStatus.SC_SEE_OTHER: diff --git a/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java b/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java index 529073753..33371934e 100644 --- a/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java +++ b/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java @@ -303,6 +303,44 @@ public class TestRedirects extends BasicServerTestBase { 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 public void testBasicRedirect303() throws Exception { InetSocketAddress address = this.localServer.getServiceAddress();