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:
Oleg Kalnichevski 2010-05-11 19:49:38 +00:00
parent 240df7363b
commit 7381110b46
3 changed files with 47 additions and 5 deletions

View File

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

View File

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

View File

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