SchemePortResolver to throw UnsupportedSchemeException in case of an unknown protocol scheme

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1490251 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-06-06 12:16:53 +00:00
parent d04b7baa64
commit f983922157
5 changed files with 32 additions and 11 deletions

View File

@ -38,6 +38,6 @@ public interface SchemePortResolver {
/**
* Returns the actual port for the host based on the protocol scheme.
*/
int resolve(HttpHost host);
int resolve(HttpHost host) throws UnsupportedSchemeException;
}

View File

@ -33,6 +33,7 @@ import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthScheme;
import org.apache.http.client.AuthCache;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.UnsupportedSchemeException;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.util.Args;
@ -65,7 +66,12 @@ public class BasicAuthCache implements AuthCache {
protected HttpHost getKey(final HttpHost host) {
if (host.getPort() <= 0) {
final int port = schemePortResolver.resolve(host);
final int port;
try {
port = schemePortResolver.resolve(host);
} catch (final UnsupportedSchemeException ignore) {
return host;
}
return new HttpHost(host.getHostName(), port, host.getSchemeName());
} else {
return host;

View File

@ -36,6 +36,7 @@ import org.apache.http.annotation.Immutable;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.UnsupportedSchemeException;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.protocol.HttpContext;
@ -74,10 +75,14 @@ public class DefaultRoutePlanner implements HttpRoutePlanner {
HttpHost target;
if (host.getPort() <= 0) {
target = new HttpHost(
host.getHostName(),
this.schemePortResolver.resolve(host),
host.getSchemeName());
try {
target = new HttpHost(
host.getHostName(),
this.schemePortResolver.resolve(host),
host.getSchemeName());
} catch (final UnsupportedSchemeException ex) {
throw new HttpException(ex.getMessage());
}
} else {
target = host;
}

View File

@ -28,6 +28,7 @@ package org.apache.http.impl.conn;
import org.apache.http.HttpHost;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.UnsupportedSchemeException;
import org.apache.http.util.Args;
/**
@ -39,16 +40,19 @@ public class DefaultSchemePortResolver implements SchemePortResolver {
public static final DefaultSchemePortResolver INSTANCE = new DefaultSchemePortResolver();
public int resolve(final HttpHost host) {
public int resolve(final HttpHost host) throws UnsupportedSchemeException {
Args.notNull(host, "HTTP host");
final int port = host.getPort();
if (port > 0) {
return port;
} else {
if ("https".equalsIgnoreCase(host.getSchemeName())) {
final String name = host.getSchemeName();
if (name.equalsIgnoreCase("http")) {
return 80;
} else if (name.equalsIgnoreCase("https")) {
return 443;
} else {
return 80;
throw new UnsupportedSchemeException(name + " protocol is not supported");
}
}
}

View File

@ -631,14 +631,20 @@ public class TestRedirects extends IntegrationTestBase {
}
}
@Test(expected=IOException.class)
@Test(expected=ClientProtocolException.class)
public void testRejectBogusRedirectLocation() throws Exception {
final HttpHost target = getServerHttp();
this.localServer.register("*", new BogusRedirectService("xxx://bogus"));
final HttpGet httpget = new HttpGet("/oldlocation/");
this.httpclient.execute(target, httpget);
try {
this.httpclient.execute(target, httpget);
} catch (ClientProtocolException ex) {
Throwable cause = ex.getCause();
Assert.assertTrue(cause instanceof HttpException);
throw ex;
}
}
@Test(expected=ClientProtocolException.class)