3

I built a simple pipeline for serving HTTP/1.1 content with Netty, and I'm looking to see how I can convert it to HTTP/2. I see various classes for converting between frames and negotiating, but I can't figure out how to assemble them into a pipeline. The following code is in Kotlin. The main issue I have is with ALPN negotiation, and also converting the frames right in both directions.

val bootstrap = ServerBootstrap()
bootstrap.group(transport.bossGroup, transport.workerGroup)
    .channelFactory(transport.factory)
    .childHandler(object : ChannelInitializer<SocketChannel>() {
        public override fun initChannel(ch: SocketChannel) {
            ch.pipeline().addLast("ssl", sslContext.newHandler(ch.alloc()))

            ch.pipeline().addLast("codec", HttpServerCodec())
            ch.pipeline().addLast("keepAlive", HttpServerKeepAliveHandler())
            ch.pipeline().addLast("aggregator", HttpObjectAggregator(65536))

            ch.pipeline().addLast("burstLimiter", burstLimiter)

            ch.pipeline().addLast(
                "readTimeoutHandler",
                ReadTimeoutHandler(60)
            )
            ch.pipeline().addLast(
                "writeTimeoutHandler",
                WriteTimeoutHandler(60)
            )

            ch.pipeline().addLast("streamer", ChunkedWriteHandler())
            ch.pipeline().addLast("handler", CustomHandler(httpHandler))
        }
    })

2 Answers 2

2
+100

I think the easiest solution would be to make use of the Http2MultiplexHandler together with the Http2StreamFrameToHttpObjectCodec. This will allow you to speak HTTP2 while still provide you with the same "objects" in your handler as with HTTP1.

So it would be something like:

ch.pipeline().addLast(Http2FrameCodecBuilder.forServer().build());
ch.pipeline().addLast(new Http2MultiplexHandler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) {
        ch.pipeline().addLast(new Http2StreamFrameToHttpObjectCodec(true));
        // Add all your other handlers that act on HTTP1 objects
    }
});

If you also need to add handlers for SSL etc depends a bit on your use case.

For a more detailed example you can also consult the http2 multiplex examples that are part of netty itself. These handle H1/H2 at the same time depending on what the client asks for.

Sign up to request clarification or add additional context in comments.

Comments

0

What about a non-code solution - e.g. front-end netty with Apache - let Apache do the HTTP/2 for you? And then just pass on the trafic to your pipeline see the Apache HTTP2 How To

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.