2828package org .apache .hc .client5 .http .impl .classic ;
2929
3030import java .io .IOException ;
31+ import java .util .Arrays ;
3132
3233import org .apache .hc .client5 .http .AuthenticationStrategy ;
3334import org .apache .hc .client5 .http .EndpointInfo ;
4041import org .apache .hc .client5 .http .classic .ExecChainHandler ;
4142import org .apache .hc .client5 .http .classic .ExecRuntime ;
4243import org .apache .hc .client5 .http .config .RequestConfig ;
44+ import org .apache .hc .client5 .http .impl .AlpnHeaderSupport ;
4345import org .apache .hc .client5 .http .impl .auth .AuthCacheKeeper ;
4446import org .apache .hc .client5 .http .impl .auth .AuthenticationHandler ;
4547import org .apache .hc .client5 .http .impl .routing .BasicRouteDirector ;
6567import org .apache .hc .core5 .http .message .BasicClassicHttpRequest ;
6668import org .apache .hc .core5 .http .message .StatusLine ;
6769import org .apache .hc .core5 .http .protocol .HttpProcessor ;
70+ import org .apache .hc .core5 .http2 .HttpVersionPolicy ;
71+ import org .apache .hc .core5 .http2 .ssl .H2TlsSupport ;
6872import org .apache .hc .core5 .util .Args ;
6973import org .slf4j .Logger ;
7074import org .slf4j .LoggerFactory ;
@@ -139,7 +143,6 @@ public ClassicHttpResponse execute(
139143 step = this .routeDirector .nextStep (route , fact );
140144
141145 switch (step ) {
142-
143146 case HttpRouteDirector .CONNECT_TARGET :
144147 execRuntime .connectEndpoint (context );
145148 tracker .connectTarget (route .isSecure ());
@@ -162,11 +165,8 @@ public ClassicHttpResponse execute(
162165 }
163166 break ;
164167
165- case HttpRouteDirector .TUNNEL_PROXY : {
166- // Proxy chains are not supported by HttpClient.
167- // Fail fast instead of attempting an untested tunnel to an intermediate proxy.
168+ case HttpRouteDirector .TUNNEL_PROXY :
168169 throw new HttpException ("Proxy chains are not supported." );
169- }
170170
171171 case HttpRouteDirector .LAYER_PROTOCOL :
172172 execRuntime .upgradeTls (context );
@@ -197,14 +197,6 @@ public ClassicHttpResponse execute(
197197 }
198198 }
199199
200- /**
201- * Creates a tunnel to the target server.
202- * The connection must be established to the (last) proxy.
203- * A CONNECT request for tunnelling through the proxy will
204- * be created and sent, the response received and checked.
205- * This method does <i>not</i> processChallenge the connection with
206- * information about the tunnel, that is left to the caller.
207- */
208200 private ClassicHttpResponse createTunnelToTarget (
209201 final String exchangeId ,
210202 final HttpRoute route ,
@@ -228,6 +220,16 @@ private ClassicHttpResponse createTunnelToTarget(
228220 final ClassicHttpRequest connect = new BasicClassicHttpRequest (Method .CONNECT , target , authority );
229221 connect .setVersion (HttpVersion .HTTP_1_1 );
230222
223+ // RFC 7639: advertise the same ALPN protocols the tunnel's TLS layer will offer, derived
224+ // from the target's HttpVersionPolicy published on the context by the connection manager,
225+ // so the header cannot diverge from the protocol actually negotiated inside the tunnel.
226+ if (route .isSecure ()) {
227+ final HttpVersionPolicy configured = context .getHttpVersionPolicy ();
228+ final HttpVersionPolicy versionPolicy = configured != null ? configured : HttpVersionPolicy .NEGOTIATE ;
229+ connect .setHeader (AlpnHeaderSupport .formatValue (
230+ Arrays .asList (H2TlsSupport .selectApplicationProtocols (versionPolicy ))));
231+ }
232+
231233 this .proxyHttpProcessor .process (connect , null , context );
232234
233235 while (response == null ) {
@@ -262,12 +264,10 @@ private ClassicHttpResponse createTunnelToTarget(
262264 authCacheKeeper .updateOnResponse (proxy , null , proxyAuthExchange , context );
263265 }
264266 if (updated ) {
265- // Retry request
266267 if (this .reuseStrategy .keepAlive (connect , response , context )) {
267268 if (LOG .isDebugEnabled ()) {
268269 LOG .debug ("{} connection kept alive" , exchangeId );
269270 }
270- // Consume response content
271271 final HttpEntity entity = response .getEntity ();
272272 EntityUtils .consume (entity );
273273 } else {
@@ -295,26 +295,11 @@ private ClassicHttpResponse createTunnelToTarget(
295295 return null ;
296296 }
297297
298- /**
299- * Creates a tunnel to an intermediate proxy.
300- * This method is <i>not</i> implemented in this class.
301- * It just throws an exception here.
302- */
303298 private boolean createTunnelToProxy (
304299 final HttpRoute route ,
305300 final int hop ,
306301 final HttpClientContext context ) throws HttpException {
307-
308- // Have a look at createTunnelToTarget and replicate the parts
309- // you need in a custom derived class. If your proxies don't require
310- // authentication, it is not too hard. But for the stock version of
311- // HttpClient, we cannot make such simplifying assumptions and would
312- // have to include proxy authentication code. The HttpComponents team
313- // is currently not in a position to support rarely used code of this
314- // complexity. Feel free to submit patches that refactor the code in
315- // createTunnelToTarget to facilitate re-use for proxy tunnelling.
316-
317302 throw new HttpException ("Proxy chains are not supported." );
318303 }
319304
320- }
305+ }
0 commit comments