jsdever 2002/09/23 19:53:28
Modified: httpclient/src/java/org/apache/commons/httpclient
HttpMethodBase.java
Log:
Fix for redirect to default port -1 to port 80 failing.
Refactor out the checking for valid redirect request into static method
checkValidRedirect(). Added another static helper method that returns
the default port number for a given protocol. http->80, https->443
Fix for bug: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=12890
Contributed by: Jeff Dever
Revision Changes Path
1.61 +75 -24
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.60
retrieving revision 1.61
diff -u -r1.60 -r1.61
--- HttpMethodBase.java 12 Sep 2002 11:36:45 -0000 1.60
+++ HttpMethodBase.java 24 Sep 2002 02:53:27 -0000 1.61
@@ -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;
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>