diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java index 7a5cfba1e..ec31f3b09 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java @@ -765,7 +765,7 @@ public class H2AsyncClientBuilder { } final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry(); - final IOEventHandlerFactory ioEventHandlerFactory = new H2AsyncClientEventHandlerFactory( + final IOEventHandlerFactory ioEventHandlerFactory = new H2AsyncClientProtocolStarter( new DefaultHttpProcessor(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl()), (request, context) -> pushConsumerRegistry.get(request), h2Config != null ? h2Config : H2Config.DEFAULT, diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientProtocolStarter.java similarity index 95% rename from httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java rename to httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientProtocolStarter.java index adc565b14..11137fa5e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientEventHandlerFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientProtocolStarter.java @@ -39,8 +39,8 @@ import org.apache.hc.core5.http.protocol.HttpProcessor; import org.apache.hc.core5.http2.config.H2Config; import org.apache.hc.core5.http2.frame.FramePrinter; import org.apache.hc.core5.http2.frame.RawFrame; +import org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler; import org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory; -import org.apache.hc.core5.http2.impl.nio.H2OnlyClientProtocolNegotiator; import org.apache.hc.core5.http2.impl.nio.H2StreamListener; import org.apache.hc.core5.reactor.IOEventHandler; import org.apache.hc.core5.reactor.IOEventHandlerFactory; @@ -49,7 +49,7 @@ import org.apache.hc.core5.util.Args; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { +class H2AsyncClientProtocolStarter implements IOEventHandlerFactory { private static final Logger HEADER_LOG = LoggerFactory.getLogger("org.apache.hc.client5.http.headers"); private static final Logger FRAME_LOG = LoggerFactory.getLogger("org.apache.hc.client5.http2.frame"); @@ -61,7 +61,7 @@ class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { private final H2Config h2Config; private final CharCodingConfig charCodingConfig; - H2AsyncClientEventHandlerFactory( + H2AsyncClientProtocolStarter( final HttpProcessor httpProcessor, final HandlerFactory exchangeHandlerFactory, final H2Config h2Config, @@ -163,7 +163,7 @@ class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { } }); - return new H2OnlyClientProtocolNegotiator(ioSession, http2StreamHandlerFactory, false); + return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false); } final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory( httpProcessor, @@ -171,7 +171,7 @@ class H2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { h2Config, charCodingConfig, null); - return new H2OnlyClientProtocolNegotiator(ioSession, http2StreamHandlerFactory, false); + return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java index 58510879a..5fe29b7a8 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java @@ -933,7 +933,7 @@ public class HttpAsyncClientBuilder { } } final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry(); - final IOEventHandlerFactory ioEventHandlerFactory = new HttpAsyncClientEventHandlerFactory( + final IOEventHandlerFactory ioEventHandlerFactory = new HttpAsyncClientProtocolNegotiationStarter( new DefaultHttpProcessor(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl()), (request, context) -> pushConsumerRegistry.get(request), h2Config != null ? h2Config : H2Config.DEFAULT, diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientProtocolNegotiationStarter.java similarity index 85% rename from httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java rename to httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientProtocolNegotiationStarter.java index 15e98de5b..68e617a43 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientEventHandlerFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientProtocolNegotiationStarter.java @@ -40,6 +40,7 @@ import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.config.CharCodingConfig; import org.apache.hc.core5.http.config.Http1Config; import org.apache.hc.core5.http.impl.Http1StreamListener; +import org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler; import org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory; import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestWriterFactory; import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory; @@ -54,9 +55,13 @@ import org.apache.hc.core5.http2.HttpVersionPolicy; import org.apache.hc.core5.http2.config.H2Config; import org.apache.hc.core5.http2.frame.FramePrinter; import org.apache.hc.core5.http2.frame.RawFrame; +import org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler; import org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory; -import org.apache.hc.core5.http2.impl.nio.ClientHttpProtocolNegotiator; +import org.apache.hc.core5.http2.impl.nio.ClientH2UpgradeHandler; +import org.apache.hc.core5.http2.impl.nio.ClientHttp1UpgradeHandler; import org.apache.hc.core5.http2.impl.nio.H2StreamListener; +import org.apache.hc.core5.http2.impl.nio.HttpProtocolNegotiator; +import org.apache.hc.core5.http2.ssl.ApplicationProtocol; import org.apache.hc.core5.reactor.IOEventHandler; import org.apache.hc.core5.reactor.IOEventHandlerFactory; import org.apache.hc.core5.reactor.ProtocolIOSession; @@ -64,7 +69,7 @@ import org.apache.hc.core5.util.Args; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory { +class HttpAsyncClientProtocolNegotiationStarter implements IOEventHandlerFactory { private static final Logger STREAM_LOG = LoggerFactory.getLogger(InternalHttpAsyncClient.class); private static final Logger HEADER_LOG = LoggerFactory.getLogger("org.apache.hc.client5.http.headers"); @@ -81,7 +86,7 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory { private final NHttpMessageParserFactory http1ResponseParserFactory; private final NHttpMessageWriterFactory http1RequestWriterFactory; - HttpAsyncClientEventHandlerFactory( + HttpAsyncClientProtocolNegotiationStarter( final HttpProcessor httpProcessor, final HandlerFactory exchangeHandlerFactory, final H2Config h2Config, @@ -100,13 +105,16 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory { @Override public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) { + final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory; + final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory; + if (STREAM_LOG.isDebugEnabled() || HEADER_LOG.isDebugEnabled() || FRAME_LOG.isDebugEnabled() || FRAME_PAYLOAD_LOG.isDebugEnabled() || FLOW_CTRL_LOG.isDebugEnabled()) { final String id = ioSession.getId(); - final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory( + http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory( httpProcessor, h1Config, charCodingConfig, @@ -147,7 +155,7 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory { } }); - final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory( + http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory( httpProcessor, exchangeHandlerFactory, h2Config, @@ -231,31 +239,35 @@ class HttpAsyncClientEventHandlerFactory implements IOEventHandlerFactory { } }); - return new ClientHttpProtocolNegotiator( - ioSession, - http1StreamHandlerFactory, - http2StreamHandlerFactory, - attachment instanceof HttpVersionPolicy ? (HttpVersionPolicy) attachment : null); + } else { + http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory( + httpProcessor, + h1Config, + charCodingConfig, + http1ConnectionReuseStrategy, + http1ResponseParserFactory, + http1RequestWriterFactory, + null); + http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory( + httpProcessor, + exchangeHandlerFactory, + h2Config, + charCodingConfig, + null); + } + + ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ClientHttp1UpgradeHandler(http1StreamHandlerFactory)); + ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ClientH2UpgradeHandler(http2StreamHandlerFactory)); + + final HttpVersionPolicy versionPolicy = attachment instanceof HttpVersionPolicy ? (HttpVersionPolicy) attachment : HttpVersionPolicy.NEGOTIATE; + switch (versionPolicy) { + case FORCE_HTTP_2: + return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false); + case FORCE_HTTP_1: + return new ClientHttp1IOEventHandler(http1StreamHandlerFactory.create(ioSession)); + default: + return new HttpProtocolNegotiator(ioSession, null); } - final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory( - httpProcessor, - h1Config, - charCodingConfig, - http1ConnectionReuseStrategy, - http1ResponseParserFactory, - http1RequestWriterFactory, - null); - final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory( - httpProcessor, - exchangeHandlerFactory, - h2Config, - charCodingConfig, - null); - return new ClientHttpProtocolNegotiator( - ioSession, - http1StreamHandlerFactory, - http2StreamHandlerFactory, - attachment instanceof HttpVersionPolicy ? (HttpVersionPolicy) attachment : null); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java index ea8ec3a46..cb0d1e2cf 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java @@ -154,7 +154,7 @@ public final class HttpAsyncClients { final AsyncClientConnectionManager connmgr) { final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry(); return createMinimalHttpAsyncClientImpl( - new HttpAsyncClientEventHandlerFactory( + new HttpAsyncClientProtocolNegotiationStarter( createMinimalProtocolProcessor(), (request, context) -> pushConsumerRegistry.get(request), h2Config, @@ -182,7 +182,7 @@ public final class HttpAsyncClients { final AsyncClientConnectionManager connmgr) { final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry(); return createMinimalHttpAsyncClientImpl( - new HttpAsyncClientEventHandlerFactory( + new HttpAsyncClientProtocolNegotiationStarter( createMinimalProtocolProcessor(), (request, context) -> pushConsumerRegistry.get(request), h2Config, @@ -285,7 +285,7 @@ public final class HttpAsyncClients { final TlsStrategy tlsStrategy) { final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry(); return createMinimalHttp2AsyncClientImpl( - new H2AsyncClientEventHandlerFactory( + new H2AsyncClientProtocolStarter( createMinimalProtocolProcessor(), (request, context) -> pushConsumerRegistry.get(request), h2Config, diff --git a/pom.xml b/pom.xml index 033fd1b59..487109c7a 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ 1.8 1.8 - 5.2-alpha2 + 5.2-beta1 2.17.0 1.15 2.5.2