String generateViaHeader(final HttpMessage msg) { if (msg.getVersion() == null) { msg.setVersion(HttpVersion.DEFAULT); } final ProtocolVersion pv = msg.getVersion(); final String existingEntry = viaHeaders.get(msg.getVersion()); if (existingEntry != null) { return existingEntry; } final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.hc.client5", getClass().getClassLoader()); final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE; final String value; final int major = pv.getMajor(); final int minor = pv.getMinor(); if (URIScheme.HTTP.same(pv.getProtocol())) { value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", major, minor, release); } else { value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), major, minor, release); } viaHeaders.put(pv, value); return value; }
/** * Returns normalized representation of the request URI optimized for use as a cache key. * This method ensures the resultant URI has an explicit port in the authority component, * and explicit path component and no fragment. * * @param requestUri original request URI * @return normalized URI. * @throws URISyntaxException */ public static URI normalize(final URI requestUri) throws URISyntaxException { Args.notNull(requestUri, "URI"); final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ; if (builder.getHost() != null) { if (builder.getScheme() == null) { builder.setScheme(URIScheme.HTTP.id); } if (builder.getPort() <= -1) { if (URIScheme.HTTP.same(builder.getScheme())) { builder.setPort(80); } else if (URIScheme.HTTPS.same(builder.getScheme())) { builder.setPort(443); } } } builder.setFragment(null); if (builder.getPath() == null) { builder.setPath("/"); } return builder.build(); }
private InetSocketAddress toSocketAddress(final HttpHost host) { int port = host.getPort(); if (port < 0) { final String scheme = host.getSchemeName(); if (URIScheme.HTTP.same(scheme)) { port = 80; } else if (URIScheme.HTTPS.same(scheme)) { port = 443; } } final String hostName = host.getHostName(); return new InetSocketAddress(hostName, port); }
private InetSocketAddress toSocketAddress(final HttpHost host) { int port = host.getPort(); if (port < 0) { final String scheme = host.getSchemeName(); if (URIScheme.HTTP.same(scheme)) { port = 80; } else if (URIScheme.HTTPS.same(scheme)) { port = 443; } } final String hostName = host.getHostName(); return new InetSocketAddress(hostName, port); }
@Override public InetSocketAddress resolve(final HttpHost host) { if (host == null) { return null; } int port = host.getPort(); if (port < 0) { final String scheme = host.getSchemeName(); if (URIScheme.HTTP.same(scheme)) { port = 80; } else if (URIScheme.HTTPS.same(scheme)) { port = 443; } } return new InetSocketAddress(host.getHostName(), port); }
@Override public InetSocketAddress resolve(final HttpHost host) { if (host == null) { return null; } int port = host.getPort(); if (port < 0) { final String scheme = host.getSchemeName(); if (URIScheme.HTTP.same(scheme)) { port = 80; } else if (URIScheme.HTTPS.same(scheme)) { port = 443; } } return new InetSocketAddress(host.getHostName(), port); }
@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; } }
@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 boolean upgrade( final TransportSecurityLayer tlsSession, final HttpHost host, final SocketAddress localAddress, final SocketAddress remoteAddress, final Object attachment, final Timeout handshakeTimeout) { final String scheme = host != null ? host.getSchemeName() : null; if (URIScheme.HTTPS.same(scheme)) { tlsSession.startTls(sslContext, host, sslBufferMode, initializer, verifier, handshakeTimeout); return true; } return false; }
@Override public boolean upgrade( final TransportSecurityLayer tlsSession, final HttpHost host, final SocketAddress localAddress, final SocketAddress remoteAddress, final Object attachment, final Timeout handshakeTimeout) { final String scheme = host != null ? host.getSchemeName() : null; if (URIScheme.HTTPS.same(scheme)) { tlsSession.startTls( sslContext, host, sslBufferMode, H2TlsSupport.enforceRequirements(attachment, initializer), verifier, handshakeTimeout); return true; } return false; }
private Socket createSocket(final HttpHost targetHost) throws IOException { final Socket sock = new Socket(); sock.setSoTimeout(socketConfig.getSoTimeout().toMillisIntBound()); sock.setReuseAddress(socketConfig.isSoReuseAddress()); sock.setTcpNoDelay(socketConfig.isTcpNoDelay()); sock.setKeepAlive(socketConfig.isSoKeepAlive()); if (socketConfig.getRcvBufSize() > 0) { sock.setReceiveBufferSize(socketConfig.getRcvBufSize()); } if (socketConfig.getSndBufSize() > 0) { sock.setSendBufferSize(socketConfig.getSndBufSize()); } final int linger = socketConfig.getSoLinger().toMillisIntBound(); if (linger >= 0) { sock.setSoLinger(true, linger); } final InetSocketAddress targetAddress = addressResolver.resolve(targetHost); sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisIntBound()); if (URIScheme.HTTPS.same(targetHost.getSchemeName())) { return sslSocketFactory.createSocket(sock, targetHost.getHostName(), targetAddress.getPort(), true); } return sock; }
private Socket createSocket(final HttpHost targetHost) throws IOException { final Socket sock = new Socket(); sock.setSoTimeout(socketConfig.getSoTimeout().toMillisIntBound()); sock.setReuseAddress(socketConfig.isSoReuseAddress()); sock.setTcpNoDelay(socketConfig.isTcpNoDelay()); sock.setKeepAlive(socketConfig.isSoKeepAlive()); if (socketConfig.getRcvBufSize() > 0) { sock.setReceiveBufferSize(socketConfig.getRcvBufSize()); } if (socketConfig.getSndBufSize() > 0) { sock.setSendBufferSize(socketConfig.getSndBufSize()); } final int linger = socketConfig.getSoLinger().toMillisIntBound(); if (linger >= 0) { sock.setSoLinger(true, linger); } final InetSocketAddress targetAddress = addressResolver.resolve(targetHost); sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisIntBound()); if (URIScheme.HTTPS.same(targetHost.getSchemeName())) { return sslSocketFactory.createSocket(sock, targetHost.getHostName(), targetAddress.getPort(), true); } return sock; }
String generateViaHeader(final HttpMessage msg) { if (msg.getVersion() == null) { msg.setVersion(HttpVersion.DEFAULT); } final ProtocolVersion pv = msg.getVersion(); final String existingEntry = viaHeaders.get(msg.getVersion()); if (existingEntry != null) { return existingEntry; } final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.hc.client5", getClass().getClassLoader()); final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE; final String value; final int major = pv.getMajor(); final int minor = pv.getMinor(); if (URIScheme.HTTP.same(pv.getProtocol())) { value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", major, minor, release); } else { value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), major, minor, release); } viaHeaders.put(pv, value); return value; }
@Override public boolean upgrade( final TransportSecurityLayer tlsSession, final HttpHost host, final SocketAddress localAddress, final SocketAddress remoteAddress, final Object attachment, final Timeout handshakeTimeout) { final String scheme = host != null ? host.getSchemeName() : null; if (URIScheme.HTTPS.same(scheme)) { tlsSession.startTls( sslContext, host, sslBufferMode, ConscryptSupport.initialize(attachment, initializer), ConscryptSupport.verify(verifier), handshakeTimeout); return true; } return false; }
@Override public void completed(final IOSession ioSession) { if (tlsStrategy != null && URIScheme.HTTPS.same(namedEndpoint.getSchemeName()) && ioSession instanceof TransportSecurityLayer) { tlsStrategy.upgrade( (TransportSecurityLayer) ioSession, namedEndpoint, ioSession.getLocalAddress(), ioSession.getRemoteAddress(), null, connectTimeout); ioSession.setSocketTimeout(connectTimeout); } callback.completed(ioSession); }
@Override public ClientHttpProtocolNegotiator createHandler(final ProtocolIOSession ioSession, final Object attachment) { if (tlsStrategy != null && ioSession.getInitialEndpoint() instanceof HttpHost) { final HttpHost host = (HttpHost) ioSession.getInitialEndpoint(); if (URIScheme.HTTPS.same(host.getSchemeName())) { tlsStrategy.upgrade( ioSession, host, ioSession.getLocalAddress(), ioSession.getRemoteAddress(), attachment, handshakeTimeout); } } return new ClientHttpProtocolNegotiator( ioSession, http1StreamHandlerFactory, http2StreamHandlerFactory, attachment instanceof HttpVersionPolicy ? (HttpVersionPolicy) attachment : versionPolicy); }
@Override public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) { if (tlsStrategy != null && ioSession.getInitialEndpoint() instanceof HttpHost) { final HttpHost host = (HttpHost) ioSession.getInitialEndpoint(); if (URIScheme.HTTPS.same(host.getSchemeName())) { tlsStrategy.upgrade( ioSession, host, ioSession.getLocalAddress(), ioSession.getRemoteAddress(), attachment); } } return new ClientHttp1IOEventHandler(streamDuplexerFactory.create(ioSession)); }
/** * Returns normalized representation of the request URI optimized for use as a cache key. * This method ensures the resultant URI has an explicit port in the authority component, * and explicit path component and no fragment. * * @param requestUri original request URI * @return normalized URI. * @throws URISyntaxException */ public static URI normalize(final URI requestUri) throws URISyntaxException { Args.notNull(requestUri, "URI"); final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ; if (builder.getHost() != null) { if (builder.getScheme() == null) { builder.setScheme(URIScheme.HTTP.id); } if (builder.getPort() <= -1) { if (URIScheme.HTTP.same(builder.getScheme())) { builder.setPort(80); } else if (URIScheme.HTTPS.same(builder.getScheme())) { builder.setPort(443); } } } builder.setFragment(null); if (builder.getPath() == null) { builder.setPath("/"); } return builder.build(); }
@Override public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) { if (tlsStrategy != null && ioSession.getInitialEndpoint() instanceof HttpHost) { final HttpHost host = (HttpHost) ioSession.getInitialEndpoint(); if (URIScheme.HTTPS.same(host.getSchemeName())) { tlsStrategy.upgrade( ioSession, host, ioSession.getLocalAddress(), ioSession.getRemoteAddress(), attachment, handshakeTimeout); } } return new ClientHttp1IOEventHandler(streamDuplexerFactory.create(ioSession)); }