Search in sources :

Example 1 with ChannelPipelineListener

use of io.micronaut.http.netty.channel.ChannelPipelineListener in project micronaut-core by micronaut-projects.

the class DefaultHttpClient method configureHttp2Ssl.

/**
 * Configures HTTP/2 for the channel when SSL is enabled.
 *
 * @param httpClientInitializer The client initializer
 * @param ch                    The channel
 * @param sslCtx                The SSL context
 * @param host                  The host
 * @param port                  The port
 * @param connectionHandler     The connection handler
 */
protected void configureHttp2Ssl(HttpClientInitializer httpClientInitializer, @NonNull SocketChannel ch, @NonNull SslContext sslCtx, String host, int port, HttpToHttp2ConnectionHandler connectionHandler) {
    ChannelPipeline pipeline = ch.pipeline();
    // Specify Host in SSLContext New Handler to add TLS SNI Extension
    pipeline.addLast(ChannelPipelineCustomizer.HANDLER_SSL, sslCtx.newHandler(ch.alloc(), host, port));
    // We must wait for the handshake to finish and the protocol to be negotiated before configuring
    // the HTTP/2 components of the pipeline.
    pipeline.addLast(ChannelPipelineCustomizer.HANDLER_HTTP2_PROTOCOL_NEGOTIATOR, new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_2) {

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) {
            // the logic to send the request should only be executed once the HTTP/2
            // Connection Preface request has been sent. Once the Preface has been sent and
            // removed then this handler is removed so we invoke the remaining logic once
            // this handler removed
            final Consumer<ChannelHandlerContext> contextConsumer = httpClientInitializer.contextConsumer;
            if (contextConsumer != null) {
                contextConsumer.accept(ctx);
            }
        }

        @Override
        protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
            if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
                ChannelPipeline p = ctx.pipeline();
                if (httpClientInitializer.stream) {
                    // stream consumer manages backpressure and reads
                    ctx.channel().config().setAutoRead(false);
                }
                p.addLast(ChannelPipelineCustomizer.HANDLER_HTTP2_SETTINGS, new Http2SettingsHandler(ch.newPromise()));
                httpClientInitializer.addEventStreamHandlerIfNecessary(p);
                httpClientInitializer.addFinalHandler(p);
                for (ChannelPipelineListener pipelineListener : pipelineListeners) {
                    pipelineListener.onConnect(p);
                }
            } else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
                ChannelPipeline p = ctx.pipeline();
                httpClientInitializer.addHttp1Handlers(p);
            } else {
                ctx.close();
                throw new HttpClientException("Unknown Protocol: " + protocol);
            }
        }
    });
    pipeline.addLast(ChannelPipelineCustomizer.HANDLER_HTTP2_CONNECTION, connectionHandler);
}
Also used : HttpClientException(io.micronaut.http.client.exceptions.HttpClientException) Consumer(java.util.function.Consumer) ChannelPipelineListener(io.micronaut.http.netty.channel.ChannelPipelineListener) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)

Aggregations

HttpClientException (io.micronaut.http.client.exceptions.HttpClientException)1 ChannelPipelineListener (io.micronaut.http.netty.channel.ChannelPipelineListener)1 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)1 Consumer (java.util.function.Consumer)1