399319 - Request.getURI() may return negative ports.

This commit is contained in:
Simone Bordet 2013-01-28 23:03:09 +01:00
parent 1f35b72d54
commit 9ba1069383
4 changed files with 35 additions and 14 deletions

View File

@ -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);

View File

@ -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<Connection> getIdleConnections()

View File

@ -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

View File

@ -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));
}
}