rwaldhoff 01/08/14 13:22:29
Modified: httpclient/src/java/org/apache/commons/httpclient Tag:
rlwrefactoring HttpMethodBase.java
Log:
improving redirect support:
* confirm that protocol, host and port have not changed before redirecting
(previously would ignore host/port altogether, causing a request for the wrong
document whenever redirected from host to host.)
* pay attention to query strings in redirects (previously quietly ignored)
* give more useful error/warning messages
Also adding getQueryString method, which was ommitted by oversight
Revision Changes Path
No revision
No revision
1.10.2.13 +58 -21
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.10.2.12
retrieving revision 1.10.2.13
diff -u -r1.10.2.12 -r1.10.2.13
--- HttpMethodBase.java 2001/08/14 18:01:48 1.10.2.12
+++ HttpMethodBase.java 2001/08/14 20:22:29 1.10.2.13
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
1.10.2.12 2001/08/14 18:01:48 rwaldhoff Exp $
- * $Revision: 1.10.2.12 $
- * $Date: 2001/08/14 18:01:48 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
1.10.2.13 2001/08/14 20:22:29 rwaldhoff Exp $
+ * $Revision: 1.10.2.13 $
+ * $Date: 2001/08/14 20:22:29 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
@@ -25,6 +25,8 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
+import java.net.URL;
+import java.net.MalformedURLException;
import org.apache.commons.httpclient.log.*;
@@ -65,7 +67,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a>
* @author Rodney Waldhoff
- * @version $Id: HttpMethodBase.java,v 1.10.2.12 2001/08/14 18:01:48 rwaldhoff Exp $
+ * @version $Id: HttpMethodBase.java,v 1.10.2.13 2001/08/14 20:22:29 rwaldhoff Exp $
*/
public abstract class HttpMethodBase implements HttpMethod {
@@ -276,6 +278,13 @@
}
/**
+ * Get my query string.
+ */
+ public String getQueryString() {
+ return queryString;
+ }
+
+ /**
* Return an iterator over my headers.
*/
public Iterator getRequestHeaders() {
@@ -365,8 +374,8 @@
Set realms = new HashSet();
for(;;) {
- // for now, just track path
- visited.add(getPath());
+ // for now, just track path and qstring
+ visited.add(getPath() + queryString);
log.debug("HttpMethodBase.execute(): looping.");
@@ -463,32 +472,60 @@
Header location = getResponseHeader("location");
if(location != null) {
- String absolutePath = location.getValue();
- if(absolutePath.startsWith("http://")) {
+ URL url = null;
+ try {
+ url = new URL(location.getValue());
+ } catch(MalformedURLException e) {
+ log.error("Exception while parsing location header \""
+ location + "\"",e);
+ throw new HttpException(e.toString());
+ }
+ if("http".equalsIgnoreCase(url.getProtocol())) {
if(connection.isSecure()) {
+ log.warn("Server is attempting to redirect an HTTPS
request to an HTTP one.");
throw new HttpException("Server is attempting to
redirect an HTTPS request to an HTTP one.");
+ }
+ } else if("https".equalsIgnoreCase(url.getProtocol())) {
+ if(!connection.isSecure()) {
+ log.warn("Server is attempting to convert an HTTP
request to an HTTP one, which is currently not supported. Returning " + statusCode +
".");
+ break;
}
- absolutePath = absolutePath.substring(7);
- } else if(absolutePath.startsWith("https://")) {
- absolutePath = absolutePath.substring(8);
}
- // skip over hostname/port
- // (should actually confirm host/port are the same if
that's the intention)
- int slash = absolutePath.indexOf('/');
- if (slash != -1) {
- absolutePath = absolutePath.substring(slash);
+ if(!connection.getHost().equalsIgnoreCase(url.getHost())) {
+ log.warn("Server is attempting to redirect a different
host, which is currently not supported. Returning " + statusCode + ".");
+ break;
}
- if (absolutePath.equals("")) {
- absolutePath = "/";
+ if(url.getPort() == -1) {
+ if(connection.isSecure()) {
+ if(connection.getPort() != 443) {
+ log.warn("Server is attempting to redirect a
different port, which is currently not supported. Returning " + statusCode + ".");
+ break;
+ }
+ } else {
+ if(connection.getPort() != 80) {
+ log.warn("Server is attempting to redirect a
different port, which is currently not supported. Returning " + statusCode + ".");
+ break;
+ }
+ }
+ } else if(connection.getPort() != url.getPort()) {
+ log.warn("Server is attempting to redirect a different
port, which is currently not supported. Returning " + statusCode + ".");
+ break;
}
- if(log.isDebugEnabled()) {
- log.debug("Changing path from \"" + getPath() + "\" to
\"" + absolutePath + "\" in response to " + statusCode + " response.");
+ String absolutePath = url.getPath();
+ if(null == absolutePath) {
+ absolutePath = "/";
}
+ String qs = url.getQuery();
+
// if we haven't already, let's try it again with the new
path
- if(visited.contains(absolutePath)) {
+ if(visited.contains(absolutePath + queryString)) {
throw new HttpException("Redirect going into a loop,
visited \"" + absolutePath + "\" already.");
} else {
+ if(log.isDebugEnabled()) {
+ log.debug("Changing path from \"" + getPath() + "\"
to \"" + absolutePath + "\" in response to " + statusCode + " response.");
+ log.debug("Changing query string from \"" +
getQueryString() + "\" to \"" + qs + "\" in response to " + statusCode + " response.");
+ }
setPath(absolutePath);
+ setQueryString(qs);
continue;
}
} else {