Reputation: 2491
What's the purpose the HTTP2ToHTTP1ServerCodec
. Why is it required to convert the HTTP/2 to HTTP/1 protocol?
channel.configureHTTP2Pipeline(mode: .server) { (streamChannel, streamID) -> EventLoopFuture<Void> in
// For every HTTP/2 stream that the client opens, we put in the `HTTP2ToHTTP1ServerCodec` which
// transforms the HTTP/2 frames to the HTTP/1 messages from the `NIOHTTP1` module.
streamChannel.pipeline.addHandler(HTTP2ToHTTP1ServerCodec(streamID: streamID)).flatMap { () -> EventLoopFuture<Void> in
// And lastly, we put in our very basic HTTP server :).
streamChannel.pipeline.addHandler(HTTPServer())
This is a modified code snippet from the Swift NIO example repo.
Upvotes: 0
Views: 371
Reputation: 54071
HTTP/2 is HTTP(/1) semantics over multiple streams in a single TCP connection (and a new wire protocol). As an option, SwiftNIO therefore can allow you to use the HTTP(/1) data types to handle HTTP/2. That allows you to fully re-use your server/client implementations that you wrote using SwiftNIO and HTTP/1 for HTTP/2 too which for most people is a benefit.
That translation however is absolutely not required. If you prefer to operate on HTTP2 frames directly, you don't need to add the HTTP2ToHTTP1ServerCodec
to your pipeline.
Later edit: I should also point out that the HTTP2toHTTP1ServerCodec
is actually quite a simple piece of code that literally just translates the inbound and outbound messages between HTTP/2 frames and HTTP/1 message parts. Code is here.
Upvotes: 4