HTTPCLIENT-1130: LaxRedirectStrategy improvements
Contributed by Alin Vasile <alinachegalati at yahoo.com> and Ryan Smith <rsmith at opensourcemasters.com> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1177980 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6ae3bb8bc8
commit
6b1ef7da2b
|
@ -72,6 +72,14 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
|||
|
||||
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
|
||||
|
||||
/**
|
||||
* Redirectable methods.
|
||||
*/
|
||||
private static final String[] REDIRECT_METHODS = new String[] {
|
||||
HttpGet.METHOD_NAME,
|
||||
HttpHead.METHOD_NAME
|
||||
};
|
||||
|
||||
public DefaultRedirectStrategy() {
|
||||
super();
|
||||
}
|
||||
|
@ -92,12 +100,10 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
|||
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;
|
||||
return isRedirectable(method) && locationHeader != null;
|
||||
case HttpStatus.SC_MOVED_PERMANENTLY:
|
||||
case HttpStatus.SC_TEMPORARY_REDIRECT:
|
||||
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|
||||
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
|
||||
return isRedirectable(method);
|
||||
case HttpStatus.SC_SEE_OTHER:
|
||||
return true;
|
||||
default:
|
||||
|
@ -198,6 +204,18 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
protected boolean isRedirectable(final String method) {
|
||||
for (String m: REDIRECT_METHODS) {
|
||||
if (m.equalsIgnoreCase(method)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public HttpUriRequest getRedirect(
|
||||
final HttpRequest request,
|
||||
final HttpResponse response,
|
||||
|
|
|
@ -27,17 +27,11 @@
|
|||
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.RedirectStrategy;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
/**
|
||||
* Lax {@link RedirectStrategy} implementation that automatically redirects all HEAD, GET and POST
|
||||
|
@ -59,35 +53,7 @@ public class LaxRedirectStrategy extends DefaultRedirectStrategy {
|
|||
};
|
||||
|
||||
@Override
|
||||
public boolean isRedirected(
|
||||
final HttpRequest request,
|
||||
final HttpResponse response,
|
||||
final HttpContext context) throws ProtocolException {
|
||||
if (request == null) {
|
||||
throw new IllegalArgumentException("HTTP request may not be null");
|
||||
}
|
||||
if (response == null) {
|
||||
throw new IllegalArgumentException("HTTP response may not be null");
|
||||
}
|
||||
|
||||
String method = request.getRequestLine().getMethod();
|
||||
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
switch (status) {
|
||||
case HttpStatus.SC_MOVED_TEMPORARILY:
|
||||
Header location = response.getFirstHeader("location");
|
||||
return isRedirectable(method) && location != null;
|
||||
case HttpStatus.SC_MOVED_PERMANENTLY:
|
||||
case HttpStatus.SC_TEMPORARY_REDIRECT:
|
||||
return isRedirectable(method);
|
||||
case HttpStatus.SC_SEE_OTHER:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRedirectable(String method) {
|
||||
protected boolean isRedirectable(String method) {
|
||||
for (String m: REDIRECT_METHODS) {
|
||||
if (m.equalsIgnoreCase(method)) {
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue