hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r537567 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http: client/ impl/client/
Date Sun, 13 May 2007 09:52:05 GMT
Author: olegk
Date: Sun May 13 02:52:04 2007
New Revision: 537567

URL: http://svn.apache.org/viewvc?view=rev&rev=537567
Log:
Ported circular redirect check

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/CircularRedirectException.java
      - copied, changed from r537480, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/CircularRedirectException.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectException.java
      - copied, changed from r537480, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/RedirectException.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectHandler.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultRedirectHandler.java

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/CircularRedirectException.java
(from r537480, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/CircularRedirectException.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/CircularRedirectException.java?view=diff&rev=537567&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/CircularRedirectException.java&r1=537480&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/CircularRedirectException.java&r2=537567
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/CircularRedirectException.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/CircularRedirectException.java
Sun May 13 02:52:04 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/CircularRedirectException.java,v
1.1 2005/01/14 19:40:39 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -28,7 +28,7 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.client;
 
 /**
  * Signals a circular redirect
@@ -38,6 +38,8 @@
  * @since 3.0
  */
 public class CircularRedirectException extends RedirectException {
+
+    private static final long serialVersionUID = 6830063487001091803L;
 
     /**
      * Creates a new CircularRedirectException with a <tt>null</tt> detail message.


Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectException.java
(from r537480, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/RedirectException.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectException.java?view=diff&rev=537567&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/RedirectException.java&r1=537480&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectException.java&r2=537567
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/RedirectException.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectException.java
Sun May 13 02:52:04 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/RedirectException.java,v
1.2 2004/04/18 23:51:35 jsdever Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -28,7 +28,9 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.client;
+
+import org.apache.http.ProtocolException;
 
 /**
  * Signals violation of HTTP specification caused by an invalid redirect
@@ -38,6 +40,8 @@
  * @since 3.0
  */
 public class RedirectException extends ProtocolException {
+
+    private static final long serialVersionUID = 4418824536372559326L;
 
     /**
      * Creates a new RedirectException with a <tt>null</tt> detail message. 

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectHandler.java?view=diff&rev=537567&r1=537566&r2=537567
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectHandler.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/RedirectHandler.java
Sun May 13 02:52:04 2007
@@ -56,11 +56,12 @@
      * given the response from the target server.
      * 
      * @param response the response received from the target server
+     * @param context the context for the request execution
      * 
      * @return <code>true</code> if the request should be redirected, <code>false</code>
      * otherwise
      */
-    boolean isRedirectNeeded(HttpResponse response);
+    boolean isRedirectNeeded(HttpResponse response, HttpContext context);
     
     /**
      * Determines the location request is expected to be redirected to 

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?view=diff&rev=537567&r1=537566&r2=537567
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
Sun May 13 02:52:04 2007
@@ -52,6 +52,7 @@
 import org.apache.http.ProtocolException;
 import org.apache.http.client.ClientRequestDirector;
 import org.apache.http.client.HttpRequestRetryHandler;
+import org.apache.http.client.RedirectException;
 import org.apache.http.client.RedirectHandler;
 import org.apache.http.client.RoutedRequest;
 import org.apache.http.client.methods.AbortableHttpRequest;
@@ -115,7 +116,10 @@
     /** The currently allocated connection. */
     protected ManagedClientConnection managedConn;
 
+    private int redirectCount;
 
+    private int maxRedirects;
+    
     public DefaultClientRequestDirector(
             final ClientConnectionManager conman,
             final ConnectionReuseStrategy reustrat,
@@ -151,6 +155,9 @@
         this.requestExec   = new HttpRequestExecutor(params);
 
         this.managedConn   = null;
+        
+        this.redirectCount = 0;
+        this.maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100);
 
         //@@@ authentication?
 
@@ -238,7 +245,7 @@
         }
         
         int execCount = 0;
-
+        
         HttpResponse response = null;
         boolean done = false;
         try {
@@ -516,8 +523,14 @@
 
         HttpParams params = request.getParams();
         if (params.getBooleanParameter(HttpClientParams.HANDLE_REDIRECTS, true) &&

-                this.redirectHandler.isRedirectNeeded(response)) {
+                this.redirectHandler.isRedirectNeeded(response, context)) {
 
+            if (redirectCount >= maxRedirects) {
+                throw new RedirectException("Maximum redirects ("
+                        + maxRedirects + ") exceeded");
+            }
+            redirectCount++;
+            
             URI uri;
             try {
                 uri = this.redirectHandler.getLocationURI(response, context);

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultRedirectHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultRedirectHandler.java?view=diff&rev=537567&r1=537566&r2=537567
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultRedirectHandler.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultRedirectHandler.java
Sun May 13 02:52:04 2007
@@ -33,6 +33,8 @@
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -41,6 +43,7 @@
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.ProtocolException;
+import org.apache.http.client.CircularRedirectException;
 import org.apache.http.client.RedirectHandler;
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.params.HttpParams;
@@ -60,8 +63,16 @@
 public class DefaultRedirectHandler implements RedirectHandler {
 
     private static final Log LOG = LogFactory.getLog(DefaultRedirectHandler.class);
+    
+    private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
 
-    public boolean isRedirectNeeded(final HttpResponse response) {
+    public DefaultRedirectHandler() {
+        super();
+    }
+    
+    public boolean isRedirectNeeded(
+            final HttpResponse response,
+            final HttpContext context) {
         if (response == null) {
             throw new IllegalArgumentException("HTTP response may not be null");
         }
@@ -131,6 +142,42 @@
                 throw new ProtocolException(ex.getMessage(), ex);
             }
         }
+        
+        if (params.isParameterFalse(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS)) {
+            
+            Set redirectLocations = (Set) context.getAttribute(REDIRECT_LOCATIONS);
+            
+            if (redirectLocations == null) {
+                redirectLocations = new HashSet();
+                context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
+            }
+            
+            URI redirectURI;
+            if (uri.getQuery() != null || uri.getFragment() != null) {
+                try {
+                    redirectURI = new URI(
+                            uri.getScheme(),
+                            null,
+                            uri.getHost(),
+                            uri.getPort(),
+                            uri.getPath(),
+                            null,
+                            null);
+                } catch (URISyntaxException ex) {
+                    throw new ProtocolException(ex.getMessage(), ex);
+                }
+            } else {
+                redirectURI = uri;
+            }
+            
+            if (redirectLocations.contains(redirectURI)) {
+                throw new CircularRedirectException("Circular redirect to '" +
+                        redirectURI + "'");
+            } else {
+                redirectLocations.add(redirectURI);
+            }
+        }
+        
         return uri;
     }
 



Mime
View raw message