Skip to content

Commit 6b2de95

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 6b2de95

File tree

6 files changed

+483
-36
lines changed

6 files changed

+483
-36
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ public void cancelled() {
250250
public void completed(final AsyncExecRuntime execRuntime) {
251251
final HttpHost proxy = route.getProxyHost();
252252
tracker.connectProxy(proxy, route.isSecure() && !route.isTunnelled());
253+
if (route.isTunnelled() && execRuntime instanceof InternalH2AsyncExecRuntime) {
254+
if (route.getHopCount() > 2) {
255+
asyncExecCallback.failed(new HttpException("Proxy chains are not supported"));
256+
return;
257+
}
258+
if (LOG.isDebugEnabled()) {
259+
LOG.debug("{} H2 tunnel to target already established by connection pool", exchangeId);
260+
}
261+
tracker.tunnelTarget(false);
262+
if (route.isLayered()) {
263+
tracker.layerProtocol(route.isSecure());
264+
}
265+
}
253266
if (LOG.isDebugEnabled()) {
254267
LOG.debug("{} connected to proxy", exchangeId);
255268
}

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

Lines changed: 16 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,18 @@ 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 InternalH2ConnPool connPool = new InternalH2ConnPool(
978+
connectionInitiator,
979+
host -> null,
980+
tlsStrategyCopy,
981+
ioEventHandlerFactory,
982+
proxyConnectHttpProcessor,
983+
proxyAuthStrategyCopy,
984+
schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE,
985+
authCachingDisabled,
986+
authSchemeRegistryCopy,
987+
credentialsProviderCopy,
988+
defaultRequestConfig);
975989
connPool.setConnectionConfigResolver(connectionConfigResolver);
976990

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

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ HttpRoute determineRoute(
108108
final HttpHost httpHost,
109109
final HttpRequest request,
110110
final HttpClientContext clientContext) throws HttpException {
111-
final HttpRoute route = routePlanner.determineRoute(httpHost, request, clientContext);
112-
if (route.isTunnelled()) {
113-
throw new HttpException("HTTP/2 tunneling not supported");
114-
}
115-
return route;
111+
return routePlanner.determineRoute(httpHost, request, clientContext);
116112
}
117113

118114
}

0 commit comments

Comments
 (0)