Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ let package = Package(

.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
],
swiftSettings: extraSettings
),
Expand Down
21 changes: 19 additions & 2 deletions Sources/AHCHTTPClient/AHC+HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import Foundation
import HTTPTypes
import NIOCore
import NIOHTTP1
import NIOSSL
import Synchronization

@available(anyAppleOS 26.0, *)
extension AsyncHTTPClient.HTTPClient: HTTPAPIs.HTTPClient {
public typealias RequestWriter = RequestBodyWriter
public typealias ResponseConcludingReader = ResponseReader

public struct RequestOptions: HTTPClientCapability.RequestOptions {

public struct RequestOptions: HTTPClientCapability.DeclarativeTLS {
public var serverTrustPolicy: TrustEvaluationPolicy = .default
}

public struct RequestBodyWriter: AsyncWriter, ~Copyable {
Expand Down Expand Up @@ -163,6 +164,7 @@ extension AsyncHTTPClient.HTTPClient: HTTPAPIs.HTTPClient {
let sequence = request.headerFields.lazy.map({ ($0.name.rawName, $0.value) })
ahcRequest.headers.add(contentsOf: sequence)
}
ahcRequest.tlsConfiguration = Self.tlsConfiguration(for: options.serverTrustPolicy)

if let body, body.knownLength != 0 {
let (asyncStream, startUploadContinuation) = AsyncStream.makeStream(of: HTTPClientRequest.Body.RequestWriter.self)
Expand Down Expand Up @@ -217,4 +219,19 @@ extension AsyncHTTPClient.HTTPClient: HTTPAPIs.HTTPClient {

return try result!.get()
}

private static func tlsConfiguration(for policy: TrustEvaluationPolicy) -> TLSConfiguration? {
switch policy {
case .default:
return nil
case .allowNameMismatch:
var config = TLSConfiguration.makeClientConfiguration()
config.certificateVerification = .noHostnameVerification
return config
case .allowAny:
var config = TLSConfiguration.makeClientConfiguration()
config.certificateVerification = .none
return config
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//
//===----------------------------------------------------------------------===//

public import NetworkTypes

@available(anyAppleOS 26.0, *)
extension HTTPClientCapability {
/// A protocol for HTTP request options that support TLS policies.
Expand Down
1 change: 1 addition & 0 deletions Sources/HTTPAPIs/HTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@_exported public import AsyncStreaming
@_exported public import ContainersPreview
@_exported public import HTTPTypes
@_exported public import NetworkTypes

/// The namespace for HTTP.
public enum HTTP {}
6 changes: 3 additions & 3 deletions Sources/HTTPClient/DefaultHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public final class DefaultHTTPClient: HTTPAPIs.HTTPClient {
options: HTTPRequestOptions,
responseHandler: (HTTPResponse, consuming ResponseConcludingReader) async throws -> Return
) async throws -> Return {
// TODO: translate request options
let options = self.client.defaultRequestOptions
var actualOptions = self.client.defaultRequestOptions
actualOptions.serverTrustPolicy = options.serverTrustPolicy
let body = body.map {
HTTPClientRequestBody<ActualHTTPClient.RequestWriter>(other: $0) { RequestWriter(actual: $0) }
}
return try await self.client.perform(request: request, body: body, options: options) { response, body in
return try await self.client.perform(request: request, body: body, options: actualOptions) { response, body in
try await responseHandler(response, ResponseConcludingReader(actual: body))
}
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/HTTPClient/HTTPRequestOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/// The options for the default HTTP client implementation.
@available(anyAppleOS 26.0, *)
public struct HTTPRequestOptions: HTTPClientCapability.RequestOptions {
public struct HTTPRequestOptions: HTTPClientCapability.DeclarativeTLS {
public var serverTrustPolicy: TrustEvaluationPolicy = .default

public init() {}
}
Loading