From 0abf7eb929a21bb3ef30aa570c5027c1c2ad905d Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Mon, 4 Jul 2022 09:37:23 +0200 Subject: [PATCH] Adopt `Sendable` for types in `NIOHTTP1` (#2213) --- Sources/NIOHTTP1/HTTPDecoder.swift | 11 +++++++--- Sources/NIOHTTP1/HTTPEncoder.swift | 10 ++++++++++ .../NIOHTTP1/HTTPServerPipelineHandler.swift | 5 +++++ .../HTTPServerProtocolErrorHandler.swift | 5 +++++ .../NIOHTTP1/HTTPServerUpgradeHandler.swift | 6 +++++- Sources/NIOHTTP1/HTTPTypes.swift | 20 +++++++++++++++---- .../NIOHTTPClientUpgradeHandler.swift | 4 ++++ .../NIOHTTP1/NIOHTTPObjectAggregator.swift | 16 ++++++++++++--- 8 files changed, 66 insertions(+), 11 deletions(-) diff --git a/Sources/NIOHTTP1/HTTPDecoder.swift b/Sources/NIOHTTP1/HTTPDecoder.swift index 9cbd9f906..caadc9696 100644 --- a/Sources/NIOHTTP1/HTTPDecoder.swift +++ b/Sources/NIOHTTP1/HTTPDecoder.swift @@ -442,7 +442,7 @@ public typealias HTTPResponseDecoder = HTTPDecoder -public enum HTTPDecoderKind { +public enum HTTPDecoderKind: NIOSendable { case request case response } @@ -697,8 +697,13 @@ public final class HTTPDecoder: ByteToMessageDecoder, HTTPDecoderDelega } } +#if swift(>=5.6) +@available(*, unavailable) +extension HTTPDecoder: Sendable {} +#endif + /// Strategy to use when a HTTPDecoder is removed from a pipeline after a HTTP upgrade was detected. -public enum RemoveAfterUpgradeStrategy { +public enum RemoveAfterUpgradeStrategy: NIOSendable { /// Forward all the remaining bytes that are currently buffered in the deccoder to the next handler in the pipeline. case forwardBytes /// Fires a `ByteToMessageDecoder.leftoverDataWhenDone` error through the pipeline @@ -708,7 +713,7 @@ public enum RemoveAfterUpgradeStrategy { } /// Strategy to use when a HTTPDecoder receives an informational HTTP response (1xx except 101) -public struct NIOInformationalResponseStrategy: Hashable { +public struct NIOInformationalResponseStrategy: Hashable, NIOSendable { enum Base { case drop case forward diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index 16956886f..810618f4e 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -166,6 +166,11 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler, RemovableChannelH } } +#if swift(>=5.6) +@available(*, unavailable) +extension HTTPRequestEncoder: Sendable {} +#endif + /// A `ChannelOutboundHandler` that can serialize HTTP responses. /// /// This channel handler is used to translate messages from a series of @@ -198,6 +203,11 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel } } +#if swift(>=5.6) +@available(*, unavailable) +extension HTTPResponseEncoder: Sendable {} +#endif + private extension ByteBuffer { private mutating func write(status: HTTPResponseStatus) { self.writeString(String(status.code)) diff --git a/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift b/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift index b2860dee7..509402c64 100644 --- a/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift +++ b/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift @@ -470,3 +470,8 @@ public final class HTTPServerPipelineHandler: ChannelDuplexHandler, RemovableCha } } } + +#if swift(>=5.6) +@available(*, unavailable) +extension HTTPServerPipelineHandler: Sendable {} +#endif diff --git a/Sources/NIOHTTP1/HTTPServerProtocolErrorHandler.swift b/Sources/NIOHTTP1/HTTPServerProtocolErrorHandler.swift index c48cdccf9..1ef25994c 100644 --- a/Sources/NIOHTTP1/HTTPServerProtocolErrorHandler.swift +++ b/Sources/NIOHTTP1/HTTPServerProtocolErrorHandler.swift @@ -71,3 +71,8 @@ public final class HTTPServerProtocolErrorHandler: ChannelDuplexHandler, Removab context.write(data, promise: promise) } } + +#if swift(>=5.6) +@available(*, unavailable) +extension HTTPServerProtocolErrorHandler: Sendable {} +#endif diff --git a/Sources/NIOHTTP1/HTTPServerUpgradeHandler.swift b/Sources/NIOHTTP1/HTTPServerUpgradeHandler.swift index 4e16419ce..5abc2d3ee 100644 --- a/Sources/NIOHTTP1/HTTPServerUpgradeHandler.swift +++ b/Sources/NIOHTTP1/HTTPServerUpgradeHandler.swift @@ -19,7 +19,7 @@ public enum HTTPServerUpgradeErrors: Error { } /// User events that may be fired by the `HTTPServerProtocolUpgrader`. -public enum HTTPServerUpgradeEvents { +public enum HTTPServerUpgradeEvents: NIOSendable { /// Fired when HTTP upgrade has completed and the /// `HTTPServerProtocolUpgrader` is about to remove itself from the /// `ChannelPipeline`. @@ -320,6 +320,10 @@ public final class HTTPServerUpgradeHandler: ChannelInboundHandler, RemovableCha } } +#if swift(>=5.5) && canImport(_Concurrency) +extension HTTPServerUpgradeHandler: @unchecked Sendable {} +#endif + extension HTTPServerUpgradeHandler { /// The state of the upgrade handler. private enum UpgradeState { diff --git a/Sources/NIOHTTP1/HTTPTypes.swift b/Sources/NIOHTTP1/HTTPTypes.swift index e382bba62..5272c6e7e 100644 --- a/Sources/NIOHTTP1/HTTPTypes.swift +++ b/Sources/NIOHTTP1/HTTPTypes.swift @@ -123,6 +123,10 @@ public struct HTTPRequestHead: Equatable { } } +#if swift(>=5.5) && canImport(_Concurrency) +extension HTTPRequestHead: @unchecked Sendable {} +#endif + /// The parts of a complete HTTP message, either request or response. /// /// A HTTP message is made up of a request or status line with several headers, @@ -135,6 +139,10 @@ public enum HTTPPart { case end(HTTPHeaders?) } +#if swift(>=5.5) && canImport(_Concurrency) +extension HTTPPart: Sendable where HeadT: Sendable, BodyT: Sendable {} +#endif + extension HTTPPart: Equatable {} /// The components of a HTTP request from the view of a HTTP client. @@ -228,6 +236,10 @@ public struct HTTPResponseHead: Equatable { } } +#if swift(>=5.5) && canImport(_Concurrency) +extension HTTPResponseHead: @unchecked Sendable {} +#endif + extension HTTPResponseHead { /// Determines if the head is purely informational. If a head is informational another head will follow this /// head eventually. @@ -286,7 +298,7 @@ extension HTTPHeaders { /// field when needed. It also supports recomposing headers to a maximally joined /// or split representation, such that header fields that are able to be repeated /// can be represented appropriately. -public struct HTTPHeaders: CustomStringConvertible, ExpressibleByDictionaryLiteral { +public struct HTTPHeaders: CustomStringConvertible, ExpressibleByDictionaryLiteral, NIOSendable { @usableFromInline internal var headers: [(String, String)] internal var keepAliveState: KeepAliveState = .unknown @@ -601,7 +613,7 @@ extension HTTPHeaders: Equatable { } } -public enum HTTPMethod: Equatable { +public enum HTTPMethod: Equatable, NIOSendable { internal enum HasBody { case yes case no @@ -660,7 +672,7 @@ public enum HTTPMethod: Equatable { } /// A structure representing a HTTP version. -public struct HTTPVersion: Equatable { +public struct HTTPVersion: Equatable, NIOSendable { /// Create a HTTP version. /// /// - Parameter major: The major version number. @@ -1056,7 +1068,7 @@ extension HTTPResponseStatus { } /// A HTTP response status code. -public enum HTTPResponseStatus { +public enum HTTPResponseStatus: NIOSendable { /* use custom if you want to use a non-standard response code or have it available in a (UInt, String) pair from a higher-level web framework. */ case custom(code: UInt, reasonPhrase: String) diff --git a/Sources/NIOHTTP1/NIOHTTPClientUpgradeHandler.swift b/Sources/NIOHTTP1/NIOHTTPClientUpgradeHandler.swift index e2b082dfb..7a08b237b 100644 --- a/Sources/NIOHTTP1/NIOHTTPClientUpgradeHandler.swift +++ b/Sources/NIOHTTP1/NIOHTTPClientUpgradeHandler.swift @@ -369,6 +369,10 @@ public final class NIOHTTPClientUpgradeHandler: ChannelDuplexHandler, RemovableC } } +#if swift(>=5.5) && canImport(_Concurrency) +extension NIOHTTPClientUpgradeHandler: @unchecked Sendable {} +#endif + extension NIOHTTPClientUpgradeHandler { /// The state of the upgrade handler. fileprivate enum UpgradeState { diff --git a/Sources/NIOHTTP1/NIOHTTPObjectAggregator.swift b/Sources/NIOHTTP1/NIOHTTPObjectAggregator.swift index fec32d478..2c42a7433 100644 --- a/Sources/NIOHTTP1/NIOHTTPObjectAggregator.swift +++ b/Sources/NIOHTTP1/NIOHTTPObjectAggregator.swift @@ -17,7 +17,7 @@ import NIOCore /// /// A full HTTP request is made up of a response header encoded by `.head` /// and an optional `.body`. -public struct NIOHTTPServerRequestFull { +public struct NIOHTTPServerRequestFull: NIOSendable { public var head: HTTPRequestHead public var body: ByteBuffer? @@ -33,7 +33,7 @@ extension NIOHTTPServerRequestFull: Equatable {} /// /// A full HTTP response is made up of a response header encoded by `.head` /// and an optional `.body`. -public struct NIOHTTPClientResponseFull { +public struct NIOHTTPClientResponseFull: NIOSendable { public var head: HTTPResponseHead public var body: ByteBuffer? @@ -69,7 +69,7 @@ public struct NIOHTTPObjectAggregatorError: Error, Equatable { public static let unexpectedMessageEnd = NIOHTTPObjectAggregatorError(base: .unexpectedMessageEnd) } -public struct NIOHTTPObjectAggregatorEvent: Hashable { +public struct NIOHTTPObjectAggregatorEvent: Hashable, NIOSendable { private enum Base { case httpExpectationFailed case httpFrameTooLong @@ -301,6 +301,11 @@ public final class NIOHTTPServerRequestAggregator: ChannelInboundHandler, Remova } } +#if swift(>=5.6) +@available(*, unavailable) +extension NIOHTTPServerRequestAggregator: Sendable {} +#endif + /// A `ChannelInboundHandler` that handles HTTP chunked `HTTPClientResponsePart` /// messages by aggregating individual message chunks into a single /// `NIOHTTPClientResponseFull`. @@ -397,3 +402,8 @@ public final class NIOHTTPClientResponseAggregator: ChannelInboundHandler, Remov self.buffer = context.channel.allocator.buffer(capacity: 0) } } + +#if swift(>=5.6) +@available(*, unavailable) +extension NIOHTTPClientResponseAggregator: Sendable {} +#endif