Index: test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
===================================================================
--- test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java (revision 169255)
+++ test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java (working copy)
@@ -32,7 +32,6 @@
import java.io.Reader;
import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.server.HttpService;
import org.apache.commons.httpclient.server.SimpleRequest;
import org.apache.commons.httpclient.server.SimpleResponse;
@@ -126,71 +125,6 @@
}
}
- public void testRelativeURLHitWithDefaultHost() throws IOException {
- this.server.setHttpService(new EchoService());
- // Set default host
- this.client.getHostConfiguration().setHost(
- this.server.getLocalAddress(),
- this.server.getLocalPort(),
- Protocol.getProtocol("http"));
-
- GetMethod httpget = new GetMethod("/test/");
- try {
- this.client.executeMethod(httpget);
- assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
- } finally {
- httpget.releaseConnection();
- }
- }
-
- public void testRelativeURLHitWithoutDefaultHost() throws IOException {
- this.server.setHttpService(new EchoService());
- // reset default host configuration
- this.client.setHostConfiguration(new HostConfiguration());
-
- GetMethod httpget = new GetMethod("/test/");
- try {
- this.client.executeMethod(httpget);
- fail("IllegalArgumentException should have been thrown");
- } catch (IllegalArgumentException expected) {
- } finally {
- httpget.releaseConnection();
- }
- }
-
- public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
- this.server.setHttpService(new EchoService());
- // reset default host configuration
- this.client.setHostConfiguration(new HostConfiguration());
-
- GetMethod httpget = new GetMethod("http://" +
- this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
- try {
- this.client.executeMethod(httpget);
- assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
- } finally {
- httpget.releaseConnection();
- }
- }
-
- public void testAbsoluteURLHitWithDefaultHost() throws IOException {
- this.server.setHttpService(new EchoService());
- // Somewhere out there in pampa
- this.client.getHostConfiguration().setHost(
- "somewhere.in.pampa",
- 9999,
- Protocol.getProtocol("http"));
-
- GetMethod httpget = new GetMethod("http://" +
- this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
- try {
- this.client.executeMethod(httpget);
- assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
- } finally {
- httpget.releaseConnection();
- }
- }
-
public void testHttpMethodBasePaths() throws Exception {
HttpMethod simple = new FakeHttpMethod();
String[] paths = {
Index: test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
===================================================================
--- test/org/apache/commons/httpclient/server/ProxyRequestHandler.java (revision 169255)
+++ test/org/apache/commons/httpclient/server/ProxyRequestHandler.java (working copy)
@@ -30,12 +30,14 @@
package org.apache.commons.httpclient.server;
import java.io.IOException;
+import java.net.UnknownHostException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.URI;
+import org.apache.commons.httpclient.URIException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -72,14 +74,27 @@
final SimpleHttpServerConnection conn,
final SimpleRequest request) throws IOException {
- RequestLine oldreqline = request.getRequestLine();
- URI uri = new URI(oldreqline.getUri(), true);
- SimpleHost host = new SimpleHost(uri.getHost(), uri.getPort());
- SimpleHttpServerConnection proxyconn = this.connmanager.openConnection(host);
- proxyconn.setSocketTimeout(0);
+ RequestLine oldreqline = request.getRequestLine();
+ URI uri = null;
+ SimpleHost host = null;
try {
-
-
+ uri = new URI(oldreqline.getUri(), true);
+ host = new SimpleHost(uri.getHost(), uri.getPort());
+ } catch (URIException ex) {
+ SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_BAD_REQUEST);
+ conn.writeResponse(response);
+ return;
+ }
+ SimpleHttpServerConnection proxyconn = null;
+ try {
+ proxyconn = this.connmanager.openConnection(host);
+ } catch (UnknownHostException e) {
+ SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_NOT_FOUND);
+ conn.writeResponse(response);
+ return;
+ }
+ try {
+ proxyconn.setSocketTimeout(0);
// Rewrite target url
RequestLine newreqline = new RequestLine(
oldreqline.getMethod(),
@@ -103,6 +118,7 @@
if (response == null) {
return;
}
+ response.setHeader(new Header("Via", "1.1 test (Test-Proxy)"));
connheader = response.getFirstHeader("Connection");
if (connheader != null) {
String s = connheader.getValue();
Index: test/org/apache/commons/httpclient/TestNoHost.java
===================================================================
--- test/org/apache/commons/httpclient/TestNoHost.java (revision 169255)
+++ test/org/apache/commons/httpclient/TestNoHost.java (working copy)
@@ -89,6 +89,7 @@
// Preferences
suite.addTest(TestParamsAll.suite());
suite.addTest(TestVirtualHost.suite());
+ suite.addTest(TestHostConfiguration.suite());
// URIs
suite.addTest(TestURI.suite());
suite.addTest(TestURIUtil.suite());
Index: java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java
===================================================================
--- java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java (revision 169255)
+++ java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java (working copy)
@@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.InterruptedIOException;
+import java.net.UnknownHostException;
/**
* The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
@@ -98,6 +99,10 @@
// Timeout
return false;
}
+ if (exception instanceof UnknownHostException) {
+ // Unknown host
+ return false;
+ }
if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
// SSL handshake exception
return false;
Index: java/org/apache/commons/httpclient/HttpClient.java
===================================================================
--- java/org/apache/commons/httpclient/HttpClient.java (revision 169255)
+++ java/org/apache/commons/httpclient/HttpClient.java (working copy)
@@ -353,7 +353,7 @@
* {@link HostConfiguration host configuration} with the given custom
* {@link HttpState HTTP state}.
*
- * @param hostConfiguration The {@link HostConfiguration host configuration} to use.
+ * @param hostconfig The {@link HostConfiguration host configuration} to use.
* @param method the {@link HttpMethod HTTP method} to execute.
* @param state the {@link HttpState HTTP state} to use when executing the method.
* If null, the state returned by {@link #getState} will be used instead.
@@ -366,7 +366,7 @@
* cannot be recovered from.
* @since 2.0
*/
- public int executeMethod(HostConfiguration hostConfiguration,
+ public int executeMethod(HostConfiguration hostconfig,
final HttpMethod method, final HttpState state)
throws IOException, HttpException {
@@ -376,18 +376,21 @@
throw new IllegalArgumentException("HttpMethod parameter may not be null");
}
HostConfiguration defaulthostconfig = getHostConfiguration();
- if (hostConfiguration == null || hostConfiguration == defaulthostconfig) {
+ if (hostconfig == null) {
+ hostconfig = defaulthostconfig;
+ }
+ URI uri = method.getURI();
+ if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
// make a deep copy of the host defaults
- hostConfiguration = new HostConfiguration(defaulthostconfig);
- URI uri = method.getURI();
+ hostconfig = new HostConfiguration(hostconfig);
if (uri.isAbsoluteURI()) {
- hostConfiguration.setHost(uri);
+ hostconfig.setHost(uri);
}
}
HttpMethodDirector methodDirector = new HttpMethodDirector(
this.httpConnectionManager,
- hostConfiguration,
+ hostconfig,
this.params,
(state == null ? getState() : state));
methodDirector.executeMethod(method);
Index: test/org/apache/commons/httpclient/TestHostConfiguration.java
===================================================================
--- /dev/null 2005-05-11 16:18:31.216000000 +0200
+++ test/org/apache/commons/httpclient/TestHostConfiguration.java 2005-05-11 16:09:13.122107400 +0200
@@ -0,0 +1,162 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * ====================================================================
+ *
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+package org.apache.commons.httpclient;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.server.SimpleProxy;
+
+/**
+ * Tests basic HostConfiguration functionality.
+ *
+ * @author Oleg Kalnichevski
+ *
+ * @version $Id: $
+ */
+public class TestHostConfiguration extends HttpClientTestBase {
+
+ public TestHostConfiguration(final String testName) throws IOException {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestHostConfiguration.class);
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestHostConfiguration.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public void testRelativeURLHitWithDefaultHost() throws IOException {
+ this.server.setHttpService(new EchoService());
+ // Set default host
+ this.client.getHostConfiguration().setHost(
+ this.server.getLocalAddress(),
+ this.server.getLocalPort(),
+ Protocol.getProtocol("http"));
+
+ GetMethod httpget = new GetMethod("/test/");
+ try {
+ this.client.executeMethod(httpget);
+ assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
+ } finally {
+ httpget.releaseConnection();
+ }
+ }
+
+ public void testRelativeURLHitWithoutDefaultHost() throws IOException {
+ this.server.setHttpService(new EchoService());
+ // reset default host configuration
+ this.client.setHostConfiguration(new HostConfiguration());
+
+ GetMethod httpget = new GetMethod("/test/");
+ try {
+ this.client.executeMethod(httpget);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException expected) {
+ } finally {
+ httpget.releaseConnection();
+ }
+ }
+
+ public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
+ this.server.setHttpService(new EchoService());
+ // reset default host configuration
+ this.client.setHostConfiguration(new HostConfiguration());
+
+ GetMethod httpget = new GetMethod("http://" +
+ this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
+ try {
+ this.client.executeMethod(httpget);
+ assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
+ } finally {
+ httpget.releaseConnection();
+ }
+ }
+
+ public void testAbsoluteURLOverridesClientDefaultHost() throws IOException {
+ this.server.setHttpService(new EchoService());
+ // Somewhere out there in pampa
+ this.client.getHostConfiguration().setHost("somewhere.outthere.in.pampa", 9999);
+
+ GetMethod httpget = new GetMethod("http://" +
+ this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
+ try {
+ this.client.executeMethod(httpget);
+ assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
+ } finally {
+ httpget.releaseConnection();
+ }
+ httpget = new GetMethod("/test/");
+ try {
+ this.client.executeMethod(httpget);
+ fail("UnknownHostException should have been thrown");
+ } catch (UnknownHostException expected) {
+ } finally {
+ httpget.releaseConnection();
+ }
+ }
+
+ public void testAbsoluteURLOverridesDefaultHostParam() throws IOException {
+
+ this.proxy = new SimpleProxy();
+
+ this.server.setHttpService(new EchoService());
+ // reset default host configuration
+ HostConfiguration hostconfig = new HostConfiguration();
+ hostconfig.setHost("somehwere.outthere.in.pampa", 9999);
+ hostconfig.setProxy(
+ this.proxy.getLocalAddress(),
+ this.proxy.getLocalPort());
+
+ GetMethod httpget = new GetMethod("http://" +
+ this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
+ try {
+ this.client.executeMethod(hostconfig, httpget);
+ assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
+ assertNotNull(httpget.getResponseHeader("Via"));
+ } finally {
+ httpget.releaseConnection();
+ }
+ httpget = new GetMethod("/test/");
+ try {
+ this.client.executeMethod(hostconfig, httpget);
+ assertEquals(HttpStatus.SC_NOT_FOUND, httpget.getStatusCode());
+ } finally {
+ httpget.releaseConnection();
+ }
+ }
+
+}