protected HttpHost getKey(final HttpHost host) { if (host.getPort() <= 0) { 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; } }
/** * Obtains the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} * parameter value. * {@link #NO_HOST} will be mapped to {@code null}, * to allow unsetting in a hierarchy. * * @param params the parameters in which to look up * * @return the default proxy set in the argument parameters, or * {@code null} if not set */ public static HttpHost getDefaultProxy(final HttpParams params) { Args.notNull(params, "Parameters"); HttpHost proxy = (HttpHost) params.getParameter(DEFAULT_PROXY); if ((proxy != null) && NO_HOST.equals(proxy)) { // value is explicitly unset proxy = null; } return proxy; }
/** * Creates a HttpHostConnectException based on original {@link java.io.IOException}. * * @since 4.3 */ public HttpHostConnectException( final IOException cause, final HttpHost host, final InetAddress... remoteAddresses) { super("Connect to " + (host != null ? host.toHostString() : "remote host") + (remoteAddresses != null && remoteAddresses .length > 0 ? " " + Arrays.asList(remoteAddresses) : "") + ((cause != null && cause.getMessage() != null) ? " failed: " + cause.getMessage() : " refused")); this.host = host; initCause(cause); }
/** * Defines auth scope for a specific host of origin. * * @param origin host of origin * @param realm authentication realm. May be {@link #ANY_REALM} if applies * to any realm on the host. * @param schemeName authentication scheme. May be {@link #ANY_SCHEME} if applies * to any scheme supported by the host. * * @since 4.2 */ public AuthScope( final HttpHost origin, final String realm, final String schemeName) { Args.notNull(origin, "Host"); this.host = origin.getHostName().toLowerCase(Locale.ROOT); this.port = origin.getPort() < 0 ? ANY_PORT : origin.getPort(); this.realm = realm == null ? ANY_REALM : realm; this.scheme = schemeName == null ? ANY_SCHEME : schemeName.toUpperCase(Locale.ROOT); this.origin = origin; }
@Override public int resolve(final HttpHost host) throws UnsupportedSchemeException { Args.notNull(host, "HTTP host"); final int port = host.getPort(); if (port > 0) { return port; } final String name = host.getSchemeName(); if (name.equalsIgnoreCase("http")) { return 80; } else if (name.equalsIgnoreCase("https")) { return 443; } else { throw new UnsupportedSchemeException(name + " protocol is not supported"); } }
final String host = target.getHostName(); int port = target.getPort(); if (port < 0) { final Scheme scheme = connManager.getSchemeRegistry(). getScheme(target.getSchemeName()); port = scheme.getDefaultPort();
@Override public String toString() { return this.httphost.getHostName() + ":" + getPort(); }
public HttpResponse execute(final HttpUriRequest request, final HttpContext context) throws IOException { final URI uri = request.getURI(); final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); return execute(httpHost, request, context); }
final HttpContext context) throws IOException { 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 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(); final int port = this.schemePortResolver.resolve(host); sock = lsf.createLayeredSocket(sock, host.getHostName(), port, context); conn.bind(sock); }
@Override protected HttpHost determineProxy( final HttpHost target, final HttpRequest request, 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); } final List<Proxy> proxies = this.proxySelector.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(getHost(isa), isa.getPort()); } return result; }
final int port = ((HttpInetConnection) conn).getRemotePort(); if (address != null) { targethost = new HttpHost(address.getHostName(), port); request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
/** * Obtains the scheme for a host. * Convenience method for {@code getScheme(host.getSchemeName())} * * @param host the host for which to obtain the scheme * * @return the scheme for the given host, never {@code null} * * @throws IllegalStateException * if a scheme with the respective name is not registered */ public final Scheme getScheme(final HttpHost host) { Args.notNull(host, "Host"); return getScheme(host.getSchemeName()); }
@Override public String toString() { return toURI(); }
/** * For a given {@link HttpHost} and {@link HttpRequest} get a URI from the * pair that I can use as an identifier KEY into my HttpCache * * @param host The host for this request * @param req the {@link HttpRequest} * @return String the extracted URI */ public String getURI(final HttpHost host, final HttpRequest req) { if (isRelativeRequest(req)) { return canonicalizeUri(String.format("%s%s", host.toString(), req.getRequestLine().getUri())); } return canonicalizeUri(req.getRequestLine().getUri()); }
@Override public HttpClientConnection create(final HttpHost host) throws IOException { final String scheme = host.getSchemeName(); Socket socket = null; if ("http".equalsIgnoreCase(scheme)) { throw new IOException(scheme + " scheme is not supported"); final String hostname = host.getHostName(); int port = host.getPort(); if (port == -1) { if (host.getSchemeName().equalsIgnoreCase("http")) { port = 80; } else if (host.getSchemeName().equalsIgnoreCase("https")) { port = 443;
@Override public Socket connectSocket(int timeout, Socket plain, HttpHost host, InetSocketAddress remoteAddr, InetSocketAddress localAddr, HttpContext context) throws IOException { MyLog.d(this, "Preparing direct SSL connection (without proxy) to " + host); // we'll rather use an SSLSocket directly plain.close(); // create a plain SSL socket, but don't do hostname/certificate verification yet SSLSocket ssl = (SSLSocket)sslSocketFactory.createSocket(remoteAddr.getAddress(), host.getPort()); // connect, set SNI, shake hands, verify, print connection info connectWithSNI(ssl, host.getHostName()); return ssl; }
verifyHostname(sslsock, host.getHostName()); return sock; } else { return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
/** * Creates {@code HttpHost} instance from string. Text may not contain any blanks. * * @since 4.4 */ public static HttpHost create(final String s) { Args.containsNoBlanks(s, "HTTP Host"); String text = s; String scheme = null; final int schemeIdx = text.indexOf("://"); if (schemeIdx > 0) { scheme = text.substring(0, schemeIdx); text = text.substring(schemeIdx + 3); } int port = -1; final int portIdx = text.lastIndexOf(":"); if (portIdx > 0) { try { port = Integer.parseInt(text.substring(portIdx + 1)); } catch (NumberFormatException ex) { throw new IllegalArgumentException("Invalid HTTP host: " + text); } text = text.substring(0, portIdx); } return new HttpHost(text, port, scheme); }