public static HttpHost normalize(final HttpHost host, final SchemePortResolver schemePortResolver) { if (host == null) { return null; } if (host.getPort() < 0) { final int port = (schemePortResolver != null ? schemePortResolver: DefaultSchemePortResolver.INSTANCE).resolve(host); if (port > 0) { return new HttpHost(host.getSchemeName(), host.getHostName(), port); } } return host; }
@Override public String toString() { return toURI(); }
/** * @since 4.4 */ public Request viaProxy(final String proxy) { try { this.proxy = HttpHost.create(proxy); } catch (final URISyntaxException e) { throw new IllegalArgumentException("Invalid host"); } return this; }
buf.append(target.getSchemeName()).append("://"); buf.append(target.getHostName()); if (target.getPort() >= 0) { buf.append(":").append(target.getPort());
@Before public void setUp() throws Exception { defaultHost = new HttpHost("foo.example.com"); mockEntry = mock(HttpCacheEntry.class); mockRequest = mock(HttpRequest.class); extractor = CacheKeyGenerator.INSTANCE; }
@Override public boolean upgrade( final TransportSecurityLayer tlsSession, final HttpHost host, final SocketAddress localAddress, final SocketAddress remoteAddress, final Object attachment) { final String scheme = host != null ? host.getSchemeName() : null; if (URIScheme.HTTPS.same(scheme)) { tlsSession.startTls(sslContext, host, sslBufferMode, initializer, verifier); return true; } return false; }
@Override public void upgrade( final ManagedHttpClientConnection conn, final HttpHost host, final HttpContext context) throws IOException { final HttpClientContext clientContext = HttpClientContext.adapt(context); final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(clientContext); final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported"); } if (!(sf instanceof LayeredConnectionSocketFactory)) { throw new UnsupportedSchemeException(host.getSchemeName() + " protocol does not support connection upgrade"); } final LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory) sf; Socket sock = conn.getSocket(); if (sock == null) { throw new ConnectionClosedException("Connection is closed"); } final int port = this.schemePortResolver.resolve(host); sock = lsf.createLayeredSocket(sock, host.getHostName(), port, context); conn.bind(sock); }
hostName = route.getTargetHost().getHostName(); port = route.getTargetHost().getPort();
Args.notNull(context, "Context"); final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(context); final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported"); final InetAddress[] addresses = host.getAddress() != null ? new InetAddress[] { host.getAddress() } : this.dnsResolver.resolve(host.getHostName()); final int port = this.schemePortResolver.resolve(host); for (int i = 0; i < addresses.length; i++) {
@Override public int resolve(final HttpHost host) { Args.notNull(host, "HTTP host"); final int port = host.getPort(); if (port > 0) { return port; } final String name = host.getSchemeName(); if (URIScheme.HTTP.same(name)) { return 80; } else if (URIScheme.HTTPS.same(name)) { return 443; } else { return -1; } }
final ComplexFuture<ManagedAsyncClientConnection> future = new ComplexFuture<>(callback); final HttpHost remoteEndpoint = RoutingSupport.normalize(host, schemePortResolver); final InetAddress remoteAddress = host.getAddress(); final TlsStrategy tlsStrategy = tlsStrategyLookup != null ? tlsStrategyLookup.lookup(host.getSchemeName()) : null; final Future<IOSession> sessionFuture = sessionRequester.connect( connectionInitiator, remoteEndpoint, remoteAddress != null ? new InetSocketAddress(remoteAddress, remoteEndpoint.getPort()) : null, localAddress, connectTimeout,
@Override protected HttpHost determineProxy(final HttpHost target, final HttpContext context) throws HttpException { final URI targetURI; try { targetURI = new URI(target.toURI()); } catch (final URISyntaxException ex) { throw new HttpException("Cannot convert host to URI: " + target, ex); } ProxySelector proxySelectorInstance = this.proxySelector; if (proxySelectorInstance == null) { proxySelectorInstance = ProxySelector.getDefault(); } if (proxySelectorInstance == null) { //The proxy selector can be "unset", so we must be able to deal with a null selector return null; } final List<Proxy> proxies = proxySelectorInstance.select(targetURI); final Proxy p = chooseProxy(proxies); HttpHost result = null; if (p.type() == Proxy.Type.HTTP) { // convert the socket address to an HttpHost if (!(p.address() instanceof InetSocketAddress)) { throw new HttpException("Unable to handle non-Inet proxy address: " + p.address()); } final InetSocketAddress isa = (InetSocketAddress) p.address(); // assume default scheme (http) result = new HttpHost(null, isa.getAddress(), isa.getHostString(), isa.getPort()); } return result; }
@Test public void testCreateFromString() throws Exception { Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("https://somehost:8080")); Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("HttpS://SomeHost:8080")); Assert.assertEquals(new HttpHost(null, "somehost", 1234), HttpHost.create("somehost:1234")); Assert.assertEquals(new HttpHost(null, "somehost", -1), HttpHost.create("somehost")); }
@Test public void testToHostString() { final HttpHost host1 = new HttpHost("somehost"); Assert.assertEquals("somehost", host1.toHostString()); final HttpHost host2 = new HttpHost("somehost"); Assert.assertEquals("somehost", host2.toHostString()); final HttpHost host3 = new HttpHost("somehost", -1); Assert.assertEquals("somehost", host3.toHostString()); final HttpHost host4 = new HttpHost("somehost", 8888); Assert.assertEquals("somehost:8888", host4.toHostString()); }
final RouteInfo route = clientContext.getHttpRoute(); if (route != null) { spn = "HTTP/" + route.getProxyHost().getHostName(); } else { final RouteInfo route = clientContext.getHttpRoute(); if (route != null) { spn = "HTTP/" + route.getTargetHost().getHostName(); } else {
@Test public void testEquals() throws Exception { final HttpHost host1 = new HttpHost("http", "somehost", 8080); final HttpHost host2 = new HttpHost("http", "somehost", 80); final HttpHost host3 = new HttpHost("http", "someotherhost", 8080); final HttpHost host4 = new HttpHost("http", "somehost", 80); final HttpHost host5 = new HttpHost("http", "SomeHost", 80); final HttpHost host6 = new HttpHost("myhttp", "SomeHost", 80); final HttpHost host7 = new HttpHost( "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80); final HttpHost host8 = new HttpHost("http", "127.0.0.1", 80); final HttpHost host9 = new HttpHost( "http", InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80); final HttpHost host10 = new HttpHost( "http", InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80); final HttpHost host11 = new HttpHost( "http", InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80); Assert.assertTrue(host1.equals(host1)); Assert.assertFalse(host1.equals(host2)); Assert.assertFalse(host1.equals(host3)); Assert.assertTrue(host2.equals(host4)); Assert.assertTrue(host2.equals(host5)); Assert.assertFalse(host5.equals(host6)); Assert.assertFalse(host7.equals(host8)); Assert.assertTrue(!host7.equals(host9)); Assert.assertFalse(host1.equals(null)); Assert.assertFalse(host1.equals("http://somehost")); Assert.assertFalse(host9.equals("http://somehost")); Assert.assertFalse(host8.equals(host9));
private HttpRoute(final HttpHost targetHost, final InetAddress local, final List<HttpHost> proxies, final boolean secure, final TunnelType tunnelled, final LayerType layered) { Args.notNull(targetHost, "Target host"); Args.notNegative(targetHost.getPort(), "Target port"); this.targetHost = targetHost; this.localAddress = local; if (proxies != null && !proxies.isEmpty()) { this.proxyChain = new ArrayList<>(proxies); } else { this.proxyChain = null; } if (tunnelled == TunnelType.TUNNELLED) { Args.check(this.proxyChain != null, "Proxy required if tunnelled"); } this.secure = secure; this.tunnelled = tunnelled != null ? tunnelled : TunnelType.PLAIN; this.layered = layered != null ? layered : LayerType.PLAIN; }
if (ex instanceof NoHttpResponseException) { final NoHttpResponseException updatedex = new NoHttpResponseException( route.getTargetHost().toHostString() + " failed to respond"); updatedex.setStackTrace(ex.getStackTrace()); throw updatedex;
return UNREACHABLE; if (!plan.getTargetHost().equals(fact.getTargetHost()))