Search in sources :

Example 1 with Http2MultiplexHandler

use of io.netty.handler.codec.http2.Http2MultiplexHandler in project netty by netty.

the class Http2OrHttpHandler method configurePipeline.

@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
    if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
        ctx.pipeline().addLast(Http2FrameCodecBuilder.forServer().build());
        ctx.pipeline().addLast(new Http2MultiplexHandler(new HelloWorldHttp2Handler()));
        return;
    }
    if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
        ctx.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(MAX_CONTENT_LENGTH), new HelloWorldHttp1Handler("ALPN Negotiation"));
        return;
    }
    throw new IllegalStateException("unknown protocol: " + protocol);
}
Also used : Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HelloWorldHttp1Handler(io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 2 with Http2MultiplexHandler

use of io.netty.handler.codec.http2.Http2MultiplexHandler in project rest.li by linkedin.

the class Http2AlpnHandler method configurePipeline.

@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
    switch(protocol) {
        case ApplicationProtocolNames.HTTP_2:
            ctx.pipeline().addLast(Http2FrameCodecBuilder.forClient().initialSettings(_http2Settings).build());
            ctx.pipeline().addLast(new Http2MultiplexHandler(new UnsupportedHandler()));
            _alpnPromise.setSuccess();
            break;
        default:
            _alpnPromise.setFailure(new IllegalStateException("Unsupported protocol '" + protocol + "' is negotiated."));
    }
}
Also used : Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler)

Example 3 with Http2MultiplexHandler

use of io.netty.handler.codec.http2.Http2MultiplexHandler in project reactor-netty by reactor.

the class Http2PoolTest method acquireInvalidate.

@Test
void acquireInvalidate() {
    EmbeddedChannel channel = new EmbeddedChannel(Http2FrameCodecBuilder.forClient().build(), new Http2MultiplexHandler(new ChannelHandlerAdapter() {
    }));
    PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.just(Connection.from(channel))).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
    InstrumentedPool<Connection> http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
    try {
        List<PooledRef<Connection>> acquired = new ArrayList<>();
        http2Pool.acquire().subscribe(acquired::add);
        http2Pool.acquire().subscribe(acquired::add);
        http2Pool.acquire().subscribe(acquired::add);
        assertThat(acquired).hasSize(3);
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(3);
        for (PooledRef<Connection> slot : acquired) {
            slot.invalidate().block(Duration.ofSeconds(1));
        }
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
        for (PooledRef<Connection> slot : acquired) {
            // second invalidate() should be ignored and ACQUIRED size should remain the same
            slot.invalidate().block(Duration.ofSeconds(1));
        }
        assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
    } finally {
        channel.finishAndReleaseAll();
        Connection.from(channel).dispose();
    }
}
Also used : Connection(reactor.netty.Connection) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) PoolConfig(reactor.netty.internal.shaded.reactor.pool.PoolConfig) PooledRef(reactor.netty.internal.shaded.reactor.pool.PooledRef) Test(org.junit.jupiter.api.Test)

Example 4 with Http2MultiplexHandler

use of io.netty.handler.codec.http2.Http2MultiplexHandler in project reactor-netty by reactor.

the class HttpServerConfig method configureH2Pipeline.

static void configureH2Pipeline(ChannelPipeline p, boolean accessLogEnabled, @Nullable Function<AccessLogArgProvider, AccessLog> accessLog, @Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate, ServerCookieDecoder cookieDecoder, ServerCookieEncoder cookieEncoder, HttpServerFormDecoderProvider formDecoderProvider, @Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler, Http2Settings http2Settings, ConnectionObserver listener, @Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle, int minCompressionSize, ChannelOperations.OnSetup opsFactory, boolean validate) {
    p.remove(NettyPipeline.ReactiveBridge);
    Http2FrameCodecBuilder http2FrameCodecBuilder = Http2FrameCodecBuilder.forServer().validateHeaders(validate).initialSettings(http2Settings);
    if (p.get(NettyPipeline.LoggingHandler) != null) {
        http2FrameCodecBuilder.frameLogger(new Http2FrameLogger(LogLevel.DEBUG, "reactor.netty.http.server.h2"));
    }
    p.addLast(NettyPipeline.HttpCodec, http2FrameCodecBuilder.build()).addLast(NettyPipeline.H2MultiplexHandler, new Http2MultiplexHandler(new H2Codec(accessLogEnabled, accessLog, compressPredicate, cookieDecoder, cookieEncoder, formDecoderProvider, forwardedHeaderHandler, listener, mapHandle, minCompressionSize, opsFactory)));
}
Also used : Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) Http2FrameCodecBuilder(io.netty.handler.codec.http2.Http2FrameCodecBuilder) Http2FrameLogger(io.netty.handler.codec.http2.Http2FrameLogger)

Example 5 with Http2MultiplexHandler

use of io.netty.handler.codec.http2.Http2MultiplexHandler in project cxf by apache.

the class NettyHttpServletPipelineFactory method getDefaultHttp2ChannelPipeline.

protected ChannelPipeline getDefaultHttp2ChannelPipeline(Channel channel) throws Exception {
    // Create a default pipeline implementation with HTTP/2 support
    ChannelPipeline pipeline = channel.pipeline();
    SslContext sslCtx = configureServerHttp2SSLOnDemand();
    if (sslCtx != null) {
        final SslHandler sslHandler = sslCtx.newHandler(channel.alloc());
        LOG.log(Level.FINE, "Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast(sslHandler, new Http2OrHttpHandler());
        return pipeline;
    }
    final UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(Http2FrameCodecBuilder.forServer().build(), new Http2MultiplexHandler(createHttp2ChannelInitializer()));
            } else {
                return null;
            }
        }
    };
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler, createHttp2ChannelInitializerPriorKnowledge());
    pipeline.addLast(cleartextUpgradeHandler);
    pipeline.addLast(new SimpleChannelInboundHandler<HttpMessage>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP
            final ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(applicationExecutor, ctx.name(), "handler", getServletHandler());
            pipeline.replace(this, "aggregator", new HttpObjectAggregator(maxChunkContentSize));
            // Remove the following line if you don't want automatic content compression.
            pipeline.addLast("deflater", new HttpContentCompressor());
            // Set up the idle handler
            pipeline.addLast("idle", new IdleStateHandler(nettyHttpServerEngine.getReadIdleTime(), nettyHttpServerEngine.getWriteIdleTime(), 0));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });
    return pipeline;
}
Also used : CleartextHttp2ServerUpgradeHandler(io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

Http2MultiplexHandler (io.netty.handler.codec.http2.Http2MultiplexHandler)12 Http2FrameLogger (io.netty.handler.codec.http2.Http2FrameLogger)4 ChannelPipeline (io.netty.channel.ChannelPipeline)3 SslHandler (io.netty.handler.ssl.SslHandler)3 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)2 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)2 Http2FrameCodec (io.netty.handler.codec.http2.Http2FrameCodec)2 Http2FrameCodecBuilder (io.netty.handler.codec.http2.Http2FrameCodecBuilder)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 Connection (reactor.netty.Connection)2 PoolConfig (reactor.netty.internal.shaded.reactor.pool.PoolConfig)2 PooledRef (reactor.netty.internal.shaded.reactor.pool.PooledRef)2 Http2ProtocolUpgradeHandler (com.linkedin.r2.netty.handler.http2.Http2ProtocolUpgradeHandler)1 UnsupportedHandler (com.linkedin.r2.netty.handler.http2.UnsupportedHandler)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelPromise (io.netty.channel.ChannelPromise)1