mirror of
https://github.com/apple/swift-nio
synced 2026-11-29 05:24:21 +00:00
Let Upgrade handler remove other HTTP handlers. (#144)
Motivation: The HTTP pipeline may contain other HTTP protocol specific handlers, beyond just the ones that the upgrade handler knows explicitly. It should be possible to get the upgrade handler to remove those at the right time. Modifications: Add extra support for removing handlers. Result: It'll be possible for the HTTP upgrade logic to remove things like the pipelining handler at upgrade time.
This commit is contained in:
1 parent
25dd80aa9f
commit
79a87ab76a
3 files changed
+131
-105
No files matched your search
@@ -67,8 +67,8 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
private let upgraders: [String: HTTPProtocolUpgrader]
|
||||
private let upgradeCompletionHandler: (ChannelHandlerContext) -> Void
|
||||
|
||||
private let httpDecoder: HTTPRequestDecoder?
|
||||
private let httpEncoder: HTTPResponseEncoder?
|
||||
private let extraHTTPHandlers: [ChannelHandler]
|
||||
|
||||
/// Whether we've already seen the first request.
|
||||
private var seenFirstRequest = false
|
||||
@@ -91,6 +91,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
/// Pass `nil` to this parameter if for any reason you want to keep the `HTTPRequestDecoder` in
|
||||
/// the pipeline after upgrade.
|
||||
/// - Parameter upgradeCompletionHandler: A block that will be fired when HTTP upgrade is complete.
|
||||
@available(*, deprecated, message: "Please use init(upgraders:httpEncoder:extraHTTPHandlers:upgradeCompletionHandler:)")
|
||||
public init(upgraders: [HTTPProtocolUpgrader], httpEncoder: HTTPResponseEncoder?, httpDecoder: HTTPRequestDecoder?, upgradeCompletionHandler: @escaping (ChannelHandlerContext) -> Void) {
|
||||
var upgraderMap = [String: HTTPProtocolUpgrader]()
|
||||
for upgrader in upgraders {
|
||||
@@ -98,8 +99,35 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
}
|
||||
self.upgraders = upgraderMap
|
||||
self.upgradeCompletionHandler = upgradeCompletionHandler
|
||||
self.httpDecoder = httpDecoder
|
||||
self.httpEncoder = httpEncoder
|
||||
|
||||
if let decoder = httpDecoder {
|
||||
self.extraHTTPHandlers = [decoder]
|
||||
} else {
|
||||
self.extraHTTPHandlers = []
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a `HTTPServerUpgradeHandler`.
|
||||
///
|
||||
/// - Parameter upgraders: All `HTTPProtocolUpgrader` objects that this pipeline will be able
|
||||
/// to use to handle HTTP upgrade.
|
||||
/// - Parameter httpEncoder: The `HTTPResponseEncoder` encoding responses from this handler and which will
|
||||
/// be removed from the pipeline once the upgrade response is sent. This is used to ensure
|
||||
/// that the pipeline will be in a clean state after upgrade.
|
||||
/// - Parameter extraHTTPHandlers: Any other handlers that are directly related to handling HTTP. At the very least
|
||||
/// this should include the `HTTPDecoder`, but should also include any other handler that cannot tolerate
|
||||
/// receiving non-HTTP data.
|
||||
/// - Parameter upgradeCompletionHandler: A block that will be fired when HTTP upgrade is complete.
|
||||
public init(upgraders: [HTTPProtocolUpgrader], httpEncoder: HTTPResponseEncoder, extraHTTPHandlers: [ChannelHandler], upgradeCompletionHandler: @escaping (ChannelHandlerContext) -> Void) {
|
||||
var upgraderMap = [String: HTTPProtocolUpgrader]()
|
||||
for upgrader in upgraders {
|
||||
upgraderMap[upgrader.supportedProtocol] = upgrader
|
||||
}
|
||||
self.upgraders = upgraderMap
|
||||
self.upgradeCompletionHandler = upgradeCompletionHandler
|
||||
self.httpEncoder = httpEncoder
|
||||
self.extraHTTPHandlers = extraHTTPHandlers
|
||||
}
|
||||
|
||||
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
|
||||
@@ -167,15 +195,16 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
// We are now upgrading, any further data should be buffered and replayed.
|
||||
self.upgrading = true
|
||||
|
||||
// Before we finish the upgrade we have to remove the HTTPDecoder from the
|
||||
// pipeline, to prevent it parsing any more data. We'll buffer the data until that completes.
|
||||
// Before we finish the upgrade we have to remove the HTTPDecoder and any other non-Encoder HTTP
|
||||
// handlers from the pipeline, to prevent them parsing any more data. We'll buffer the data until
|
||||
// that completes.
|
||||
// While there are a lot of Futures involved here it's quite possible that all of this code will
|
||||
// actually complete synchronously: we just want to program for the possibility that it won't.
|
||||
// Once that's done, we send the upgrade response, then remove the HTTP encoder, then call the
|
||||
// internal handler, then call the user code, and then finally when the user code is done we do
|
||||
// our final cleanup steps, namely we replay the received data we buffered in the meantime and
|
||||
// then remove ourselves from the pipeline.
|
||||
_ = self.removeHandler(ctx: ctx, handler: self.httpDecoder).then { (_: Bool) in
|
||||
_ = self.removeExtraHandlers(ctx: ctx).then {
|
||||
self.sendUpgradeResponse(ctx: ctx, upgradeRequest: request, responseHeaders: responseHeaders)
|
||||
}.then {
|
||||
self.removeHandler(ctx: ctx, handler: self.httpEncoder)
|
||||
@@ -233,4 +262,14 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler {
|
||||
return ctx.eventLoop.newSucceededFuture(result: true)
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes any extra HTTP-related handlers from the channel pipeline.
|
||||
private func removeExtraHandlers(ctx: ChannelHandlerContext) -> EventLoopFuture<Void> {
|
||||
guard self.extraHTTPHandlers.count > 0 else {
|
||||
return ctx.eventLoop.newSucceededFuture(result: ())
|
||||
}
|
||||
|
||||
return EventLoopFuture<Void>.andAll(self.extraHTTPHandlers.map { ctx.pipeline.remove(handler: $0).map { (_: Bool) in () }},
|
||||
eventLoop: ctx.eventLoop)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user