From 6b1ef7da2bf3a4970a9f9251065fdd5b2ebff02c Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 1 Oct 2011 13:59:33 +0000 Subject: [PATCH] HTTPCLIENT-1130: LaxRedirectStrategy improvements Contributed by Alin Vasile and Ryan Smith git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1177980 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/DefaultRedirectStrategy.java | 26 +++++++++++--- .../http/impl/client/LaxRedirectStrategy.java | 36 +------------------ 2 files changed, 23 insertions(+), 39 deletions(-) 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 f33777bce..b44ef5781 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 @@ -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, diff --git a/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java b/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java index dd77d10c8..add0be337 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java @@ -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;