You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Michael Becke <be...@u.washington.edu> on 2003/03/17 04:20:46 UTC

{PATCH] switch redirect to use URI

Attached is a patch that switches redirect processing from using URL to 
URI.  The only reason for doing this is to avoid setting the system 
property "java.protocol.handler.pkgs" to 
"com.sun.net.ssl.internal.www.protocol" when using HTTPS in pre 1.4 
JVMs.  Please let me know if you think this is worthwhile.

Mike



[PATCH] switch redirect to use URI

Posted by Michael Becke <be...@u.washington.edu>.
Again, but with the patch inlined.

On Sunday, March 16, 2003, at 10:20 PM, Michael Becke wrote:

> Attached is a patch that switches redirect processing from using URL  
> to URI.  The only reason for doing this is to avoid setting the system  
> property "java.protocol.handler.pkgs" to  
> "com.sun.net.ssl.internal.www.protocol" when using HTTPS in pre 1.4  
> JVMs.  Please let me know if you think this is worthwhile.
>
> Mike
>

Index: src/java/org/apache/commons/httpclient/HttpMethodBase.java
===================================================================
RCS file:  
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/ 
httpclient/HttpMethodBase.java,v
retrieving revision 1.123
diff -u -r1.123 HttpMethodBase.java
--- src/java/org/apache/commons/httpclient/HttpMethodBase.java	13 Mar  
2003 17:51:28 -0000	1.123
+++ src/java/org/apache/commons/httpclient/HttpMethodBase.java	17 Mar  
2003 03:14:40 -0000
@@ -68,14 +68,12 @@
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InterruptedIOException;
-import java.net.MalformedURLException;
-import java.net.URL;
  import java.util.HashSet;
  import java.util.Set;

-import org.apache.commons.httpclient.cookie.MalformedCookieException;
  import org.apache.commons.httpclient.cookie.CookiePolicy;
  import org.apache.commons.httpclient.cookie.CookieSpec;
+import org.apache.commons.httpclient.cookie.MalformedCookieException;
  import org.apache.commons.httpclient.protocol.Protocol;
  import org.apache.commons.httpclient.util.URIUtil;
  import org.apache.commons.logging.Log;
@@ -128,7 +126,7 @@
   * @author <a href="mailto:dion@apache.org">dIon Gillard</a>
   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
   * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
- * @author Ortwin GlÔøΩ
+ * @author Ortwin GlÔøΩck
   * @author Eric Johnson
   * @author Michael Becke
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
@@ -1074,33 +1072,37 @@

          //rfc2616 demands the location value be a complete URI
          //Location       = "Location" ":" absoluteURI
