? .HttpMethodBase.java.swp ? portredirect.diff ? methods/methods.diff Index: HttpMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v retrieving revision 1.19 diff -u -r1.19 HttpMethod.java --- HttpMethod.java 7 Sep 2002 01:07:47 -0000 1.19 +++ HttpMethod.java 23 Sep 2002 21:28:22 -0000 @@ -72,10 +72,17 @@ *
* @author Remy Maucherat * @author Rod Waldhoff +<<<<<<< HttpMethod.java + * @author Jeff Dever + * + * @version $Revision: 1.18 $ $Date: 2002/09/05 03:37:55 $ + * @since 1.0 +======= * @author Jeff Dever * * @version $Revision: 1.19 $ $Date: 2002/09/07 01:07:47 $ * @since 1.0 +>>>>>>> 1.19 */ public interface HttpMethod { Index: HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.60 diff -u -r1.60 HttpMethodBase.java --- HttpMethodBase.java 12 Sep 2002 11:36:45 -0000 1.60 +++ HttpMethodBase.java 23 Sep 2002 21:28:23 -0000 @@ -817,15 +817,15 @@ + "'"); } - URL url = null; //the new url //rfc2616 demands the location value be a complete URI //Location = "Location" ":" absoluteURI + URL url = null; //the new url try { url = new URL(location); } catch (Exception ex) { if (isStrictMode()) { - log.error("Redirected location '" + locationHeader.getValue() + + log.error("Redirected location '" + location + "' is not acceptable in strict mode"); return statusCode; //should we throw an exception? } @@ -846,24 +846,11 @@ } //check for redirect to a different protocol, host or port - String error = null; - if (!conn.getProtocol().equalsIgnoreCase( - url.getProtocol())) { - error = "Redirect from protocol " + conn.getProtocol() - + " to " + url.getProtocol() - + " is not supported"; - } - if (!conn.getHost().equalsIgnoreCase(url.getHost())) { - error = "Redirect from host " + conn.getHost() + " to " - + url.getHost() + " is not supported"; - } - if (conn.getPort() != url.getPort()) { - error = "Redirect from port " + conn.getPort() + " to " - + url.getPort() + " is not supported"; - } - if (error != null) { - log.warn(error); - //throw new HttpException(error); + try{ + checkValidRedirect(conn, url); + } catch (HttpException ex) { + //log the error and let the client handle the redirect + log.warn(ex.getMessage()); return statusCode; } @@ -906,6 +893,70 @@ log.error("Narrowly avoided an infinite loop in execute"); throw new HttpException("Maximum redirects ("+ maxForwards +") exceeded"); + } + + + /** + * Check for a valid redirect given the current conn and new url. + * Redirect to a different protocol, host or port are checked for validity. + * + * @param conn The existing HttpConnection + * @param url The new URL to redirect to + * @throws HttpException if the redirect is invalid + * @since 2.0 + */ + private static void checkValidRedirect(HttpConnection conn, URL url) + throws HttpException { + log.trace("enter HttpMethodBase.checkValidRedirect(HttpConnection, URL)"); + + String oldProtocol = conn.getProtocol().toLowerCase(); + String newProtocol = url.getProtocol().toLowerCase(); + if (! oldProtocol.equals(newProtocol)) { + throw new HttpException("Redirect from protocol " + oldProtocol + + " to " + newProtocol + " is not supported"); + } + + String oldHost = conn.getHost(); + String newHost = url.getHost(); + if (! oldHost.equalsIgnoreCase(newHost)) { + throw new HttpException("Redirect from host " + oldHost + + " to " + newHost + " is not supported"); + } + + int oldPort = conn.getPort(); + if (oldPort < 0) { + oldPort = getDefaultPort(oldProtocol); + } + int newPort = url.getPort(); + if (newPort < 0) { + newPort = getDefaultPort(newProtocol); + } + if (oldPort != newPort) { + throw new HttpException("Redirect from port " + oldPort + + " to " + newPort + " is not supported"); + } + } + + + /** + * Returns the default port for the given protocol. + * + * @param protocol currently only http and https are recognized + * @return the default port of the given protocol or -1 if the + * protocol is not recognized. + * + * @since 2.0 + * + */ + private static int getDefaultPort(String protocol) { + String proto = protocol.toLowerCase().trim(); + if (proto.equals("http")){ + return 80; + } + else if (proto.equals("https")){ + return 443; + } + return -1; } /**