HTTPCLIENT-1575: Disallow HttpRoute instances with default (-1) port

Contributed by Joseph Walton <joe at kafsemo.org>

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1638138 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-11-11 15:09:48 +00:00
parent ae5eb31ed7
commit 1964e9530a
17 changed files with 44 additions and 37 deletions

View File

@ -79,7 +79,7 @@ public abstract class AbstractProtocolTest {
@Before
public void setUp() {
host = new HttpHost("foo.example.com");
host = new HttpHost("foo.example.com", 80);
route = new HttpRoute(host);

View File

@ -62,7 +62,7 @@ public class TestAsynchronousValidationRequest {
public void setUp() {
mockParent = EasyMock.createNiceMock(AsynchronousValidator.class);
mockClient = EasyMock.createNiceMock(CachingExec.class);
route = new HttpRoute(new HttpHost("foo.example.com"));
route = new HttpRoute(new HttpHost("foo.example.com", 80));
request = HttpRequestWrapper.wrap(new HttpGet("/"));
context = HttpClientContext.create();
mockExecAware = EasyMock.createNiceMock(HttpExecutionAware.class);

View File

@ -64,7 +64,7 @@ public class TestAsynchronousValidator {
@Before
public void setUp() {
mockClient = EasyMock.createNiceMock(CachingExec.class);
route = new HttpRoute(new HttpHost("foo.example.com"));
route = new HttpRoute(new HttpHost("foo.example.com", 80));
request = HttpRequestWrapper.wrap(new HttpGet("/"));
context = HttpClientContext.create();
context.setTargetHost(new HttpHost("foo.example.com"));

View File

@ -139,7 +139,7 @@ public abstract class TestCachingExecChain {
config = CacheConfig.DEFAULT;
asyncValidator = new AsynchronousValidator(config);
host = new HttpHost("foo.example.com");
host = new HttpHost("foo.example.com", 80);
route = new HttpRoute(host);
request = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/stuff",
HttpVersion.HTTP_1_1));

View File

@ -164,7 +164,7 @@ public class TestExponentialBackingOffSchedulingStrategy {
final ClientExecChain clientExecChain = EasyMock.createNiceMock(ClientExecChain.class);
final CachingExec cachingHttpClient = new CachingExec(clientExecChain);
final AsynchronousValidator mockValidator = new AsynchronousValidator(impl);
final HttpRoute httpRoute = new HttpRoute(new HttpHost("foo.example.com"));
final HttpRoute httpRoute = new HttpRoute(new HttpHost("foo.example.com", 80));
final HttpRequestWrapper httpRequestWrapper = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/"));
final HttpClientContext httpClientContext = new HttpClientContext();
return new AsynchronousValidationRequest(mockValidator, cachingHttpClient, httpRoute, httpRequestWrapper,

View File

@ -90,7 +90,7 @@ public class TestHttpCacheJiraNumber1147 {
final ClientExecChain backend = EasyMock.createNiceMock(ClientExecChain.class);
final HttpRequestWrapper get = HttpRequestWrapper.wrap(new HttpGet("http://somehost/"));
final HttpClientContext context = HttpClientContext.create();
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
context.setTargetHost(target);

View File

@ -93,7 +93,7 @@ public class TestProtocolDeviations {
@Before
public void setUp() {
host = new HttpHost("foo.example.com");
host = new HttpHost("foo.example.com", 80);
route = new HttpRoute(host);

View File

@ -4806,7 +4806,7 @@ public class TestProtocolRequirements extends AbstractProtocolTest {
protected void testUnsafeMethodDoesNotInvalidateCacheForHeaderUri(
final HttpRequestWrapper unsafeReq) throws Exception, IOException {
final HttpHost otherHost = new HttpHost("bar.example.com");
final HttpHost otherHost = new HttpHost("bar.example.com", 80);
final HttpRoute otherRoute = new HttpRoute(otherHost);
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/content", HttpVersion.HTTP_1_1));

View File

@ -71,6 +71,7 @@ public final class HttpRoute implements RouteInfo, Cloneable {
private HttpRoute(final HttpHost target, final InetAddress local, final List<HttpHost> proxies,
final boolean secure, final TunnelType tunnelled, final LayerType layered) {
Args.notNull(target, "Target host");
Args.check(target.getPort() != -1, "target host port must be specified");
this.targetHost = target;
this.localAddress = local;
if (proxies != null && !proxies.isEmpty()) {

View File

@ -45,7 +45,7 @@ public class TestHttpRoute {
// a selection of constants for generating routes
public final static
HttpHost TARGET1 = new HttpHost("target1.test.invalid");
HttpHost TARGET1 = new HttpHost("target1.test.invalid", 80);
public final static
HttpHost TARGET2 = new HttpHost("target2.test.invalid", 8080);
// It is not necessary to have extra targets for https.
@ -577,4 +577,10 @@ public class TestHttpRoute {
Assert.assertEquals("route was modified", route3, route1);
}
@Test(expected = IllegalArgumentException.class)
public void testFailOnConstructionWithoutExplicitTargetPort() {
final HttpHost target = new HttpHost("somehost", -1, "https");
new HttpRoute(target);
}
}

View File

@ -42,7 +42,7 @@ public class TestRouteDirector {
// a selection of constants for generating routes
public final static
HttpHost TARGET1 = new HttpHost("target1.test.invalid");
HttpHost TARGET1 = new HttpHost("target1.test.invalid", 80);
public final static
HttpHost TARGET2 = new HttpHost("target2.test.invalid", 8080);
// It is not necessary to have extra targets for https.
@ -50,7 +50,7 @@ public class TestRouteDirector {
// for routes, they will not be determined from the scheme.
public final static
HttpHost PROXY1 = new HttpHost("proxy1.test.invalid");
HttpHost PROXY1 = new HttpHost("proxy1.test.invalid", 80);
public final static
HttpHost PROXY2 = new HttpHost("proxy2.test.invalid", 1080);
public final static

View File

@ -45,7 +45,7 @@ public class TestRouteTracker {
// a selection of constants for generating routes
public final static
HttpHost TARGET1 = new HttpHost("target1.test.invalid");
HttpHost TARGET1 = new HttpHost("target1.test.invalid", 80);
public final static
HttpHost TARGET2 = new HttpHost("target2.test.invalid", 8080);
// It is not necessary to have extra targets for https.
@ -53,7 +53,7 @@ public class TestRouteTracker {
// for routes, they will not be determined from the scheme.
public final static
HttpHost PROXY1 = new HttpHost("proxy1.test.invalid");
HttpHost PROXY1 = new HttpHost("proxy1.test.invalid", 80);
public final static
HttpHost PROXY2 = new HttpHost("proxy2.test.invalid", 1080);
public final static

View File

@ -47,7 +47,7 @@ public class TestAIMDBackoffManager {
@Before
public void setUp() {
connPerRoute = new MockConnPoolControl();
route = new HttpRoute(new HttpHost("localhost:80"));
route = new HttpRoute(new HttpHost("localhost", 80));
clock = new MockClock();
impl = new AIMDBackoffManager(connPerRoute, clock);
impl.setPerHostConnectionCap(10);

View File

@ -94,7 +94,7 @@ public class TestInternalHttpClient {
@Test
public void testExecute() throws Exception {
final HttpGet httpget = new HttpGet("http://somehost/stuff");
final HttpRoute route = new HttpRoute(new HttpHost("somehost"));
final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
final ArgumentCaptor<HttpRequestWrapper> argcap = ArgumentCaptor.forClass(HttpRequestWrapper.class);
Mockito.when(routePlanner.determineRoute(
@ -117,7 +117,7 @@ public class TestInternalHttpClient {
@Test(expected=ClientProtocolException.class)
public void testExecuteHttpException() throws Exception {
final HttpGet httpget = new HttpGet("http://somehost/stuff");
final HttpRoute route = new HttpRoute(new HttpHost("somehost"));
final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
Mockito.when(routePlanner.determineRoute(
Mockito.eq(new HttpHost("somehost")),

View File

@ -85,7 +85,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testLeaseReleaseNonReusable() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -112,7 +112,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testLeaseReleaseReusable() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
@ -142,7 +142,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testLeaseReleaseReusableWithState() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -173,7 +173,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testLeaseDifferentRoute() throws Exception {
final HttpHost target1 = new HttpHost("somehost");
final HttpHost target1 = new HttpHost("somehost", 80);
final HttpRoute route1 = new HttpRoute(target1);
Mockito.when(connFactory.create(
@ -193,7 +193,7 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertEquals(route1, mgr.getRoute());
Assert.assertEquals(null, mgr.getState());
final HttpHost target2 = new HttpHost("otherhost");
final HttpHost target2 = new HttpHost("otherhost", 80);
final HttpRoute route2 = new HttpRoute(target2);
final ConnectionRequest connRequest2 = mgr.requestConnection(route2, null);
final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
@ -209,7 +209,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testLeaseExpired() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -259,7 +259,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testShutdown() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -297,7 +297,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testCloseExpired() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -326,7 +326,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testCloseIdle() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -355,7 +355,7 @@ public class TestBasicHttpClientConnectionManager {
@Test(expected=IllegalStateException.class)
public void testAlreadyLeased() throws Exception {
final HttpHost target = new HttpHost("somehost");
final HttpHost target = new HttpHost("somehost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(
@ -372,7 +372,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testTargetConnect() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost target = new HttpHost("somehost", 443, "https");
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, true);
@ -415,7 +415,7 @@ public class TestBasicHttpClientConnectionManager {
@Test
public void testProxyConnectAndUpgrade() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost target = new HttpHost("somehost", 443, "https");
final HttpHost proxy = new HttpHost("someproxy", 8080);
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});

View File

@ -92,7 +92,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testLeaseRelease() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
@ -121,7 +121,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testReleaseRouteIncomplete() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
@ -149,7 +149,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test(expected=InterruptedException.class)
public void testLeaseFutureCancelled() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
@ -166,7 +166,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test(expected=ConnectionPoolTimeoutException.class)
public void testLeaseFutureTimeout() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
Mockito.when(future.isCancelled()).thenReturn(Boolean.TRUE);
@ -179,7 +179,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testReleaseReusable() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
final CPoolEntry entry = Mockito.spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
@ -205,7 +205,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testReleaseNonReusable() throws Exception {
final HttpHost target = new HttpHost("localhost");
final HttpHost target = new HttpHost("localhost", 80);
final HttpRoute route = new HttpRoute(target);
final CPoolEntry entry = Mockito.spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
@ -231,7 +231,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testTargetConnect() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost target = new HttpHost("somehost", 443, "https");
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, true);
@ -280,7 +280,7 @@ public class TestPoolingHttpClientConnectionManager {
@Test
public void testProxyConnectAndUpgrade() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost target = new HttpHost("somehost", 443, "https");
final HttpHost proxy = new HttpHost("someproxy", 8080);
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});

View File

@ -271,7 +271,7 @@ public class TestRedirectExec {
Mockito.when(httpRoutePlanner.determineRoute(
Mockito.eq(target),
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpClientContext>any())).thenReturn(new HttpRoute(new HttpHost("otherhost")));
Mockito.<HttpClientContext>any())).thenReturn(new HttpRoute(new HttpHost("otherhost", 80)));
redirectExec.execute(route, request, context, execAware);