diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 95fd9f9ce4c..2280d3c299e 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -444,8 +444,7 @@ public class HttpClient extends ContainerLifeCycle protected HttpDestination provideDestination(String scheme, String host, int port) { - if (port <= 0) - port = "https".equalsIgnoreCase(scheme) ? 443 : 80; + port = normalizePort(scheme, port); String address = address(scheme, host, port); HttpDestination destination = destinations.get(address); @@ -871,6 +870,12 @@ public class HttpClient extends ContainerLifeCycle return encodingField; } + protected int normalizePort(String scheme, int port) + { + return port > 0 ? port : + "https".equalsIgnoreCase(scheme) ? 443 : 80; + } + protected HttpConnection newHttpConnection(HttpClient httpClient, EndPoint endPoint, HttpDestination destination) { return new HttpConnection(httpClient, endPoint, destination); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java index c30ecb58a36..4463d519bdf 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java @@ -88,11 +88,7 @@ public class HttpDestination implements Destination, AutoCloseable, Dumpable proxyAddress = proxyConfig != null && proxyConfig.matches(host, port) ? new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()) : null; - String hostValue = host; - if ("https".equalsIgnoreCase(scheme) && port != 443 || - "http".equalsIgnoreCase(scheme) && port != 80) - hostValue += ":" + port; - hostField = new HttpField(HttpHeader.HOST, hostValue); + hostField = new HttpField(HttpHeader.HOST, host + ":" + port); } protected BlockingQueue getIdleConnections() diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java index 7307895442c..c0c320eb255 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java @@ -79,10 +79,10 @@ public class HttpRequest implements Request { this.client = client; this.conversation = conversation; - scheme(uri.getScheme()); + scheme = uri.getScheme(); host = uri.getHost(); - port = uri.getPort(); - path(uri.getPath()); + port = client.normalizePort(scheme, uri.getPort()); + path = uri.getPath(); String query = uri.getRawQuery(); if (query != null) { @@ -482,7 +482,11 @@ public class HttpRequest implements Request private URI buildURI() { - return URI.create(getScheme() + "://" + getHost() + ":" + getPort() + getPath()); + String path = getPath(); + URI result = URI.create(path); + if (!result.isAbsolute()) + result = URI.create(getScheme() + "://" + getHost() + ":" + getPort() + path); + return result; } @Override diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ExternalSiteTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ExternalSiteTest.java index 67c538d683e..c5982b4a344 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ExternalSiteTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ExternalSiteTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.net.Socket; +import java.net.URI; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -69,17 +70,32 @@ public class ExternalSiteTest Assume.assumeNoException(x); } - final CountDownLatch latch = new CountDownLatch(1); + final CountDownLatch latch1 = new CountDownLatch(1); client.newRequest(host, port).send(new Response.CompleteListener() { @Override public void onComplete(Result result) { if (!result.isFailed() && result.getResponse().getStatus() == 200) - latch.countDown(); + latch1.countDown(); } }); + Assert.assertTrue(latch1.await(10, TimeUnit.SECONDS)); - Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); + // Try again the same URI, but without specifying the port + final CountDownLatch latch2 = new CountDownLatch(1); + client.newRequest("http://" + host).send(new Response.CompleteListener() + { + @Override + public void onComplete(Result result) + { + Assert.assertTrue(result.isSucceeded()); + Assert.assertEquals(200, result.getResponse().getStatus()); + URI uri = result.getRequest().getURI(); + Assert.assertTrue(uri.getPort() > 0); + latch2.countDown(); + } + }); + Assert.assertTrue(latch2.await(10, TimeUnit.SECONDS)); } }