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";
|
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() {
|
public DefaultRedirectStrategy() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -92,12 +100,10 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
||||||
Header locationHeader = response.getFirstHeader("location");
|
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)
|
return isRedirectable(method) && locationHeader != null;
|
||||||
|| 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:
|
||||||
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|
return isRedirectable(method);
|
||||||
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
|
|
||||||
case HttpStatus.SC_SEE_OTHER:
|
case HttpStatus.SC_SEE_OTHER:
|
||||||
return true;
|
return true;
|
||||||
default:
|
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(
|
public HttpUriRequest getRedirect(
|
||||||
final HttpRequest request,
|
final HttpRequest request,
|
||||||
final HttpResponse response,
|
final HttpResponse response,
|
||||||
|
|
|
@ -27,17 +27,11 @@
|
||||||
|
|
||||||
package org.apache.http.impl.client;
|
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.annotation.Immutable;
|
||||||
import org.apache.http.client.RedirectStrategy;
|
import org.apache.http.client.RedirectStrategy;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpHead;
|
import org.apache.http.client.methods.HttpHead;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
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
|
* Lax {@link RedirectStrategy} implementation that automatically redirects all HEAD, GET and POST
|
||||||
|
@ -59,35 +53,7 @@ public class LaxRedirectStrategy extends DefaultRedirectStrategy {
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRedirected(
|
protected boolean isRedirectable(String method) {
|
||||||
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) {
|
|
||||||
for (String m: REDIRECT_METHODS) {
|
for (String m: REDIRECT_METHODS) {
|
||||||
if (m.equalsIgnoreCase(method)) {
|
if (m.equalsIgnoreCase(method)) {
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue