From 702cd7c56d5d44eeba73fdf83918339b26dc855c Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Thu, 16 Nov 2023 11:30:21 +0000 Subject: [PATCH] Fix the typed HTTP upgrade compiler guards (#2594) The compiler guards were unnecessarily complex and I also didn't cover 3 methods where we used the types. --- Sources/NIOHTTP1/HTTPTypedPipelineSetup.swift | 2 +- .../NIOHTTP1/NIOTypedHTTPClientUpgradeHandler.swift | 2 +- .../NIOTypedHTTPClientUpgraderStateMachine.swift | 2 +- .../NIOHTTP1/NIOTypedHTTPServerUpgradeHandler.swift | 2 +- .../NIOTypedHTTPServerUpgraderStateMachine.swift | 2 +- .../NIOWebSocket/NIOWebSocketClientUpgrader.swift | 2 +- .../NIOWebSocket/NIOWebSocketServerUpgrader.swift | 2 +- Tests/NIOHTTP1Tests/HTTPClientUpgradeTests.swift | 10 +++++++--- Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift | 12 ++++++++++-- .../WebSocketClientEndToEndTests.swift | 2 +- .../WebSocketServerEndToEndTests.swift | 2 +- 11 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Sources/NIOHTTP1/HTTPTypedPipelineSetup.swift b/Sources/NIOHTTP1/HTTPTypedPipelineSetup.swift index 4135203a8e..9021062488 100644 --- a/Sources/NIOHTTP1/HTTPTypedPipelineSetup.swift +++ b/Sources/NIOHTTP1/HTTPTypedPipelineSetup.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) import NIOCore // MARK: - Server pipeline configuration diff --git a/Sources/NIOHTTP1/NIOTypedHTTPClientUpgradeHandler.swift b/Sources/NIOHTTP1/NIOTypedHTTPClientUpgradeHandler.swift index ea76a74b91..c683b61b3e 100644 --- a/Sources/NIOHTTP1/NIOTypedHTTPClientUpgradeHandler.swift +++ b/Sources/NIOHTTP1/NIOTypedHTTPClientUpgradeHandler.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) import NIOCore /// An object that implements `NIOTypedHTTPClientProtocolUpgrader` knows how to handle HTTP upgrade to diff --git a/Sources/NIOHTTP1/NIOTypedHTTPClientUpgraderStateMachine.swift b/Sources/NIOHTTP1/NIOTypedHTTPClientUpgraderStateMachine.swift index 875fb2ce64..6e9c696811 100644 --- a/Sources/NIOHTTP1/NIOTypedHTTPClientUpgraderStateMachine.swift +++ b/Sources/NIOHTTP1/NIOTypedHTTPClientUpgraderStateMachine.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) import DequeModule import NIOCore diff --git a/Sources/NIOHTTP1/NIOTypedHTTPServerUpgradeHandler.swift b/Sources/NIOHTTP1/NIOTypedHTTPServerUpgradeHandler.swift index 1a1a47988c..b6a90b1294 100644 --- a/Sources/NIOHTTP1/NIOTypedHTTPServerUpgradeHandler.swift +++ b/Sources/NIOHTTP1/NIOTypedHTTPServerUpgradeHandler.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) import NIOCore /// An object that implements `NIOTypedHTTPServerProtocolUpgrader` knows how to handle HTTP upgrade to diff --git a/Sources/NIOHTTP1/NIOTypedHTTPServerUpgraderStateMachine.swift b/Sources/NIOHTTP1/NIOTypedHTTPServerUpgraderStateMachine.swift index c4fa19c348..bc2536f7c8 100644 --- a/Sources/NIOHTTP1/NIOTypedHTTPServerUpgraderStateMachine.swift +++ b/Sources/NIOHTTP1/NIOTypedHTTPServerUpgraderStateMachine.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) import DequeModule import NIOCore diff --git a/Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift b/Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift index d1b190c288..a9e456f857 100644 --- a/Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift +++ b/Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift @@ -74,7 +74,7 @@ public final class NIOWebSocketClientUpgrader: NIOHTTPClientProtocolUpgrader { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) /// A `NIOTypedHTTPClientProtocolUpgrader` that knows how to do the WebSocket upgrade dance. /// /// This upgrader assumes that the `HTTPClientUpgradeHandler` will create and send the upgrade request. diff --git a/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift b/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift index 14f29f750b..0672bc4a06 100644 --- a/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift +++ b/Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift @@ -175,7 +175,7 @@ public final class NIOWebSocketServerUpgrader: HTTPServerProtocolUpgrader, @unch } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) /// A `NIOTypedHTTPServerProtocolUpgrader` that knows how to do the WebSocket upgrade dance. /// /// Users may frequently want to offer multiple websocket endpoints on the same port. For this diff --git a/Tests/NIOHTTP1Tests/HTTPClientUpgradeTests.swift b/Tests/NIOHTTP1Tests/HTTPClientUpgradeTests.swift index 195338f9ef..a0cda42f73 100644 --- a/Tests/NIOHTTP1Tests/HTTPClientUpgradeTests.swift +++ b/Tests/NIOHTTP1Tests/HTTPClientUpgradeTests.swift @@ -32,7 +32,7 @@ extension EmbeddedChannel { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) protocol TypedAndUntypedHTTPClientProtocolUpgrader: NIOHTTPClientProtocolUpgrader, NIOTypedHTTPClientProtocolUpgrader where UpgradeResult == Bool {} #else @@ -287,9 +287,13 @@ private final class RecordingHTTPHandler: ChannelInboundHandler, RemovableChanne @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) private func assertPipelineContainsUpgradeHandler(channel: Channel) { let handler = try? channel.pipeline.syncOperations.handler(type: NIOHTTPClientUpgradeHandler.self) - let typedHandler = try? channel.pipeline.syncOperations.handler(type: NIOTypedHTTPClientUpgradeHandler.self) + #if !canImport(Darwin) || swift(>=5.10) + let typedHandler = try? channel.pipeline.syncOperations.handler(type: NIOTypedHTTPClientUpgradeHandler.self) XCTAssertTrue(handler != nil || typedHandler != nil) + #else + XCTAssertTrue(handler != nil) + #endif } @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) @@ -953,7 +957,7 @@ class HTTPClientUpgradeTestCase: XCTestCase { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) final class TypedHTTPClientUpgradeTestCase: HTTPClientUpgradeTestCase { override func setUpClientChannel( diff --git a/Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift b/Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift index 70d55eab55..5b48485751 100644 --- a/Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift +++ b/Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift @@ -36,11 +36,15 @@ extension ChannelPipeline { @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) fileprivate func assertContainsUpgrader() { + #if !canImport(Darwin) || swift(>=5.10) do { _ = try self.context(handlerType: NIOTypedHTTPServerUpgradeHandler.self).wait() } catch { self.assertContains(handlerType: HTTPServerUpgradeHandler.self) } + #else + self.assertContains(handlerType: HTTPServerUpgradeHandler.self) + #endif } func assertContains(handlerType: Handler.Type) { @@ -63,6 +67,7 @@ extension ChannelPipeline { // handler present, keep waiting usleep(50) } catch ChannelPipelineError.notFound { + #if !canImport(Darwin) || swift(>=5.10) // Checking if the typed variant is present do { _ = try self.context(handlerType: NIOTypedHTTPServerUpgradeHandler.self).wait() @@ -72,6 +77,9 @@ extension ChannelPipeline { // No upgrader, we're good. return } + #else + return + #endif } } @@ -174,7 +182,7 @@ internal func assertResponseIs(response: String, expectedResponseLine: String, e XCTAssertEqual(lines.count, 0) } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) protocol TypedAndUntypedHTTPServerProtocolUpgrader: HTTPServerProtocolUpgrader, NIOTypedHTTPServerProtocolUpgrader where UpgradeResult == Bool {} #else @@ -1557,7 +1565,7 @@ class HTTPServerUpgradeTestCase: XCTestCase { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) final class TypedHTTPServerUpgradeTestCase: HTTPServerUpgradeTestCase { fileprivate override func setUpTestWithAutoremoval( diff --git a/Tests/NIOWebSocketTests/WebSocketClientEndToEndTests.swift b/Tests/NIOWebSocketTests/WebSocketClientEndToEndTests.swift index 35b89c02b7..1e64a27544 100644 --- a/Tests/NIOWebSocketTests/WebSocketClientEndToEndTests.swift +++ b/Tests/NIOWebSocketTests/WebSocketClientEndToEndTests.swift @@ -405,7 +405,7 @@ class WebSocketClientEndToEndTests: XCTestCase { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) final class TypedWebSocketClientEndToEndTests: WebSocketClientEndToEndTests { func setUpClientChannel( diff --git a/Tests/NIOWebSocketTests/WebSocketServerEndToEndTests.swift b/Tests/NIOWebSocketTests/WebSocketServerEndToEndTests.swift index 609a4e9325..d73d1f21dc 100644 --- a/Tests/NIOWebSocketTests/WebSocketServerEndToEndTests.swift +++ b/Tests/NIOWebSocketTests/WebSocketServerEndToEndTests.swift @@ -527,7 +527,7 @@ class WebSocketServerEndToEndTests: XCTestCase { } } -#if !canImport(Darwin) || (canImport(Darwin) && swift(>=5.10)) +#if !canImport(Darwin) || swift(>=5.10) @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) final class TypedWebSocketServerEndToEndTests: WebSocketServerEndToEndTests { override func createTestFixtures(