Skip to content

Commit f0f0a43

Browse files
committed
HTTPCLIENT-2261 - Enable HTTP/2 CONNECT tunneling for HTTP/2 clients through HTTP/2 proxies
Wire HTTP/2 tunnel establishment into InternalH2ConnPool for tunneled routes by using H2OverH2TunnelSupport to convert an existing proxy HTTP/2 connection into a stream-backed tunnel session
1 parent 08f3fdc commit f0f0a43

21 files changed

+3500
-63
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/async/AsyncExecRuntime.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ Cancellable execute(
175175
*/
176176
void markConnectionNonReusable();
177177

178+
/**
179+
* Returns the route that has already been established by the connection pool,
180+
* or {@code null} if route completion is not handled at the pool level.
181+
*
182+
* @return the established route, or {@code null}.
183+
*
184+
* @since 5.7
185+
*/
186+
default HttpRoute getEstablishedRoute() {
187+
return null;
188+
}
189+
178190
/**
179191
* Forks this runtime for parallel execution.
180192
*

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncConnectExec.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.apache.hc.client5.http.auth.AuthenticationException;
4747
import org.apache.hc.client5.http.auth.ChallengeType;
4848
import org.apache.hc.client5.http.auth.MalformedChallengeException;
49-
import org.apache.hc.client5.http.config.RequestConfig;
5049
import org.apache.hc.client5.http.impl.auth.AuthCacheKeeper;
5150
import org.apache.hc.client5.http.impl.auth.AuthenticationHandler;
5251
import org.apache.hc.client5.http.impl.routing.BasicRouteDirector;
@@ -250,6 +249,15 @@ public void cancelled() {
250249
public void completed(final AsyncExecRuntime execRuntime) {
251250
final HttpHost proxy = route.getProxyHost();
252251
tracker.connectProxy(proxy, route.isSecure() && !route.isTunnelled());
252+
if (route.isTunnelled() && execRuntime.getEstablishedRoute() != null) {
253+
if (LOG.isDebugEnabled()) {
254+
LOG.debug("{} tunnel to target already established by connection pool", exchangeId);
255+
}
256+
tracker.tunnelTarget(false);
257+
if (route.isLayered()) {
258+
tracker.layerProtocol(route.isSecure());
259+
}
260+
}
253261
if (LOG.isDebugEnabled()) {
254262
LOG.debug("{} connected to proxy", exchangeId);
255263
}
@@ -519,31 +527,8 @@ private boolean needAuthentication(
519527
final HttpHost proxy,
520528
final HttpResponse response,
521529
final HttpClientContext context) throws AuthenticationException, MalformedChallengeException {
522-
final RequestConfig config = context.getRequestConfigOrDefault();
523-
if (config.isAuthenticationEnabled()) {
524-
final boolean proxyAuthRequested = authenticator.isChallenged(proxy, ChallengeType.PROXY, response, proxyAuthExchange, context);
525-
final boolean proxyMutualAuthRequired = authenticator.isChallengeExpected(proxyAuthExchange);
526-
527-
if (authCacheKeeper != null) {
528-
if (proxyAuthRequested) {
529-
authCacheKeeper.updateOnChallenge(proxy, null, proxyAuthExchange, context);
530-
} else {
531-
authCacheKeeper.updateOnNoChallenge(proxy, null, proxyAuthExchange, context);
532-
}
533-
}
534-
535-
if (proxyAuthRequested || proxyMutualAuthRequired) {
536-
final boolean updated = authenticator.handleResponse(proxy, ChallengeType.PROXY, response,
537-
proxyAuthStrategy, proxyAuthExchange, context);
538-
539-
if (authCacheKeeper != null) {
540-
authCacheKeeper.updateOnResponse(proxy, null, proxyAuthExchange, context);
541-
}
542-
543-
return updated;
544-
}
545-
}
546-
return false;
530+
return authenticator.needProxyAuthentication(
531+
proxyAuthExchange, proxy, response, proxyAuthStrategy, authCacheKeeper, context);
547532
}
548533

549534
private void proceedConnected(

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,12 @@ public CloseableHttpAsyncClient build() {
840840
new H2AsyncMainClientExec(httpProcessor),
841841
ChainElement.MAIN_TRANSPORT.name());
842842

843+
final HttpProcessor proxyConnectHttpProcessor =
844+
new DefaultHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy));
845+
843846
execChainDefinition.addFirst(
844847
new AsyncConnectExec(
845-
new DefaultHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)),
848+
proxyConnectHttpProcessor,
846849
proxyAuthStrategyCopy,
847850
schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE,
848851
authCachingDisabled),
@@ -971,7 +974,21 @@ public CloseableHttpAsyncClient build() {
971974
}
972975

973976
final MultihomeConnectionInitiator connectionInitiator = new MultihomeConnectionInitiator(ioReactor, dnsResolver);
974-
final InternalH2ConnPool connPool = new InternalH2ConnPool(connectionInitiator, host -> null, tlsStrategyCopy);
977+
final H2RouteOperator routeOperator = new H2RouteOperator(
978+
tlsStrategyCopy,
979+
new H2TunnelProtocolStarter(h2Config, charCodingConfig),
980+
proxyConnectHttpProcessor,
981+
proxyAuthStrategyCopy,
982+
schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE,
983+
authCachingDisabled,
984+
authSchemeRegistryCopy,
985+
credentialsProviderCopy,
986+
defaultRequestConfig);
987+
final InternalH2ConnPool connPool = new InternalH2ConnPool(
988+
connectionInitiator,
989+
host -> null,
990+
tlsStrategyCopy,
991+
routeOperator);
975992
connPool.setConnectionConfigResolver(connectionConfigResolver);
976993

977994
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<>(closeables) : null;

0 commit comments

Comments
 (0)