You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2005/05/12 19:05:07 UTC

svn commit: r169849 - in /jakarta/commons/proper/httpclient/trunk/src: java/org/apache/commons/httpclient/ test/org/apache/commons/httpclient/ test/org/apache/commons/httpclient/server/

Author: olegk
Date: Thu May 12 10:05:07 2005
New Revision: 169849

URL: http://svn.apache.org/viewcvs?rev=169849&view=rev
Log:
PR #34780 (HttpClient should always override the host of HostConfiguration if an absolute request URI is given)

Contributed by Oleg Kalnichevski
Reviewed by Ortwin Glück

Added:
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java   (with props)
Modified:
    jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java
    jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpClient.java
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java

Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java?rev=169849&r1=169848&r2=169849&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java Thu May 12 10:05:07 2005
@@ -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.
@@ -96,6 +97,10 @@
         }
         if (exception instanceof InterruptedIOException) {
             // Timeout
+            return false;
+        }
+        if (exception instanceof UnknownHostException) {
+            // Unknown host
             return false;
         }
         if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {

Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpClient.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpClient.java?rev=169849&r1=169848&r2=169849&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpClient.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpClient.java Thu May 12 10:05:07 2005
@@ -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 <code>null</code>, 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);

Added: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java?rev=169849&view=auto
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java (added)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java Thu May 12 10:05:07 2005
@@ -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
+ * <http://www.apache.org/>.
+ */
+
+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();
+        }
+    }
+
+}

Propchange: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java?rev=169849&r1=169848&r2=169849&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java Thu May 12 10:05:07 2005
@@ -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;
@@ -123,71 +122,6 @@
             response.addHeader(new Header("Transfer-Encoding", "chunked")); 
             response.addHeader(new Header("Connection", "close"));            
             return true;
-        }
-    }
-
-    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();
         }
     }
 

Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java?rev=169849&r1=169848&r2=169849&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestNoHost.java Thu May 12 10:05:07 2005
@@ -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());

Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java?rev=169849&r1=169848&r2=169849&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java Thu May 12 10:05:07 2005
@@ -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(); 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org