Skip to content

Commit 956dbf8

Browse files
committed
Add another variant configureAsyncHTTPServerPipeline
1 parent 31c8609 commit 956dbf8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Sources/NIOHTTP2/HTTP2PipelineHelpers.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,61 @@ extension Channel {
651651
http2ConnectionInitializer: http2ConnectionInitializer
652652
)
653653
}
654+
655+
/// Configures a `ChannelPipeline` to speak either HTTP/1.1 or HTTP/2 according to what can be negotiated with the client.
656+
///
657+
/// This helper takes care of configuring the server pipeline such that it negotiates whether to
658+
/// use HTTP/1.1 or HTTP/2.
659+
///
660+
/// This function doesn't configure the TLS handler. Callers of this function need to add a TLS
661+
/// handler appropriately configured to perform protocol negotiation.
662+
///
663+
/// - Parameters:
664+
/// - streamDelegate: A delegate which is called when streams are created and closed.
665+
/// - http2Configuration: The settings that will be used when establishing the HTTP/2 connections and new HTTP/2 streams.
666+
/// - http1ConnectionInitializer: An optional callback that will be invoked only when the negotiated protocol
667+
/// is HTTP/1.1 to configure the connection channel.
668+
/// - http2ConnectionInitializer: An optional callback that will be invoked only when the negotiated protocol
669+
/// is HTTP/2 to configure the connection channel. The channel has an `ChannelOutboundHandler/OutboundIn` type of ``HTTP2Frame``.
670+
/// - http2StreamInitializer: A closure that will be called whenever the remote peer initiates a new stream.
671+
/// The output of this closure is the element type of the returned multiplexer
672+
/// - Returns: An `EventLoopFuture` containing a `NIOTypedApplicationProtocolNegotiationHandler` that completes when the channel
673+
/// is ready to negotiate. This can then be used to access the `NIOProtocolNegotiationResult` which may itself
674+
/// be waited on to retrieve the result of the negotiation.
675+
@inlinable
676+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
677+
public func configureAsyncHTTPServerPipeline<HTTP1ConnectionOutput: Sendable, HTTP2ConnectionOutput: Sendable, HTTP2StreamOutput: Sendable>(
678+
streamDelegate: NIOHTTP2StreamDelegate,
679+
http2Configuration: NIOHTTP2Handler.Configuration = .init(),
680+
http1ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP1ConnectionOutput>,
681+
http2ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2ConnectionOutput>,
682+
http2StreamInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2StreamOutput>
683+
) -> EventLoopFuture<EventLoopFuture<NIONegotiatedHTTPVersion<
684+
HTTP1ConnectionOutput,
685+
(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)
686+
>>> {
687+
let http2ConnectionInitializer: NIOChannelInitializerWithOutput<(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)> = { channel in
688+
channel.configureAsyncHTTP2Pipeline(
689+
mode: .server,
690+
streamDelegate: streamDelegate,
691+
configuration: http2Configuration,
692+
streamInitializer: http2StreamInitializer
693+
).flatMap { multiplexer in
694+
return http2ConnectionInitializer(channel).map { connectionChannel in
695+
(connectionChannel, multiplexer)
696+
}
697+
}
698+
}
699+
let http1ConnectionInitializer: NIOChannelInitializerWithOutput<HTTP1ConnectionOutput> = { channel in
700+
channel.pipeline.configureHTTPServerPipeline().flatMap { _ in
701+
http1ConnectionInitializer(channel)
702+
}
703+
}
704+
return self.configureHTTP2AsyncSecureUpgrade(
705+
http1ConnectionInitializer: http1ConnectionInitializer,
706+
http2ConnectionInitializer: http2ConnectionInitializer
707+
)
708+
}
654709
}
655710

656711
extension ChannelPipeline.SynchronousOperations {

0 commit comments

Comments
 (0)