hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1735768 - in /httpcomponents/httpcore/trunk: httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/ httpcore5/src/main/java/org/apache/hc/core5/http/ httpcore5/src/main/java/org/apache/hc/core5/http/message/ httpcore5/src/main/java...
Date Sat, 19 Mar 2016 13:04:52 GMT
Author: olegk
Date: Sat Mar 19 13:04:52 2016
New Revision: 1735768

URL: http://svn.apache.org/viewvc?rev=1735768&view=rev
Log:
RFC 7540: Request message to include host attribute (containing scheme and authority components)

Removed:
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/TestBasicRequest.java
Modified:
    httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/BenchmarkWorker.java
    httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/HttpBenchmark.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpCoreContext.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestTargetHost.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/integration/TestHttpAsyncHandlers.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/testserver/io/HttpClient.java

Modified: httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/BenchmarkWorker.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/BenchmarkWorker.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/BenchmarkWorker.java
Sat Mar 19 13:04:52 2016
@@ -71,20 +71,17 @@ class BenchmarkWorker implements Runnabl
     private final HttpRequestExecutor httpexecutor;
     private final ConnectionReuseStrategy connstrategy;
     private final HttpRequest request;
-    private final HttpHost targetHost;
     private final Config config;
     private final SocketFactory socketFactory;
     private final Stats stats = new Stats();
 
     public BenchmarkWorker(
             final HttpRequest request,
-            final HttpHost targetHost,
             final SocketFactory socketFactory,
             final Config config) {
         super();
         this.context = new HttpCoreContext();
         this.request = request;
-        this.targetHost = targetHost;
         this.config = config;
         final HttpProcessorBuilder builder = HttpProcessorBuilder.create()
                 .addAll(
@@ -108,6 +105,7 @@ class BenchmarkWorker implements Runnabl
         final HttpVersion version = config.isUseHttp1_0() ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
         final BenchmarkConnection conn = new BenchmarkConnection(8 * 1024, stats);
 
+        final HttpHost targetHost = this.request.getHost();
         final String scheme = targetHost.getSchemeName();
         final String hostname = targetHost.getHostName();
         int port = targetHost.getPort();
@@ -120,7 +118,6 @@ class BenchmarkWorker implements Runnabl
         }
 
         // Populate the execution context
-        context.setTargetHost(this.targetHost);
         context.setProtocolVersion(version);
 
         stats.start();

Modified: httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/HttpBenchmark.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/HttpBenchmark.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/HttpBenchmark.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-ab/src/main/java/org/apache/hc/core5/http/benchmark/HttpBenchmark.java
Sat Mar 19 13:04:52 2016
@@ -94,7 +94,7 @@ public class HttpBenchmark {
         this.config = config != null ? config : new Config();
     }
 
-    private HttpRequest createRequest() {
+    private HttpRequest createRequest(final HttpHost host) {
         final URL url = config.getUrl();
         HttpEntity entity = null;
 
@@ -152,6 +152,7 @@ public class HttpBenchmark {
         if (config.getSoapAction() != null && config.getSoapAction().length() >
0) {
             request.addHeader(new DefaultHeader("SOAPAction", config.getSoapAction()));
         }
+        request.setHost(host);
         return request;
     }
 
@@ -211,8 +212,7 @@ public class HttpBenchmark {
         final BenchmarkWorker[] workers = new BenchmarkWorker[config.getThreads()];
         for (int i = 0; i < workers.length; i++) {
             workers[i] = new BenchmarkWorker(
-                    createRequest(),
-                    host,
+                    createRequest(host),
                     socketFactory,
                     config);
             workerPool.execute(workers[i]);

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpRequest.java
Sat Mar 19 13:04:52 2016
@@ -50,4 +50,20 @@ public interface HttpRequest extends Htt
      */
     String getUri();
 
+    /**
+     * Obtains target host of this request message if available.
+     *
+     * @return  the target host or {@code null}.
+     *
+     * @since 5.0
+     */
+    HttpHost getHost();
+
+    /**
+     * Sets target host of this request message.
+     *
+     * @since 5.0
+     */
+    void setHost(HttpHost host);
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
Sat Mar 19 13:04:52 2016
@@ -27,10 +27,14 @@
 
 package org.apache.hc.core5.http.message;
 
+import java.net.URI;
+
 import org.apache.hc.core5.annotation.NotThreadSafe;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.ProtocolVersion;
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TextUtils;
 
 /**
  * Basic implementation of {@link HttpRequest}.
@@ -43,20 +47,65 @@ public class BasicHttpRequest extends Ab
     private static final long serialVersionUID = 1L;
 
     private final String method;
-    private String uri;
+    private HttpHost host;
+    private String requestUri;
     private ProtocolVersion version;
 
     /**
-     * Creates an instance of this class using the given request method
-     * and URI.
+     * Creates request message with the given method and request URI.
+     *
+     * @param method request method.
+     * @param requestUri request URI.
+     */
+    public BasicHttpRequest(final String method, final String requestUri) {
+        this(method, null, requestUri);
+    }
+
+    /**
+     * Creates request message with the given method, host and request URI.
      *
      * @param method request method.
-     * @param uri request URI.
+     * @param host request host.
+     * @param requestUri request URI.
+     *
+     * @since 5.0
      */
-    public BasicHttpRequest(final String method, final String uri) {
+    public BasicHttpRequest(final String method, final HttpHost host, final String requestUri)
{
         super();
         this.method = Args.notNull(method, "Method name");
-        this.uri = Args.notNull(uri, "Request URI");
+        this.host = host;
+        this.requestUri = Args.notNull(requestUri, "Request URI");
+    }
+
+    /**
+     * Creates request message with the given method, request URI.
+     *
+     * @param method request method.
+     * @param requestUri request URI.
+     *
+     * @since 5.0
+     */
+    public BasicHttpRequest(final String method, final URI requestUri) {
+        super();
+        this.method = Args.notNull(method, "Method name");
+        Args.notNull(requestUri, "Request URI");
+        if (requestUri.isAbsolute()) {
+            this.host = new HttpHost(requestUri.getHost(), requestUri.getPort(), requestUri.getScheme());
+        } else {
+            this.host = null;
+        }
+        final StringBuilder buf = new StringBuilder();
+        final String path = requestUri.getRawPath();
+        if (!TextUtils.isBlank(path)) {
+            buf.append(path);
+        } else {
+            buf.append("/");
+        }
+        final String query = requestUri.getRawQuery();
+        if (query != null) {
+            buf.append('?').append(query);
+        }
+        this.requestUri = buf.toString();
     }
 
     @Override
@@ -74,15 +123,32 @@ public class BasicHttpRequest extends Ab
         return this.method;
     }
 
+    /**
+     * @since 5.0
+     */
+    @Override
+    public HttpHost getHost() {
+        return this.host;
+    }
+
+    /**
+     * @since 5.0
+     */
+    @Override
+    public void setHost(final HttpHost host) {
+        this.host = host;
+    }
+
     @Override
     public String getUri() {
-        return this.uri;
+        return this.requestUri;
     }
 
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
-        sb.append(this.method).append(" ").append(this.uri).append(" ").append(super.toString());
+        sb.append(this.method).append(" ").append(this.host).append(" ").append(this.requestUri).append("
")
+                .append(super.toString());
         return sb.toString();
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpCoreContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpCoreContext.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpCoreContext.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpCoreContext.java
Sat Mar 19 13:04:52 2016
@@ -29,7 +29,6 @@ package org.apache.hc.core5.http.protoco
 
 import org.apache.hc.core5.annotation.NotThreadSafe;
 import org.apache.hc.core5.http.HttpConnection;
-import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.ProtocolVersion;
@@ -62,12 +61,6 @@ public class HttpCoreContext implements
      */
     public static final String HTTP_RESPONSE    = HttpContext.RESERVED_PREFIX + "response";
 
-    /**
-     * Attribute name of a {@link org.apache.hc.core5.http.HttpHost} object that
-     * represents the connection target.
-     */
-    public static final String HTTP_TARGET_HOST = "http.target_host";
-
     public static HttpCoreContext create() {
         return new HttpCoreContext(new BasicHttpContext());
     }
@@ -148,12 +141,4 @@ public class HttpCoreContext implements
         return getAttribute(HTTP_RESPONSE, HttpResponse.class);
     }
 
-    public void setTargetHost(final HttpHost host) {
-        setAttribute(HTTP_TARGET_HOST, host);
-    }
-
-    public HttpHost getTargetHost() {
-        return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
-    }
-
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestTargetHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestTargetHost.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestTargetHost.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestTargetHost.java
Sat Mar 19 13:04:52 2016
@@ -43,8 +43,8 @@ import org.apache.hc.core5.http.Protocol
 import org.apache.hc.core5.util.Args;
 
 /**
- * RequestTargetHost is responsible for adding {@code Host} header. This
- * interceptor is required for client side protocol processors.
+ * RequestHostOutgoing is responsible for adding {@code Host} header to the outgoing message.
+ * This interceptor is required for client side protocol processors.
  *
  * @since 4.0
  */
@@ -59,21 +59,20 @@ public class RequestTargetHost implement
     public void process(final HttpRequest request, final HttpContext context)
             throws HttpException, IOException {
         Args.notNull(request, "HTTP request");
+        Args.notNull(context, "HTTP context");
 
-        final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
-
-        final ProtocolVersion ver = coreContext.getProtocolVersion();
+        final ProtocolVersion ver = context.getProtocolVersion();
         final String method = request.getMethod();
         if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0))
{
-            extracted();
             return;
         }
 
         if (!request.containsHeader(HttpHeaders.HOST)) {
-            HttpHost targethost = coreContext.getTargetHost();
+            HttpHost targethost = request.getHost();
             if (targethost == null) {
                 // Populate the context with a default HTTP host based on the
                 // inet address of the target host
+                final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
                 final HttpConnection conn = coreContext.getConnection();
                 if (conn != null) {
                     final InetSocketAddress remoteAddress = (InetSocketAddress) conn.getRemoteAddress();
@@ -83,18 +82,13 @@ public class RequestTargetHost implement
                 }
                 if (targethost == null) {
                     if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
-                        extracted();
                         return;
                     }
-                    throw new ProtocolException("Target host missing");
+                    throw new ProtocolException("Target host is unknown");
                 }
             }
             request.addHeader(HttpHeaders.HOST, targethost.toHostString());
         }
     }
 
-    private void extracted() {
-        return;
-    }
-
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/RequestValidateHost.java
Sat Mar 19 13:04:52 2016
@@ -30,17 +30,21 @@ package org.apache.hc.core5.http.protoco
 import java.io.IOException;
 
 import org.apache.hc.core5.annotation.Immutable;
+import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpRequestInterceptor;
 import org.apache.hc.core5.http.HttpVersion;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.ProtocolVersion;
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TextUtils;
 
 /**
- * RequestValidateHost is responsible for validating {@code Host} header in HTTP/1.1 messages.
+ * RequestTargetHost is responsible for copying {@code Host} header value to
+ * {@link HttpRequest#setHost(HttpHost)} of the incoming message.
  * This interceptor is required for server side protocol processors.
  *
  * @since 5.0
@@ -56,6 +60,7 @@ public class RequestValidateHost impleme
     public void process(final HttpRequest request, final HttpContext context)
             throws HttpException, IOException {
         Args.notNull(request, "HTTP request");
+
         final ProtocolVersion version = request.getVersion() != null ? request.getVersion()
: HttpVersion.HTTP_1_1;
         if (version.greaterEquals(HttpVersion.HTTP_1_1)) {
             final int n = request.containsHeaders(HttpHeaders.HOST);
@@ -65,6 +70,24 @@ public class RequestValidateHost impleme
                 throw new ProtocolException("Multiple Host headers found");
             }
         }
+        final Header header = request.getFirstHeader(HttpHeaders.HOST);
+        if (header != null) {
+            String text = header.getValue();
+            if (TextUtils.isBlank(text)) {
+                throw new ProtocolException("Empty host header");
+            }
+            int port = -1;
+            final int portIdx = text.lastIndexOf(":");
+            if (portIdx > 0) {
+                try {
+                    port = Integer.parseInt(text.substring(portIdx + 1));
+                } catch (final NumberFormatException ex) {
+                    throw new ProtocolException("Invalid HTTP host: " + text);
+                }
+                text = text.substring(0, portIdx);
+            }
+            request.setHost(new HttpHost(text, port, null));
+        }
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/integration/TestHttpAsyncHandlers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/integration/TestHttpAsyncHandlers.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/integration/TestHttpAsyncHandlers.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/integration/TestHttpAsyncHandlers.java
Sat Mar 19 13:04:52 2016
@@ -66,10 +66,10 @@ import org.apache.hc.core5.http.nio.enti
 import org.apache.hc.core5.http.protocol.BasicHttpContext;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.ImmutableHttpProcessor;
+import org.apache.hc.core5.http.protocol.RequestTargetHost;
 import org.apache.hc.core5.http.protocol.RequestConnControl;
 import org.apache.hc.core5.http.protocol.RequestContent;
 import org.apache.hc.core5.http.protocol.RequestExpectContinue;
-import org.apache.hc.core5.http.protocol.RequestTargetHost;
 import org.apache.hc.core5.http.protocol.RequestUserAgent;
 import org.apache.hc.core5.http.testserver.nio.HttpCoreNIOTestBase;
 import org.apache.hc.core5.reactor.ListenerEndpoint;

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicMessages.java
Sat Mar 19 13:04:52 2016
@@ -27,7 +27,10 @@
 
 package org.apache.hc.core5.http.message;
 
+import java.net.URI;
+
 import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpStatus;
@@ -94,13 +97,13 @@ public class TestBasicMessages {
         Assert.assertEquals("/", request.getUri());
 
         try {
-            new BasicHttpRequest(null, null);
+            new BasicHttpRequest("GET", (String) null);
             Assert.fail("IllegalArgumentException should have been thrown");
         } catch (final IllegalArgumentException ex) {
             // expected
         }
         try {
-            new BasicHttpRequest("GET", null);
+            new BasicHttpRequest("GET", (URI) null);
             Assert.fail("IllegalArgumentException should have been thrown");
         } catch (final IllegalArgumentException ex) {
             // expected
@@ -108,6 +111,38 @@ public class TestBasicMessages {
     }
 
     @Test
+    public void testRequestBasics() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", "/stuff");
+        Assert.assertEquals("GET", request.getMethod());
+        Assert.assertEquals("/stuff", request.getUri());
+        Assert.assertEquals(null, request.getHost());
+    }
+
+    @Test
+    public void testRequestWithRelativeURI() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", new URI("/stuff"));
+        Assert.assertEquals("GET", request.getMethod());
+        Assert.assertEquals("/stuff", request.getUri());
+        Assert.assertEquals(null, request.getHost());
+    }
+
+    @Test
+    public void testRequestWithAbsoluteURI() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", new URI("https://host:9443/stuff?param=value"));
+        Assert.assertEquals("GET", request.getMethod());
+        Assert.assertEquals("/stuff?param=value", request.getUri());
+        Assert.assertEquals(new HttpHost("host", 9443, "https"), request.getHost());
+    }
+
+    @Test
+    public void testRequestWithNoPath() throws Exception {
+        final HttpRequest request = new BasicHttpRequest("GET", new URI("http://host"));
+        Assert.assertEquals("GET", request.getMethod());
+        Assert.assertEquals("/", request.getUri());
+        Assert.assertEquals(new HttpHost("host", -1, "http"), request.getHost());
+    }
+
+    @Test
     public void testSetRequestEntity() {
         final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
         Assert.assertNull(request.getEntity());

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestStandardInterceptors.java
Sat Mar 19 13:04:52 2016
@@ -384,8 +384,8 @@ public class TestStandardInterceptors {
     public void testRequestTargetHostGenerated() throws Exception {
         final HttpContext context = new BasicHttpContext(null);
         final HttpHost host = new HttpHost("somehost", 8080, "http");
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);
         final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        request.setHost(host);
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
         final Header header = request.getFirstHeader(HttpHeaders.HOST);
@@ -401,7 +401,6 @@ public class TestStandardInterceptors {
         Mockito.when(address.getHostName()).thenReturn("somehost");
         final HttpConnection conn = Mockito.mock(HttpConnection.class);
         Mockito.when(conn.getRemoteAddress()).thenReturn(new InetSocketAddress(address, 1234));
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, null);
         context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
@@ -416,7 +415,6 @@ public class TestStandardInterceptors {
         final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
         final HttpConnection conn = Mockito.mock(HttpConnection.class);
         Mockito.when(conn.getRemoteAddress()).thenReturn(null);
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, null);
         context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
@@ -426,8 +424,8 @@ public class TestStandardInterceptors {
     public void testRequestTargetHostNotGenerated() throws Exception {
         final HttpContext context = new BasicHttpContext(null);
         final HttpHost host = new HttpHost("somehost", 8080, "http");
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);
         final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        request.setHost(host);
         request.addHeader(new BasicHeader(HttpHeaders.HOST, "whatever"));
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
@@ -481,8 +479,8 @@ public class TestStandardInterceptors {
     public void testRequestTargetHostConnectHttp11() throws Exception {
         final HttpContext context = new BasicHttpContext(null);
         final HttpHost host = new HttpHost("somehost", 8080, "http");
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);
         final BasicHttpRequest request = new BasicHttpRequest("CONNECT", "/");
+        request.setHost(host);
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
         final Header header = request.getFirstHeader(HttpHeaders.HOST);
@@ -495,8 +493,8 @@ public class TestStandardInterceptors {
         final HttpContext context = new BasicHttpContext(null);
         context.setProtocolVersion(HttpVersion.HTTP_1_0);
         final HttpHost host = new HttpHost("somehost", 8080, "http");
-        context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);
         final BasicHttpRequest request = new BasicHttpRequest("CONNECT", "/");
+        request.setHost(host);
         final RequestTargetHost interceptor = new RequestTargetHost();
         interceptor.process(request, context);
         final Header header = request.getFirstHeader(HttpHeaders.HOST);
@@ -1064,6 +1062,28 @@ public class TestStandardInterceptors {
     }
 
     @Test
+    public void testRequestHttpHostHeader() throws Exception {
+        final HttpContext context = new BasicHttpContext(null);
+        final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        request.setVersion(HttpVersion.HTTP_1_1);
+        request.setHeader(HttpHeaders.HOST, "host:8888");
+        final RequestValidateHost interceptor = new RequestValidateHost();
+        interceptor.process(request, context);
+        Assert.assertEquals(new HttpHost("host", 8888), request.getHost());
+    }
+
+    @Test
+    public void testRequestHttpHostHeaderNoPort() throws Exception {
+        final HttpContext context = new BasicHttpContext(null);
+        final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        request.setVersion(HttpVersion.HTTP_1_1);
+        request.setHeader(HttpHeaders.HOST, "host");
+        final RequestValidateHost interceptor = new RequestValidateHost();
+        interceptor.process(request, context);
+        Assert.assertEquals(new HttpHost("host", -1), request.getHost());
+    }
+
+    @Test
     public void testRequestHttp11HostHeaderPresent() throws Exception {
         final HttpContext context = new BasicHttpContext(null);
         final BasicHttpRequest request = new BasicHttpRequest("GET", "/");

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/testserver/io/HttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/testserver/io/HttpClient.java?rev=1735768&r1=1735767&r2=1735768&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/testserver/io/HttpClient.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/testserver/io/HttpClient.java
Sat Mar 19 13:04:52 2016
@@ -103,7 +103,9 @@ public class HttpClient {
             final HttpRequest request,
             final HttpHost targetHost,
             final HttpClientConnection conn) throws HttpException, IOException {
-        this.context.setTargetHost(targetHost);
+        if (request.getHost() == null) {
+            request.setHost(targetHost);
+        }
         this.httpexecutor.preProcess(request, this.httpproc, this.context);
         final HttpResponse response = this.httpexecutor.execute(request, conn, this.context);
         this.httpexecutor.postProcess(response, this.httpproc, this.context);



Mime
View raw message