From 06d4028cde1bd19e673a43c021fa05b9665d712f Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 3 Nov 2022 14:41:42 +0100 Subject: [PATCH] Add missing `Sendable` requirements (#2305) --- .../NIOWebSocketServerUpgrader.swift | 68 +++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift b/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift index 7b68cc728..4d1f77f6a 100644 --- a/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift +++ b/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift @@ -64,7 +64,15 @@ fileprivate extension HTTPHeaders { public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unchecked Sendable { // This type *is* Sendable but we can't express that properly until Swift 5.7. In the meantime // the conformance is `@unchecked`. - + + #if swift(>=5.7) + // FIXME: remove @unchecked when 5.7 is the minimum supported version. + private typealias ShouldUpgrade = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture + private typealias UpgradePipelineHandler = @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture + #else + private typealias ShouldUpgrade = (Channel, HTTPRequestHead) -> EventLoopFuture + private typealias UpgradePipelineHandler = (Channel, HTTPRequestHead) -> EventLoopFuture + #endif /// RFC 6455 specs this as the required entry in the Upgrade header. public let supportedProtocol: String = "websocket" @@ -73,13 +81,12 @@ public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unch /// which NIO requires. We check for these manually. public let requiredUpgradeHeaders: [String] = [] - private let shouldUpgrade: (Channel, HTTPRequestHead) -> EventLoopFuture - private let upgradePipelineHandler: (Channel, HTTPRequestHead) -> EventLoopFuture + private let shouldUpgrade: ShouldUpgrade + private let upgradePipelineHandler: UpgradePipelineHandler private let maxFrameSize: Int private let automaticErrorHandling: Bool #if swift(>=5.7) - // FIXME: remove @unchecked when 5.7 is the minimum supported version. /// Create a new `NIOWebSocketServerUpgrader`. /// /// - parameters: @@ -132,6 +139,7 @@ public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unch } #endif + #if swift(>=5.7) /// Create a new `NIOWebSocketServerUpgrader`. /// /// - parameters: @@ -152,11 +160,61 @@ public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unch /// websocket protocol. This only needs to add the user handlers: the /// `WebSocketFrameEncoder` and `WebSocketFrameDecoder` will have been added to the /// pipeline automatically. - public init( + @preconcurrency + public convenience init( + maxFrameSize: Int, + automaticErrorHandling: Bool = true, + shouldUpgrade: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture, + upgradePipelineHandler: @escaping @Sendable (Channel, HTTPRequestHead) -> EventLoopFuture + ) { + self.init( + _maxFrameSize: maxFrameSize, + automaticErrorHandling: automaticErrorHandling, + shouldUpgrade: shouldUpgrade, + upgradePipelineHandler: upgradePipelineHandler + ) + } + #else + /// Create a new `NIOWebSocketServerUpgrader`. + /// + /// - parameters: + /// - maxFrameSize: The maximum frame size the decoder is willing to tolerate from the + /// remote peer. WebSockets in principle allows frame sizes up to `2**64` bytes, but + /// this is an objectively unreasonable maximum value (on AMD64 systems it is not + /// possible to even. Users may set this to any value up to `UInt32.max`. + /// - automaticErrorHandling: Whether the pipeline should automatically handle protocol + /// errors by sending error responses and closing the connection. Defaults to `true`, + /// may be set to `false` if the user wishes to handle their own errors. + /// - shouldUpgrade: A callback that determines whether the websocket request should be + /// upgraded. This callback is responsible for creating a `HTTPHeaders` object with + /// any headers that it needs on the response *except for* the `Upgrade`, `Connection`, + /// and `Sec-WebSocket-Accept` headers, which this upgrader will handle. Should return + /// an `EventLoopFuture` containing `nil` if the upgrade should be refused. + /// - upgradePipelineHandler: A function that will be called once the upgrade response is + /// flushed, and that is expected to mutate the `Channel` appropriately to handle the + /// websocket protocol. This only needs to add the user handlers: the + /// `WebSocketFrameEncoder` and `WebSocketFrameDecoder` will have been added to the + /// pipeline automatically. + public convenience init( maxFrameSize: Int, automaticErrorHandling: Bool = true, shouldUpgrade: @escaping (Channel, HTTPRequestHead) -> EventLoopFuture, upgradePipelineHandler: @escaping (Channel, HTTPRequestHead) -> EventLoopFuture + ) { + self.init( + _maxFrameSize: maxFrameSize, + automaticErrorHandling: automaticErrorHandling, + shouldUpgrade: shouldUpgrade, + upgradePipelineHandler: upgradePipelineHandler + ) + } + #endif + + private init( + _maxFrameSize maxFrameSize: Int, + automaticErrorHandling: Bool, + shouldUpgrade: @escaping ShouldUpgrade, + upgradePipelineHandler: @escaping UpgradePipelineHandler ) { precondition(maxFrameSize <= UInt32.max, "invalid overlarge max frame size") self.shouldUpgrade = shouldUpgrade