commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java
Date Thu, 27 Mar 2003 03:53:08 GMT
mbecke      2003/03/26 19:53:08

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
  Log:
  The redirect request is now processed with URI instead of URL.
  
  Reviewed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.125     +52 -46    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.124
  retrieving revision 1.125
  diff -u -r1.124 -r1.125
  --- HttpMethodBase.java	24 Mar 2003 18:39:40 -0000	1.124
  +++ HttpMethodBase.java	27 Mar 2003 03:53:08 -0000	1.125
  @@ -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ück
  + * @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.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message