You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/10/01 15:59:33 UTC

svn commit: r1177980 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client: DefaultRedirectStrategy.java LaxRedirectStrategy.java

Author: olegk
Date: Sat Oct  1 13:59:33 2011
New Revision: 1177980

URL: http://svn.apache.org/viewvc?rev=1177980&view=rev
Log:
HTTPCLIENT-1130: LaxRedirectStrategy improvements
Contributed by Alin Vasile <alinachegalati at yahoo.com> and Ryan Smith <rsmith at opensourcemasters.com>

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java?rev=1177980&r1=1177979&r2=1177980&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java Sat Oct  1 13:59:33 2011
@@ -72,6 +72,14 @@ public class DefaultRedirectStrategy imp
 
     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 imp
         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 imp
         }
     }
 
+    /**
+     * @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,

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java?rev=1177980&r1=1177979&r2=1177980&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java Sat Oct  1 13:59:33 2011
@@ -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
     };
 
     @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;