olegk 2004/04/12 04:16:26
Modified: httpclient/src/java/org/apache/commons/httpclient
HttpMethodDirector.java
httpclient/src/test/org/apache/commons/httpclient
TestRedirects.java TestWebappHeaders.java
Log:
PR #27194 (Host configuration properties not updated when the method is redirected)
Contributed by Oleg Kalnichevski
Reviewed by Michael Becke
Revision Changes Path
1.21 +5 -6 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java
Index: HttpMethodDirector.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- HttpMethodDirector.java 12 Apr 2004 10:30:46 -0000 1.20
+++ HttpMethodDirector.java 12 Apr 2004 11:16:25 -0000 1.21
@@ -532,6 +532,8 @@
redirectUri = new URI(currentUri, redirectUri);
}
}
+ method.setURI(redirectUri);
+ hostConfiguration.setHost(redirectUri);
} catch (URIException e) {
LOG.warn("Redirected location '" + location + "' is malformed");
return false;
@@ -547,9 +549,6 @@
redirectUri + "'");
}
}
- method.setPath(redirectUri.getEscapedPath());
- method.setQueryString(redirectUri.getEscapedQuery());
- hostConfiguration.setHost(redirectUri);
if (LOG.isDebugEnabled()) {
LOG.debug("Redirecting from '" + currentUri.getEscapedURI()
1.2 +39 -14 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java
Index: TestRedirects.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestRedirects.java 12 Apr 2004 10:30:46 -0000 1.1
+++ TestRedirects.java 12 Apr 2004 11:16:25 -0000 1.2
@@ -84,12 +84,6 @@
} else if (reqline.getUri().equals("/circular-location2/")) {
response.setStatusLine("HTTP/1.1 302 Object moved");
response.addHeader(new Header("Location", "/circular-location1/"));
- } else if (reqline.getUri().equals("/location1/")) {
- response.setStatusLine("HTTP/1.1 302 Object moved");
- response.addHeader(new Header("Location", "/location2/"));
- } else if (reqline.getUri().equals("/location2/")) {
- response.setStatusLine("HTTP/1.1 200 OK");
- response.setBodyString("Successful redirect");
} else {
response.setStatusLine("HTTP/1.1 404 Not Found");
}
@@ -128,16 +122,47 @@
}
}
+ private class RedirectService2 implements HttpService {
+
+ private String host = null;
+ private int port;
+
+ public RedirectService2(final String host, int port) {
+ super();
+ this.host = host;
+ this.port = port;
+ }
+
+ public boolean process(final SimpleRequest request, final SimpleResponse response)
+ throws IOException
+ {
+ RequestLine reqline = request.getRequestLine();
+ if (reqline.getUri().equals("/location1/")) {
+ response.setStatusLine("HTTP/1.1 302 Object moved");
+ response.addHeader(new Header("Location", "http://" + this.host + ":" +
this.port + "/location2/"));
+ } else if (reqline.getUri().equals("/location2/")) {
+ response.setStatusLine("HTTP/1.1 200 OK");
+ response.setBodyString("Successful redirect");
+ } else {
+ response.setStatusLine("HTTP/1.1 404 Not Found");
+ }
+ return true;
+ }
+ }
+
public void testRedirectLocation() throws IOException {
- this.server.setHttpService(new RedirectService());
+ String host = this.server.getLocalAddress();
+ int port = this.server.getLocalPort();
+ this.server.setHttpService(new RedirectService2(host, port));
GetMethod httpget = new GetMethod("/location1/");
try {
this.client.executeMethod(httpget);
- assertEquals("/location2/", httpget.getPath());
- assertEquals(new URI("/location2/", false), httpget.getURI());
- System.out.println();
+ assertEquals(host, httpget.getURI().getHost());
+ assertEquals(port, httpget.getURI().getPort());
+ assertEquals(new URI("http://" + host + ":" + port + "/location2/", false),
httpget.getURI());
} finally {
httpget.releaseConnection();
}
}
+
}
1.12 +14 -9 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappHeaders.java
Index: TestWebappHeaders.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappHeaders.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestWebappHeaders.java 22 Feb 2004 18:08:50 -0000 1.11
+++ TestWebappHeaders.java 12 Apr 2004 11:16:25 -0000 1.12
@@ -158,13 +158,13 @@
String hostname = addr.getHostName();
HttpClient client = new HttpClient();
- GetMethod get = new GetMethod("/" + getWebappContext());
+ GetMethod get = new GetMethod("/" + getWebappContext() + "/");
// Open connection using IP. Host header should be sent
// Note: RFC 2616 is somewhat unclear on whether a host should
// be sent in this context - however, both Mozilla and IE send
// the header for an IP address, instead of sending blank.
- client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
+ client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
try {
client.executeMethod(get);
} catch (Throwable t) {
@@ -173,17 +173,23 @@
}
Header hostHeader = get.getRequestHeader("Host");
assertTrue(hostHeader != null);
+ if (getPort() == 80) {
+ // no port information should be in the value
+ assertTrue(hostHeader.getValue().equals(ip));
+ } else {
+ assertTrue(hostHeader.getValue().equals(ip + ":" + getPort()));
+ }
// reset
get.recycle();
- get.setPath("/" + getWebappContext());
+ get.setPath("/" + getWebappContext() + "/");
// Open connection using Host. Host header should
// contain this value (this test will fail if DNS
// is not available. Additionally, if the port is
// something other that 80, then the port value
// should also be present in the header.
- client.getHostConfiguration().setHost(hostname, getPort(), getProtocol());
+ client.getHostConfiguration().setHost(hostname, getPort(), getProtocol());
try {
client.executeMethod(get);
} catch (Throwable t) {
@@ -199,6 +205,5 @@
assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort()));
}
}
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
|