From 01696713076c7e92baa41692ae46e7076cb7cfda Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Thu, 11 Sep 2025 12:28:17 -0400 Subject: [PATCH 1/9] Add AsyncHTTPClient dependency and basic class (#961) --- Package.swift | 2 + .../Networking/Http/NIO/NIOHTTPClient.swift | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift diff --git a/Package.swift b/Package.swift index 754a3ccce..ce3a8fa2a 100644 --- a/Package.swift +++ b/Package.swift @@ -59,6 +59,7 @@ let package = Package( .package(url: "https://github.com/awslabs/aws-crt-swift.git", exact: "0.54.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"), .package(url: "https://github.com/open-telemetry/opentelemetry-swift", from: "1.13.0"), + .package(url: "https://github.com/swift-server/async-http-client.git", from: "1.26.0"), ] let isDocCEnabled = ProcessInfo.processInfo.environment["AWS_SWIFT_SDK_ENABLE_DOCC"] != nil @@ -97,6 +98,7 @@ let package = Package( "SmithyChecksums", "SmithyCBOR", .product(name: "AwsCommonRuntimeKit", package: "aws-crt-swift"), + .product(name: "AsyncHTTPClient", package: "async-http-client"), // Only include these on macOS, iOS, tvOS, watchOS, and macCatalyst (visionOS and Linux are excluded) .product( name: "InMemoryExporter", diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift new file mode 100644 index 000000000..17490b294 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift @@ -0,0 +1,37 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import NIOCore +import NIOPosix +import SmithyHTTPAPI + +/// AsyncHTTPClient-based HTTP client implementation that conforms to SmithyHTTPAPI.HTTPClient +/// This implementation is thread-safe and supports concurrent request execution. +public final class NIOHTTPClient: SmithyHTTPAPI.HTTPClient { + private let client: AsyncHTTPClient.HTTPClient + private let config: HttpClientConfiguration + + /// Creates a new `NIOHTTPClient`. + /// + /// The client is created with its own internal `AsyncHTTPClient`, which is configured with system defaults. + /// - Parameters: + /// - httpClientConfiguration: The configuration to use for the client's `AsyncHTTPClient` setup. + public init( + httpClientConfiguration: HttpClientConfiguration + ) throws { + self.config = httpClientConfiguration + self.client = AsyncHTTPClient.HTTPClient( + configuration: .init() // TODO + ) + } + + public func send(request: SmithyHTTPAPI.HTTPRequest) async throws -> SmithyHTTPAPI.HTTPResponse { + // TODO + return HTTPResponse() + } +} From c8ca8d73475344ec0ee1e3a684894bb9a6a699df Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Mon, 15 Sep 2025 14:55:37 -0400 Subject: [PATCH 2/9] Add basic configuration extension (#962) --- ...onfiguration+HTTPClientConfiguration.swift | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientConfiguration+HTTPClientConfiguration.swift diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientConfiguration+HTTPClientConfiguration.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientConfiguration+HTTPClientConfiguration.swift new file mode 100644 index 000000000..2d6df4e77 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientConfiguration+HTTPClientConfiguration.swift @@ -0,0 +1,40 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import NIOCore + +extension HTTPClient.Configuration { + + static func from(httpClientConfiguration: HttpClientConfiguration) -> HTTPClient.Configuration { + let connect: TimeAmount? = httpClientConfiguration.connectTimeout != nil + ? .seconds(Int64(httpClientConfiguration.connectTimeout!)) + : nil + + let read: TimeAmount? = .seconds(Int64(httpClientConfiguration.socketTimeout)) + + let timeout = HTTPClient.Configuration.Timeout( + connect: connect, + read: read + ) + + let pool = HTTPClient.Configuration.ConnectionPool( + idleTimeout: .seconds(60), // default + concurrentHTTP1ConnectionsPerHostSoftLimit: httpClientConfiguration.maxConnections + ) + + return .init( + tlsConfiguration: nil, // TODO + redirectConfiguration: nil, + timeout: timeout, + connectionPool: pool, + proxy: nil, + ignoreUncleanSSLShutdown: false, + decompression: .disabled + ) + } +} From 00f8d2c3a4855f2f3b9f61db7fd26eb4890a54be Mon Sep 17 00:00:00 2001 From: Josh Elkins Date: Mon, 15 Sep 2025 15:21:31 -0500 Subject: [PATCH 3/9] chore: Use iPhone 17 sim on Xcode 26 (#963) --- .github/workflows/continuous-integration.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 8b02bd483..4b989eea0 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -25,7 +25,7 @@ jobs: - Xcode_26.0 destination: - 'platform=iOS Simulator,OS=17.2,name=iPhone 15' - - 'platform=iOS Simulator,OS=26.0,name=iPhone 16' + - 'platform=iOS Simulator,OS=26.0,name=iPhone 17' - 'platform=tvOS Simulator,OS=17.2,name=Apple TV 4K (3rd generation) (at 1080p)' - 'platform=tvOS Simulator,OS=26.0,name=Apple TV 4K (3rd generation) (at 1080p)' - 'platform=visionOS Simulator,OS=1.0,name=Apple Vision Pro' @@ -48,7 +48,7 @@ jobs: # Don't run new simulators with old Xcode - destination: 'platform=tvOS Simulator,OS=26.0,name=Apple TV 4K (3rd generation) (at 1080p)' xcode: Xcode_15.2 - - destination: 'platform=iOS Simulator,OS=26.0,name=iPhone 16' + - destination: 'platform=iOS Simulator,OS=26.0,name=iPhone 17' xcode: Xcode_15.2 - destination: 'platform=visionOS Simulator,OS=26.0,name=Apple Vision Pro' xcode: Xcode_15.2 @@ -109,7 +109,7 @@ jobs: - Xcode_26.0 destination: - 'platform=iOS Simulator,OS=17.2,name=iPhone 15' - - 'platform=iOS Simulator,OS=26.0,name=iPhone 16' + - 'platform=iOS Simulator,OS=26.0,name=iPhone 17' - 'platform=tvOS Simulator,OS=17.2,name=Apple TV 4K (3rd generation) (at 1080p)' - 'platform=tvOS Simulator,OS=26.0,name=Apple TV 4K (3rd generation) (at 1080p)' - 'platform=visionOS Simulator,OS=1.0,name=Apple Vision Pro' @@ -132,7 +132,7 @@ jobs: # Don't run new simulators with old Xcode - destination: 'platform=tvOS Simulator,OS=26.0,name=Apple TV 4K (3rd generation) (at 1080p)' xcode: Xcode_15.2 - - destination: 'platform=iOS Simulator,OS=26.0,name=iPhone 16' + - destination: 'platform=iOS Simulator,OS=26.0,name=iPhone 17' xcode: Xcode_15.2 - destination: 'platform=visionOS Simulator,OS=26.0,name=Apple Vision Pro' xcode: Xcode_15.2 From 7e0e80df5b6037f42ef1cbebc4db9f77aebb5792 Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Mon, 15 Sep 2025 17:17:30 -0400 Subject: [PATCH 4/9] Add initial stream bridge --- .../Http/NIO/NIOHTTPClientError.swift | 0 .../Http/NIO/NIOHTTPClientStreamBridge.swift | 143 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift new file mode 100644 index 000000000..e69de29bb diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift new file mode 100644 index 000000000..887946732 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift @@ -0,0 +1,143 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import Foundation +import NIO +import Smithy +import SmithyStreams + +/// Handles streaming between Smithy streams and AsyncHTTPClient +final class NIOHTTPClientStreamBridge { + + /// Convert Smithy ByteStream to AsyncHTTPClient request body + static func convertRequestBody( + from body: ByteStream, + allocator: ByteBufferAllocator + ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { + switch body { + case .noStream: + // No body to send + return .bytes(allocator.buffer(capacity: 0)) + + case .data(let data): + // Convert Data to ByteBuffer + if let data = data { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return .bytes(buffer) + } else { + return .bytes(allocator.buffer(capacity: 0)) + } + + case .stream(let stream): + // Handle streaming request body + return try await convertStreamToRequestBody(stream: stream, allocator: allocator) + } + } + + /// Convert AsyncHTTPClient response body to Smithy ByteStream + static func convertResponseBody( + from response: AsyncHTTPClient.HTTPClientResponse + ) -> ByteStream { + let bufferedStream = BufferedStream() + + // Start a background task to stream data from AsyncHTTPClient to BufferedStream + Task { + do { + var iterator = response.body.makeAsyncIterator() + while let buffer = try await iterator.next() { + // Convert ByteBuffer to Data and write to buffered stream + if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { + let data = Data(bytes) + try bufferedStream.write(contentsOf: data) + } + } + bufferedStream.close() + } catch { + bufferedStream.closeWithError(error) + } + } + + return .stream(bufferedStream) + } + + /// Convert a Smithy Stream to AsyncHTTPClient request body + private static func convertStreamToRequestBody( + stream: Smithy.Stream, + allocator: ByteBufferAllocator + ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { + if let streamLength = stream.length { + let asyncSequence = StreamToAsyncSequence(stream: stream, allocator: allocator) + return .stream(asyncSequence, length: .known(Int64(streamLength))) + } else { + do { + let data = try await stream.readToEndAsync() + if let data = data { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return .bytes(buffer) + } else { + return .bytes(allocator.buffer(capacity: 0)) + } + } catch { + throw NIOHTTPClientError.streamingError(error) + } + } + } +} + +/// AsyncSequence adapter that converts a Smithy Stream to ByteBuffer sequence for AsyncHTTPClient +internal struct StreamToAsyncSequence: AsyncSequence, Sendable { + typealias Element = ByteBuffer + + private let stream: Smithy.Stream + private let allocator: ByteBufferAllocator + + init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { + self.stream = stream + self.allocator = allocator + } + + func makeAsyncIterator() -> AsyncIterator { + AsyncIterator(stream: stream, allocator: allocator) + } + + struct AsyncIterator: AsyncIteratorProtocol { + private let stream: Smithy.Stream + private let allocator: ByteBufferAllocator + private var isFinished = false + + init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { + self.stream = stream + self.allocator = allocator + } + + mutating func next() async throws -> ByteBuffer? { + guard !isFinished else { return nil } + + do { + // Read a chunk from the stream (using default chunk size) + let data = try await stream.readAsync(upToCount: CHUNK_SIZE_BYTES) + + if let data = data, !data.isEmpty { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return buffer + } else { + isFinished = true + stream.close() + return nil + } + } catch { + isFinished = true + stream.close() + throw NIOHTTPClientError.streamingError(error) + } + } + } +} From b38a6a17c0f94b5fc3deddb8be1d97bf0f40107d Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Mon, 15 Sep 2025 17:18:18 -0400 Subject: [PATCH 5/9] Add initial error enum for specific client errors --- .../Networking/Http/NIO/NIOHTTPClientError.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift index e69de29bb..118600cc4 100644 --- a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift @@ -0,0 +1,12 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +/// Errors that are particular to the NIO-based Smithy HTTP client. +public enum NIOHTTPClientError: Error { + + case streamingError(Error) +} From 7c909eccb5c7705e1846c526a64032798def97fd Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Tue, 23 Sep 2025 15:58:57 -0400 Subject: [PATCH 6/9] Address comments --- .../Http/NIO/NIOHTTPClientStreamBridge.swift | 49 +++--- .../NIO/NIOHTTPClientStreamBridgeTests.swift | 142 ++++++++++++++++++ 2 files changed, 168 insertions(+), 23 deletions(-) create mode 100644 Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift index 887946732..d58ef0d55 100644 --- a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift @@ -17,7 +17,8 @@ final class NIOHTTPClientStreamBridge { /// Convert Smithy ByteStream to AsyncHTTPClient request body static func convertRequestBody( from body: ByteStream, - allocator: ByteBufferAllocator + allocator: ByteBufferAllocator, + chunkSize: Int = CHUNK_SIZE_BYTES ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { switch body { case .noStream: @@ -36,31 +37,28 @@ final class NIOHTTPClientStreamBridge { case .stream(let stream): // Handle streaming request body - return try await convertStreamToRequestBody(stream: stream, allocator: allocator) + return try await convertStreamToRequestBody(stream: stream, allocator: allocator, chunkSize: chunkSize) } } /// Convert AsyncHTTPClient response body to Smithy ByteStream static func convertResponseBody( from response: AsyncHTTPClient.HTTPClientResponse - ) -> ByteStream { + ) async -> ByteStream { let bufferedStream = BufferedStream() - // Start a background task to stream data from AsyncHTTPClient to BufferedStream - Task { - do { - var iterator = response.body.makeAsyncIterator() - while let buffer = try await iterator.next() { - // Convert ByteBuffer to Data and write to buffered stream - if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { - let data = Data(bytes) - try bufferedStream.write(contentsOf: data) - } + do { + var iterator = response.body.makeAsyncIterator() + while let buffer = try await iterator.next() { + // Convert ByteBuffer to Data and write to buffered stream + if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { + let data = Data(bytes) + try bufferedStream.write(contentsOf: data) } - bufferedStream.close() - } catch { - bufferedStream.closeWithError(error) } + bufferedStream.close() + } catch { + bufferedStream.closeWithError(error) } return .stream(bufferedStream) @@ -69,10 +67,11 @@ final class NIOHTTPClientStreamBridge { /// Convert a Smithy Stream to AsyncHTTPClient request body private static func convertStreamToRequestBody( stream: Smithy.Stream, - allocator: ByteBufferAllocator + allocator: ByteBufferAllocator, + chunkSize: Int = CHUNK_SIZE_BYTES ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { if let streamLength = stream.length { - let asyncSequence = StreamToAsyncSequence(stream: stream, allocator: allocator) + let asyncSequence = StreamToAsyncSequence(stream: stream, allocator: allocator, chunkSize: chunkSize) return .stream(asyncSequence, length: .known(Int64(streamLength))) } else { do { @@ -97,32 +96,36 @@ internal struct StreamToAsyncSequence: AsyncSequence, Sendable { private let stream: Smithy.Stream private let allocator: ByteBufferAllocator + private let chunkSize: Int - init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { + init(stream: Smithy.Stream, allocator: ByteBufferAllocator, chunkSize: Int = CHUNK_SIZE_BYTES) { self.stream = stream self.allocator = allocator + self.chunkSize = chunkSize } func makeAsyncIterator() -> AsyncIterator { - AsyncIterator(stream: stream, allocator: allocator) + AsyncIterator(stream: stream, allocator: allocator, chunkSize: chunkSize) } struct AsyncIterator: AsyncIteratorProtocol { private let stream: Smithy.Stream private let allocator: ByteBufferAllocator + private let chunkSize: Int private var isFinished = false - init(stream: Smithy.Stream, allocator: ByteBufferAllocator) { + init(stream: Smithy.Stream, allocator: ByteBufferAllocator, chunkSize: Int) { self.stream = stream self.allocator = allocator + self.chunkSize = chunkSize } mutating func next() async throws -> ByteBuffer? { guard !isFinished else { return nil } do { - // Read a chunk from the stream (using default chunk size) - let data = try await stream.readAsync(upToCount: CHUNK_SIZE_BYTES) + // Read a chunk from the stream (using configurable chunk size) + let data = try await stream.readAsync(upToCount: chunkSize) if let data = data, !data.isEmpty { var buffer = allocator.buffer(capacity: data.count) diff --git a/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift new file mode 100644 index 000000000..9bbce204d --- /dev/null +++ b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift @@ -0,0 +1,142 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import Foundation +import NIO +import SmithyTestUtil +import XCTest +import class SmithyStreams.BufferedStream +import enum Smithy.LogAgentLevel +import protocol Smithy.LogAgent +import enum Smithy.ByteStream +@testable import ClientRuntime + +class NIOHTTPClientStreamBridgeTests: XCTestCase { + + func test_convertResponseBody_streamsAllDataCorrectly() async throws { + + // The maximum size of input streaming data in the tests + let maxDataSize = 65_536 // 64 kb + + // Create & fill a buffer with random bytes, for use in later test setup + // Random buffer is reused because creating random data is slow + // We are responsible for deallocating it + let randomBuffer = UnsafeMutablePointer.allocate(capacity: maxDataSize) + defer { randomBuffer.deallocate() } + + for i in 0.. Data { + switch byteStream { + case .stream(let stream): + return try await stream.readToEndAsync() ?? Data() + case .data(let data): + return data ?? Data() + case .noStream: + return Data() + } + } +} + +private extension Data { + init(buffer: ByteBuffer) { + if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { + self.init(bytes) + } else { + self.init() + } + } +} From d41064ea143dc4df1271cee9dfdbe03182ee492d Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Wed, 24 Sep 2025 16:40:26 -0400 Subject: [PATCH 7/9] re-use allocator in tests and generate random data --- .../NIO/NIOHTTPClientStreamBridgeTests.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift index 9bbce204d..9f64d9e45 100644 --- a/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift +++ b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift @@ -17,6 +17,7 @@ import enum Smithy.ByteStream @testable import ClientRuntime class NIOHTTPClientStreamBridgeTests: XCTestCase { + let allocator = ByteBufferAllocator() func test_convertResponseBody_streamsAllDataCorrectly() async throws { @@ -44,7 +45,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase { let originalData = Data(bytes: randomBuffer, count: dataSize) // Create a mock AsyncHTTPClient response with the test data - let allocator = ByteBufferAllocator() var buffer = allocator.buffer(capacity: originalData.count) buffer.writeBytes(originalData) @@ -64,7 +64,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase { } func test_convertRequestBody_withNoStream() async throws { - let allocator = ByteBufferAllocator() let byteStream = ByteStream.noStream let result = try await NIOHTTPClientStreamBridge.convertRequestBody( @@ -82,7 +81,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase { } func test_convertRequestBody_withData() async throws { - let allocator = ByteBufferAllocator() let testData = "Hello, World!".data(using: .utf8)! let byteStream = ByteStream.data(testData) @@ -100,8 +98,16 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase { } func test_convertRequestBody_withStream() async throws { - let allocator = ByteBufferAllocator() - let testData = Data(repeating: 42, count: 1000) + // Create random test data + let dataSize = 1000 + let randomBuffer = UnsafeMutablePointer.allocate(capacity: dataSize) + defer { randomBuffer.deallocate() } + + for i in 0.. Date: Thu, 25 Sep 2025 10:45:40 -0400 Subject: [PATCH 8/9] add initial stream bridge (#964) --- .../Http/NIO/NIOHTTPClientError.swift | 12 ++ .../Http/NIO/NIOHTTPClientStreamBridge.swift | 146 +++++++++++++++++ .../NIO/NIOHTTPClientStreamBridgeTests.swift | 148 ++++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift create mode 100644 Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift new file mode 100644 index 000000000..118600cc4 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientError.swift @@ -0,0 +1,12 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +/// Errors that are particular to the NIO-based Smithy HTTP client. +public enum NIOHTTPClientError: Error { + + case streamingError(Error) +} diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift new file mode 100644 index 000000000..d58ef0d55 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientStreamBridge.swift @@ -0,0 +1,146 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import Foundation +import NIO +import Smithy +import SmithyStreams + +/// Handles streaming between Smithy streams and AsyncHTTPClient +final class NIOHTTPClientStreamBridge { + + /// Convert Smithy ByteStream to AsyncHTTPClient request body + static func convertRequestBody( + from body: ByteStream, + allocator: ByteBufferAllocator, + chunkSize: Int = CHUNK_SIZE_BYTES + ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { + switch body { + case .noStream: + // No body to send + return .bytes(allocator.buffer(capacity: 0)) + + case .data(let data): + // Convert Data to ByteBuffer + if let data = data { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return .bytes(buffer) + } else { + return .bytes(allocator.buffer(capacity: 0)) + } + + case .stream(let stream): + // Handle streaming request body + return try await convertStreamToRequestBody(stream: stream, allocator: allocator, chunkSize: chunkSize) + } + } + + /// Convert AsyncHTTPClient response body to Smithy ByteStream + static func convertResponseBody( + from response: AsyncHTTPClient.HTTPClientResponse + ) async -> ByteStream { + let bufferedStream = BufferedStream() + + do { + var iterator = response.body.makeAsyncIterator() + while let buffer = try await iterator.next() { + // Convert ByteBuffer to Data and write to buffered stream + if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { + let data = Data(bytes) + try bufferedStream.write(contentsOf: data) + } + } + bufferedStream.close() + } catch { + bufferedStream.closeWithError(error) + } + + return .stream(bufferedStream) + } + + /// Convert a Smithy Stream to AsyncHTTPClient request body + private static func convertStreamToRequestBody( + stream: Smithy.Stream, + allocator: ByteBufferAllocator, + chunkSize: Int = CHUNK_SIZE_BYTES + ) async throws -> AsyncHTTPClient.HTTPClientRequest.Body { + if let streamLength = stream.length { + let asyncSequence = StreamToAsyncSequence(stream: stream, allocator: allocator, chunkSize: chunkSize) + return .stream(asyncSequence, length: .known(Int64(streamLength))) + } else { + do { + let data = try await stream.readToEndAsync() + if let data = data { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return .bytes(buffer) + } else { + return .bytes(allocator.buffer(capacity: 0)) + } + } catch { + throw NIOHTTPClientError.streamingError(error) + } + } + } +} + +/// AsyncSequence adapter that converts a Smithy Stream to ByteBuffer sequence for AsyncHTTPClient +internal struct StreamToAsyncSequence: AsyncSequence, Sendable { + typealias Element = ByteBuffer + + private let stream: Smithy.Stream + private let allocator: ByteBufferAllocator + private let chunkSize: Int + + init(stream: Smithy.Stream, allocator: ByteBufferAllocator, chunkSize: Int = CHUNK_SIZE_BYTES) { + self.stream = stream + self.allocator = allocator + self.chunkSize = chunkSize + } + + func makeAsyncIterator() -> AsyncIterator { + AsyncIterator(stream: stream, allocator: allocator, chunkSize: chunkSize) + } + + struct AsyncIterator: AsyncIteratorProtocol { + private let stream: Smithy.Stream + private let allocator: ByteBufferAllocator + private let chunkSize: Int + private var isFinished = false + + init(stream: Smithy.Stream, allocator: ByteBufferAllocator, chunkSize: Int) { + self.stream = stream + self.allocator = allocator + self.chunkSize = chunkSize + } + + mutating func next() async throws -> ByteBuffer? { + guard !isFinished else { return nil } + + do { + // Read a chunk from the stream (using configurable chunk size) + let data = try await stream.readAsync(upToCount: chunkSize) + + if let data = data, !data.isEmpty { + var buffer = allocator.buffer(capacity: data.count) + buffer.writeBytes(data) + return buffer + } else { + isFinished = true + stream.close() + return nil + } + } catch { + isFinished = true + stream.close() + throw NIOHTTPClientError.streamingError(error) + } + } + } +} diff --git a/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift new file mode 100644 index 000000000..9f64d9e45 --- /dev/null +++ b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift @@ -0,0 +1,148 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AsyncHTTPClient +import Foundation +import NIO +import SmithyTestUtil +import XCTest +import class SmithyStreams.BufferedStream +import enum Smithy.LogAgentLevel +import protocol Smithy.LogAgent +import enum Smithy.ByteStream +@testable import ClientRuntime + +class NIOHTTPClientStreamBridgeTests: XCTestCase { + let allocator = ByteBufferAllocator() + + func test_convertResponseBody_streamsAllDataCorrectly() async throws { + + // The maximum size of input streaming data in the tests + let maxDataSize = 65_536 // 64 kb + + // Create & fill a buffer with random bytes, for use in later test setup + // Random buffer is reused because creating random data is slow + // We are responsible for deallocating it + let randomBuffer = UnsafeMutablePointer.allocate(capacity: maxDataSize) + defer { randomBuffer.deallocate() } + + for i in 0...allocate(capacity: dataSize) + defer { randomBuffer.deallocate() } + + for i in 0.. Data { + switch byteStream { + case .stream(let stream): + return try await stream.readToEndAsync() ?? Data() + case .data(let data): + return data ?? Data() + case .noStream: + return Data() + } + } +} + +private extension Data { + init(buffer: ByteBuffer) { + if let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) { + self.init(bytes) + } else { + self.init() + } + } +} From 3fc7ced83d2a98cebe88c01be28b012506f2dbb0 Mon Sep 17 00:00:00 2001 From: David Yaffe Date: Thu, 25 Sep 2025 10:57:12 -0400 Subject: [PATCH 9/9] Add support for TLS configuration --- .../Networking/Http/NIO/NIOHTTPClient.swift | 20 ++++- .../Http/NIO/NIOHTTPClientTLSOptions.swift | 52 ++++++++++++ .../NIO/NIOHTTPClientTLSResolverUtils.swift | 85 +++++++++++++++++++ .../NIO/NIOHTTPClientTLSOptionsTests.swift | 65 ++++++++++++++ 4 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSOptions.swift create mode 100644 Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSResolverUtils.swift create mode 100644 Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientTLSOptionsTests.swift diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift index 17490b294..7b7f3f616 100644 --- a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClient.swift @@ -6,15 +6,20 @@ // import AsyncHTTPClient +import Foundation import NIOCore import NIOPosix +import NIOSSL import SmithyHTTPAPI +import NIOHTTP1 /// AsyncHTTPClient-based HTTP client implementation that conforms to SmithyHTTPAPI.HTTPClient /// This implementation is thread-safe and supports concurrent request execution. public final class NIOHTTPClient: SmithyHTTPAPI.HTTPClient { private let client: AsyncHTTPClient.HTTPClient private let config: HttpClientConfiguration + private let tlsConfiguration: NIOHTTPClientTLSOptions? + private let allocator: ByteBufferAllocator /// Creates a new `NIOHTTPClient`. /// @@ -25,13 +30,22 @@ public final class NIOHTTPClient: SmithyHTTPAPI.HTTPClient { httpClientConfiguration: HttpClientConfiguration ) throws { self.config = httpClientConfiguration - self.client = AsyncHTTPClient.HTTPClient( - configuration: .init() // TODO - ) + self.tlsConfiguration = httpClientConfiguration.tlsConfiguration as? NIOHTTPClientTLSOptions + self.allocator = ByteBufferAllocator() + + var clientConfig = AsyncHTTPClient.HTTPClient.Configuration() + + // Configure TLS if options are provided + if let tlsOptions = tlsConfiguration { + clientConfig.tlsConfiguration = try tlsOptions.makeNIOSSLConfiguration() + } + + self.client = AsyncHTTPClient.HTTPClient(configuration: clientConfig) } public func send(request: SmithyHTTPAPI.HTTPRequest) async throws -> SmithyHTTPAPI.HTTPResponse { // TODO return HTTPResponse() } + } diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSOptions.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSOptions.swift new file mode 100644 index 000000000..c4b40fe54 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSOptions.swift @@ -0,0 +1,52 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation +import NIOSSL + +public struct NIOHTTPClientTLSOptions: TLSConfiguration, Sendable { + + /// Optional path to a PEM certificate + public var certificate: String? + + /// Optional path to certificate directory + public var certificateDir: String? + + /// Optional path to a PEM format private key + public var privateKey: String? + + /// Optional path to PKCS #12 certificate, in PEM format + public var pkcs12Path: String? + + /// Optional PKCS#12 password + public var pkcs12Password: String? + + /// Information is provided to use custom trust store + public var useSelfSignedCertificate: Bool { + return certificate != nil || certificateDir != nil + } + + /// Information is provided to use custom key store + public var useProvidedKeystore: Bool { + return (pkcs12Path != nil && pkcs12Password != nil) || + (certificate != nil && privateKey != nil) + } + + public init( + certificate: String? = nil, + certificateDir: String? = nil, + privateKey: String? = nil, + pkcs12Path: String? = nil, + pkcs12Password: String? = nil + ) { + self.certificate = certificate + self.certificateDir = certificateDir + self.privateKey = privateKey + self.pkcs12Path = pkcs12Path + self.pkcs12Password = pkcs12Password + } +} diff --git a/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSResolverUtils.swift b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSResolverUtils.swift new file mode 100644 index 000000000..12d57b724 --- /dev/null +++ b/Sources/ClientRuntime/Networking/Http/NIO/NIOHTTPClientTLSResolverUtils.swift @@ -0,0 +1,85 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation +import NIOSSL + +extension NIOHTTPClientTLSOptions { + + func makeNIOSSLConfiguration() throws -> NIOSSL.TLSConfiguration { + var tlsConfig = NIOSSL.TLSConfiguration.makeClientConfiguration() + + if useSelfSignedCertificate { + if let certificateDir = certificateDir, let certificate = certificate { + let certificatePath = "\(certificateDir)/\(certificate)" + let certificates = try NIOHTTPClientTLSOptions.loadCertificates(from: certificatePath) + tlsConfig.trustRoots = .certificates(certificates) + } else if let certificate = certificate { + let certificates = try NIOHTTPClientTLSOptions.loadCertificates(from: certificate) + tlsConfig.trustRoots = .certificates(certificates) + } + } + + if useProvidedKeystore { + if let pkcs12Path = pkcs12Path, let pkcs12Password = pkcs12Password { + let bundle = try NIOHTTPClientTLSOptions.loadPKCS12Bundle(from: pkcs12Path, password: pkcs12Password) + tlsConfig.certificateChain = bundle.certificateChain.map { .certificate($0) } + tlsConfig.privateKey = .privateKey(bundle.privateKey) + } else if let certificate = certificate, let privateKey = privateKey { + let cert = try NIOHTTPClientTLSOptions.loadCertificate(from: certificate) + let key = try NIOHTTPClientTLSOptions.loadPrivateKey(from: privateKey) + tlsConfig.certificateChain = [.certificate(cert)] + tlsConfig.privateKey = .privateKey(key) + } + } + + return tlsConfig + } +} + +extension NIOHTTPClientTLSOptions { + + static func loadCertificates(from filePath: String) throws -> [NIOSSLCertificate] { + let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath)) + return try NIOSSLCertificate.fromPEMBytes(Array(fileData)) + } + + static func loadCertificate(from filePath: String) throws -> NIOSSLCertificate { + let certificates = try loadCertificates(from: filePath) + guard let certificate = certificates.first else { + throw NIOHTTPClientTLSError.noCertificateFound(filePath) + } + return certificate + } + + static func loadPrivateKey(from filePath: String) throws -> NIOSSLPrivateKey { + let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath)) + return try NIOSSLPrivateKey(bytes: Array(fileData), format: .pem) + } + + static func loadPKCS12Bundle(from filePath: String, password: String) throws -> NIOSSLPKCS12Bundle { + do { + return try NIOSSLPKCS12Bundle(file: filePath, passphrase: password.utf8) + } catch { + throw NIOHTTPClientTLSError.invalidPKCS12(filePath, underlying: error) + } + } +} + +public enum NIOHTTPClientTLSError: Error, LocalizedError { + case noCertificateFound(String) + case invalidPKCS12(String, underlying: Error) + + public var errorDescription: String? { + switch self { + case .noCertificateFound(let path): + return "No certificate found at path: \(path)" + case .invalidPKCS12(let path, let underlying): + return "Failed to load PKCS#12 file at path: \(path). Error: \(underlying.localizedDescription)" + } + } +} diff --git a/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientTLSOptionsTests.swift b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientTLSOptionsTests.swift new file mode 100644 index 000000000..e26d8db1f --- /dev/null +++ b/Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientTLSOptionsTests.swift @@ -0,0 +1,65 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation +import XCTest +@testable import ClientRuntime + +class NIOHTTPClientTLSOptionsTests: XCTestCase { + + func test_init_withDefaults() { + let tlsOptions = NIOHTTPClientTLSOptions() + + XCTAssertNil(tlsOptions.certificate) + XCTAssertNil(tlsOptions.certificateDir) + XCTAssertNil(tlsOptions.privateKey) + XCTAssertNil(tlsOptions.pkcs12Path) + XCTAssertNil(tlsOptions.pkcs12Password) + XCTAssertFalse(tlsOptions.useSelfSignedCertificate) + XCTAssertFalse(tlsOptions.useProvidedKeystore) + } + + func test_init_withCertificate() { + let tlsOptions = NIOHTTPClientTLSOptions(certificate: "/path/to/cert.pem") + + XCTAssertEqual(tlsOptions.certificate, "/path/to/cert.pem") + XCTAssertTrue(tlsOptions.useSelfSignedCertificate) + XCTAssertFalse(tlsOptions.useProvidedKeystore) + } + + func test_init_withCertificateDir() { + let tlsOptions = NIOHTTPClientTLSOptions(certificateDir: "/path/to/certs/") + + XCTAssertEqual(tlsOptions.certificateDir, "/path/to/certs/") + XCTAssertTrue(tlsOptions.useSelfSignedCertificate) + XCTAssertFalse(tlsOptions.useProvidedKeystore) + } + + func test_init_withPKCS12() { + let tlsOptions = NIOHTTPClientTLSOptions( + pkcs12Path: "/path/to/cert.p12", + pkcs12Password: "password" + ) + + XCTAssertEqual(tlsOptions.pkcs12Path, "/path/to/cert.p12") + XCTAssertEqual(tlsOptions.pkcs12Password, "password") + XCTAssertFalse(tlsOptions.useSelfSignedCertificate) + XCTAssertTrue(tlsOptions.useProvidedKeystore) + } + + func test_init_withCertificateAndPrivateKey() { + let tlsOptions = NIOHTTPClientTLSOptions( + certificate: "/path/to/cert.pem", + privateKey: "/path/to/key.pem" + ) + + XCTAssertEqual(tlsOptions.certificate, "/path/to/cert.pem") + XCTAssertEqual(tlsOptions.privateKey, "/path/to/key.pem") + XCTAssertTrue(tlsOptions.useSelfSignedCertificate) + XCTAssertTrue(tlsOptions.useProvidedKeystore) + } +}