Skip to content

Commit 36a85c5

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 36a85c5

10 files changed

Lines changed: 658 additions & 37 deletions

File tree

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: 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 IOEventHandlerFactory tunnelProtocolStarter = new H2TunnelProtocolStarter(
978+
h2Config,
979+
charCodingConfig);
980+
final InternalH2ConnPool connPool = new InternalH2ConnPool(
981+
connectionInitiator,
982+
host -> null,
983+
tlsStrategyCopy,
984+
tunnelProtocolStarter,
985+
proxyConnectHttpProcessor,
986+
proxyAuthStrategyCopy,
987+
schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE,
988+
authCachingDisabled,
989+
authSchemeRegistryCopy,
990+
credentialsProviderCopy,
991+
defaultRequestConfig);
975992
connPool.setConnectionConfigResolver(connectionConfigResolver);
976993

977994
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<>(closeables) : null;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
28+
package org.apache.hc.client5.http.impl.async;
29+
30+
import org.apache.hc.core5.http.config.CharCodingConfig;
31+
import org.apache.hc.core5.http.protocol.HttpProcessorBuilder;
32+
import org.apache.hc.core5.http2.config.H2Config;
33+
import org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler;
34+
import org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory;
35+
import org.apache.hc.core5.reactor.IOEventHandler;
36+
import org.apache.hc.core5.reactor.IOEventHandlerFactory;
37+
import org.apache.hc.core5.reactor.ProtocolIOSession;
38+
39+
/**
40+
* Minimal {@link IOEventHandlerFactory} for starting HTTP/2 client protocol
41+
* inside a CONNECT tunnel session.
42+
* <p>
43+
* Unlike {@link H2AsyncClientProtocolStarter}, this factory does not
44+
* install push consumer handling, frame/header logging listeners, or
45+
* exception callbacks. Those concerns belong to the outer proxy
46+
* connection, not the tunneled target connection.
47+
* </p>
48+
*
49+
* @since 5.7
50+
*/
51+
final class H2TunnelProtocolStarter implements IOEventHandlerFactory {
52+
53+
private final H2Config h2Config;
54+
private final CharCodingConfig charCodingConfig;
55+
56+
H2TunnelProtocolStarter(
57+
final H2Config h2Config,
58+
final CharCodingConfig charCodingConfig) {
59+
this.h2Config = h2Config != null ? h2Config : H2Config.DEFAULT;
60+
this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
61+
}
62+
63+
@Override
64+
public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
65+
final ClientH2StreamMultiplexerFactory multiplexerFactory = new ClientH2StreamMultiplexerFactory(
66+
HttpProcessorBuilder.create().build(),
67+
null,
68+
h2Config,
69+
charCodingConfig,
70+
null);
71+
return new ClientH2PrefaceHandler(ioSession, multiplexerFactory, false, null);
72+
}
73+
74+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,16 @@ public static MinimalHttpAsyncClient createMinimal(final AsyncClientConnectionMa
276276
private static MinimalH2AsyncClient createMinimalHttp2AsyncClientImpl(
277277
final IOEventHandlerFactory eventHandlerFactory,
278278
final AsyncPushConsumerRegistry pushConsumerRegistry,
279+
final H2Config h2Config,
280+
final CharCodingConfig charCodingConfig,
279281
final IOReactorConfig ioReactorConfig,
280282
final DnsResolver dnsResolver,
281283
final TlsStrategy tlsStrategy) {
282284
return new MinimalH2AsyncClient(
283285
eventHandlerFactory,
284286
pushConsumerRegistry,
287+
h2Config,
288+
charCodingConfig,
285289
ioReactorConfig,
286290
new DefaultThreadFactory("httpclient-main", true),
287291
new DefaultThreadFactory("httpclient-dispatch", true),
@@ -307,6 +311,8 @@ public static MinimalH2AsyncClient createHttp2Minimal(
307311
CharCodingConfig.DEFAULT,
308312
LoggingExceptionCallback.INSTANCE),
309313
pushConsumerRegistry,
314+
h2Config,
315+
CharCodingConfig.DEFAULT,
310316
ioReactorConfig,
311317
dnsResolver,
312318
tlsStrategy);

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)