-        URL redirectUrl = null;
-        URL currentUrl = null;
+        URI redirectUri = null;
+        URI currentUri = null;

          try {
-            currentUrl = new URL(conn.getProtocol().getScheme(),
-                conn.getHost(), conn.getPort(), this.getPath());
-            redirectUrl = new URL(location);
-        } catch (MalformedURLException e) {
-            if (isStrictMode()) {
-                LOG.warn("Redirected location '" + location
-                    + "' is not acceptable in strict mode");
-                return false;
-            } else { //location is incomplete, use current values for  
defaults
-                try {
-                    LOG.debug("Redirect URL is not absolute - parsing  
as relative");
-                    redirectUrl = new URL(currentUrl, location);
-                } catch (MalformedURLException ex) {
-                    LOG.warn("Redirected location '" + location
-                            + "' is malformed");
+            currentUri = new URI(
+                conn.getProtocol().getScheme(),
+                null,
+                conn.getHost(),
+                conn.getPort(),
+                this.getPath()
+            );
+            redirectUri = new URI(location.toCharArray());
+            if (redirectUri.isRelativeURI()) {
+                if (isStrictMode()) {
+                    LOG.warn("Redirected location '" + location
+                        + "' is not acceptable in strict mode");
                      return false;
+                } else {
+                    //location is incomplete, use current values for  
defaults
+                    LOG.debug("Redirect URI is not absolute - parsing  
as relative");
+                    redirectUri = new URI(currentUri, redirectUri);
                  }
              }
+        } catch (URIException e) {
+            LOG.warn("Redirected location '" + location + "' is  
malformed");
+            return false;
          }

          //check for redirect to a different protocol, host or port
          try {
-            checkValidRedirect(currentUrl, redirectUrl);
+            checkValidRedirect(currentUri, redirectUri);
          } catch (HttpException ex) {
              //LOG the error and let the client handle the redirect
              LOG.warn(ex.getMessage());
@@ -1110,51 +1112,56 @@
          //update the current location with the redirect location.
          //avoiding use of URL.getPath() and URL.getQuery() to keep
          //jdk1.2 comliance.
-        setPath(URIUtil.getPath(redirectUrl.toString()));
-        setQueryString(URIUtil.getQuery(redirectUrl.toString()));
+        setPath(redirectUri.getEscapedPath());
+        setQueryString(redirectUri.getEscapedQuery());

          if (LOG.isDebugEnabled()) {
-            LOG.debug("Redirecting from '" +  
currentUrl.toExternalForm()
-                    + "' to '" + redirectUrl.toExternalForm());
+            LOG.debug("Redirecting from '" + currentUri.getEscapedURI()
+                + "' to '" + redirectUri.getEscapedURI());
          }

          return true;
-
      }

-
      /**
-     * Check for a valid redirect given the current conn and new url.
+     * Check for a valid redirect given the current conn and new URI.
       * Redirect to a different protocol, host or port are checked for  
validity.
       *
-     * @param currentUrl The current URL (redirecting from)
-     * @param redirectUrl The new URL to redirect to
+     * @param currentUri The current URI (redirecting from)
+     * @param redirectUri The new URI to redirect to
       * @throws HttpException if the redirect is invalid
       * @since 2.0
       */
-    private static void checkValidRedirect(URL currentUrl, URL  
redirectUrl)
+    private static void checkValidRedirect(URI currentUri, URI  
redirectUri)
      throws HttpException {
          LOG.trace("enter  
HttpMethodBase.checkValidRedirect(HttpConnection, URL)");

-        String oldProtocol = currentUrl.getProtocol();
-        String newProtocol = redirectUrl.getProtocol();
+        String oldProtocol = currentUri.getScheme();
+        String newProtocol = redirectUri.getScheme();
          if (!oldProtocol.equals(newProtocol)) {
              throw new HttpException("Redirect from protocol " +  
oldProtocol
                      + " to " + newProtocol + " is not supported");
          }

-        String oldHost = currentUrl.getHost();
-        String newHost = redirectUrl.getHost();
-        if (!oldHost.equalsIgnoreCase(newHost)) {
-            throw new HttpException("Redirect from host " + oldHost
-                    + " to " + newHost + " is not supported");
+        try {
+            String oldHost = currentUri.getHost();
+            String newHost = redirectUri.getHost();
+            if (!oldHost.equalsIgnoreCase(newHost)) {
+                throw new HttpException("Redirect from host " + oldHost
+                        + " to " + newHost + " is not supported");
+            }
+        } catch (URIException e) {
+            LOG.warn("Error getting URI host", e);
+            throw new HttpException("Invalid Redirect URI from: "
+                + currentUri.getEscapedURI() + " to: " +  
redirectUri.getEscapedURI()
+            );
          }

-        int oldPort = currentUrl.getPort();
+        int oldPort = currentUri.getPort();
          if (oldPort < 0) {
              oldPort = getDefaultPort(oldProtocol);
          }
-        int newPort = redirectUrl.getPort();
+        int newPort = redirectUri.getPort();
          if (newPort < 0) {
              newPort = getDefaultPort(newProtocol);
          }
@@ -1163,7 +1170,6 @@
                      + " to " + newPort + " is not supported");
          }
      }
-

      /**
       * Returns the default port for the given protocol.