Skip to content

Commit 8d43633

Browse files
committed
Use NIOTSConnectionBootstrap(validatingGroup:)
1 parent 639d0d5 commit 8d43633

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ let package = Package(
2121
.library(name: "AsyncHTTPClient", targets: ["AsyncHTTPClient"]),
2222
],
2323
dependencies: [
24-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.13.1"),
24+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.16.0"),
2525
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.7.0"),
2626
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.3.0"),
27-
.package(url: "https://github.com/adam-fowler/swift-nio-transport-services.git", .branch("public-niots-eventloop")),
27+
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.5.1"),
2828
],
2929
targets: [
3030
.target(

Sources/AsyncHTTPClient/Utils.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public final class HTTPClientCopyingDelegate: HTTPClientResponseDelegate {
5353
}
5454

5555
extension ClientBootstrap {
56-
fileprivate static func makeBootstrap(
57-
on eventLoop: EventLoop,
56+
fileprivate func makeClientTCPBootstrap(
5857
host: String,
5958
requiresTLS: Bool,
6059
configuration: HTTPClient.Configuration
@@ -63,7 +62,7 @@ extension ClientBootstrap {
6362
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
6463
let hostname = (!requiresTLS || host.isIPAddress) ? nil : host
6564
let tlsProvider = try NIOSSLClientTLSProvider<ClientBootstrap>(context: sslContext, serverHostname: hostname)
66-
return NIOClientTCPBootstrap(ClientBootstrap(group: eventLoop), tls: tlsProvider)
65+
return NIOClientTCPBootstrap(self, tls: tlsProvider)
6766
}
6867
}
6968

@@ -79,10 +78,11 @@ extension NIOClientTCPBootstrap {
7978
let bootstrap: NIOClientTCPBootstrap
8079
#if canImport(Network)
8180
// if eventLoop is compatible with NIOTransportServices create a NIOTSConnectionBootstrap
82-
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *), eventLoop is NIOTSEventLoop {
83-
let tsBootstrap = NIOTSConnectionBootstrap(group: eventLoop).channelOption(NIOTSChannelOptions.waitForActivity, value: false)
84-
let tlsConfiguration = configuration.tlsConfiguration ?? TLSConfiguration.forClient()
81+
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *),
82+
var tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) {
8583

84+
tsBootstrap = tsBootstrap.channelOption(NIOTSChannelOptions.waitForActivity, value: false)
85+
let tlsConfiguration = configuration.tlsConfiguration ?? TLSConfiguration.forClient()
8686
// if we have a proxy and require TLS then use NIOSSL tls support
8787
if configuration.proxy != nil, requiresTLS {
8888
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
@@ -94,13 +94,19 @@ extension NIOClientTCPBootstrap {
9494
let tlsProvider = NIOTSClientTLSProvider(tlsOptions: parameters)
9595
bootstrap = NIOClientTCPBootstrap(tsBootstrap, tls: tlsProvider)
9696
}
97+
} else if let clientBootstrap = ClientBootstrap(validatingGroup: eventLoop) {
98+
bootstrap = try clientBootstrap.makeClientTCPBootstrap(host: host, requiresTLS: requiresTLS, configuration: configuration)
9799
} else {
98-
bootstrap = try ClientBootstrap.makeBootstrap(on: eventLoop, host: host, requiresTLS: requiresTLS, configuration: configuration)
100+
preconditionFailure("Cannot create bootstrap for the supplied EventLoop")
99101
}
100102
#else
101-
bootstrap = try ClientBootstrap.makeBootstrap(on: eventLoop, host: host, requiresTLS: requiresTLS, configuration: configuration)
103+
if let clientBootstrap = ClientBootstrap(validatingGroup: eventLoop) {
104+
bootstrap = try clientBootstrap.makeClientTCPBootstrap(host: host, requiresTLS: requiresTLS, configuration: configuration)
105+
} else {
106+
preconditionFailure("Cannot create bootstrap for the supplied EventLoop")
107+
}
102108
#endif
103-
109+
104110
// don't enable TLS if we have a proxy, this will be enabled later on
105111
if requiresTLS, configuration.proxy == nil {
106112
return bootstrap.enableTLS()

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,13 +1679,15 @@ class HTTPClientTests: XCTestCase {
16791679
}
16801680

16811681
XCTAssertThrowsError(try httpClient.get(url: "http://localhost:\(port)").wait()) { error in
1682+
#if canImport(Network)
16821683
if isTestingNIOTS() {
1683-
guard let ioError = error as? IOError, ioError.errnoCode == ECONNREFUSED else {
1684+
guard let ioError = error as? NWPOSIXError, ioError.errorCode == .ECONNREFUSED else {
16841685
XCTFail("Unexpected error: \(error)")
16851686
return
16861687
}
16871688
return
16881689
}
1690+
#endif
16891691
guard error is NIOConnectionError else {
16901692
XCTFail("Unexpected error: \(error)")
16911693
return

0 commit comments

Comments
 (0)