mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
HTTPUpgradeHandler should accept Futures for upgraders. (#10)
Motivation: Currently the HTTPUpgradeHandler does not allow for the protocol upgraders to return EventLoopFuture objects. This leads to an awkward scenario: it's essentially required that an upgrader be able to synchronously change the pipeline to its satisfaction and prepare for more bytes. This is not really possible: ChannelPipeline.add(handler:) returns a Future, which means that it is possible for this operation, at least in principle, to not execute synchronously. This reality puts the HTTPUpgradeHandler at odds with the reality of channel pipelines, and cannot stand. Modifications: Give the HTTPProtocolUpgrader protocol to have upgrade() return an EventLoopFuture<Void>, and update the HTTPServerUpgradeHandler to appropriately handle the returned future by buffering until the future completes. Result: It is no longer necessary to synchronously change the channel pipeline during protocol upgrade.
This commit is contained in:
1 parent
31a693fd93
commit
2a3396998d
4 files changed
+310
-20
No files matched your search
@@ -131,6 +131,29 @@ public extension ChannelPipeline {
|
||||
return addHandlers(HTTPRequestEncoder(), HTTPResponseDecoder(), first: first)
|
||||
}
|
||||
|
||||
/// Configure a `ChannelPipeline` for use as a HTTP server that can perform a HTTP
|
||||
/// upgrade to a non-HTTP protocol: that is, after upgrade the channel pipeline must
|
||||
/// have none of the handlers added by this function in it.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - first: Whether to add the HTTP server at the head of the channel pipeline,
|
||||
/// or at the tail.
|
||||
/// - upgraders: The HTTP protocol upgraders to offer.
|
||||
/// - upgradeCompletionHandler: A block that will be fired when the HTTP upgrade is
|
||||
/// complete.
|
||||
/// - returns: An `EventLoopFuture` that will fire when the pipeline is configured.
|
||||
public func addHTTPServerHandlersWithUpgrader(first: Bool = false,
|
||||
upgraders: [HTTPProtocolUpgrader],
|
||||
_ upgradeCompletionHandler: @escaping (ChannelHandlerContext) -> Void) -> EventLoopFuture<Void> {
|
||||
let responseEncoder = HTTPResponseEncoder()
|
||||
let requestDecoder = HTTPRequestDecoder()
|
||||
let upgrader = HTTPServerUpgradeHandler(upgraders: upgraders,
|
||||
httpEncoder: responseEncoder,
|
||||
httpDecoder: requestDecoder,
|
||||
upgradeCompletionHandler: upgradeCompletionHandler)
|
||||
return addHandlers(responseEncoder, requestDecoder, upgrader, first: first)
|
||||
}
|
||||
|
||||
/// Adds the provided channel handlers to the pipeline in the order given, taking account
|
||||
/// of the behaviour of `ChannelHandler.add(first:)`.
|
||||
private func addHandlers(_ handlers: ChannelHandler..., first: Bool) -> EventLoopFuture<Void> {
|
||||
|
||||
Reference in new issue
Block a user