Skip to content

Commit 4b108a5

Browse files
committed
Moved makeHTTPClientBootstrapBase back to Utils.swift
It is now a member of NIOClientTCPBootstrap
1 parent 6566f64 commit 4b108a5

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

Sources/AsyncHTTPClient/ConnectionPool.swift

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -370,54 +370,12 @@ final class ConnectionPool {
370370
}
371371
}
372372

373-
private func makeNonTSBootstrap(on eventLoop: EventLoop) throws -> NIOClientTCPBootstrap {
374-
let tlsConfiguration = configuration.tlsConfiguration ?? TLSConfiguration.forClient()
375-
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
376-
let tlsProvider = try NIOSSLClientTLSProvider<ClientBootstrap>(context: sslContext, serverHostname: (key.scheme == .unix || key.host.isIPAddress) ? nil : key.host)
377-
return NIOClientTCPBootstrap(ClientBootstrap(group: eventLoop), tls: tlsProvider)
378-
}
379-
380-
/// create a TCP Bootstrap based off what type of `EventLoop` has been passed to the function.
381-
private func makeBootstrap(on eventLoop: EventLoop) throws -> NIOClientTCPBootstrap {
382-
let bootstrap: NIOClientTCPBootstrap
383-
#if canImport(Network)
384-
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *), eventLoop is NIOTSEventLoop {
385-
let tlsProvider = NIOTSClientTLSProvider(tlsOptions: .init())
386-
bootstrap = NIOClientTCPBootstrap(NIOTSConnectionBootstrap(group: eventLoop), tls: tlsProvider)
387-
} else {
388-
bootstrap = try makeNonTSBootstrap(on: eventLoop)
389-
}
390-
#else
391-
bootstrap = try makeNonTSBootstrap(on: eventLoop)
392-
#endif
393-
394-
if key.scheme == .https {
395-
return bootstrap.enableTLS()
396-
}
397-
return bootstrap
398-
}
399-
400-
private func makeHTTPClientBootstrapBase(on eventLoop: EventLoop) throws -> NIOClientTCPBootstrap {
401-
return try makeBootstrap(on: eventLoop)
402-
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
403-
.channelInitializer { channel in
404-
let channelAddedFuture: EventLoopFuture<Void>
405-
switch self.configuration.proxy {
406-
case .none:
407-
channelAddedFuture = eventLoop.makeSucceededFuture(())
408-
case .some:
409-
channelAddedFuture = channel.pipeline.addProxyHandler(host: self.key.host, port: self.key.port, authorization: self.configuration.proxy?.authorization)
410-
}
411-
return channelAddedFuture
412-
}
413-
}
414-
415373
private func makeConnection(on eventLoop: EventLoop) -> EventLoopFuture<Connection> {
416374
self.activityPrecondition(expected: [.opened])
417375
let address = HTTPClient.resolveAddress(host: self.key.host, port: self.key.port, proxy: self.configuration.proxy)
418376
let bootstrap : NIOClientTCPBootstrap
419377
do {
420-
bootstrap = try makeHTTPClientBootstrapBase(on: eventLoop)
378+
bootstrap = try NIOClientTCPBootstrap.makeHTTPClientBootstrapBase(on: eventLoop, host: key.host, port: key.port, requiresTLS: self.key.scheme == .https, configuration: self.configuration)
421379
} catch {
422380
return eventLoop.makeFailedFuture(error)
423381
}

Sources/AsyncHTTPClient/Utils.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import NIO
1616
import NIOHTTP1
1717
import NIOHTTPCompression
18+
import NIOSSL
19+
import NIOTransportServices
1820

1921
internal extension String {
2022
var isIPAddress: Bool {
@@ -46,6 +48,52 @@ public final class HTTPClientCopyingDelegate: HTTPClientResponseDelegate {
4648
}
4749
}
4850

51+
extension ClientBootstrap {
52+
fileprivate static func makeBootstrap(on eventLoop: EventLoop, host: String, requiresTLS: Bool, configuration: HTTPClient.Configuration) throws -> NIOClientTCPBootstrap {
53+
let tlsConfiguration = configuration.tlsConfiguration ?? TLSConfiguration.forClient()
54+
let sslContext = try NIOSSLContext(configuration: tlsConfiguration)
55+
let tlsProvider = try NIOSSLClientTLSProvider<ClientBootstrap>(context: sslContext, serverHostname: (!requiresTLS || host.isIPAddress) ? nil : host)
56+
return NIOClientTCPBootstrap(ClientBootstrap(group: eventLoop), tls: tlsProvider)
57+
}
58+
}
59+
60+
extension NIOClientTCPBootstrap {
61+
/// create a TCP Bootstrap based off what type of `EventLoop` has been passed to the function.
62+
fileprivate static func makeBootstrap(on eventLoop: EventLoop, host: String, requiresTLS: Bool, configuration: HTTPClient.Configuration) throws -> NIOClientTCPBootstrap {
63+
let bootstrap: NIOClientTCPBootstrap
64+
#if canImport(Network)
65+
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *), eventLoop is NIOTSEventLoop {
66+
let tlsProvider = NIOTSClientTLSProvider(tlsOptions: .init())
67+
bootstrap = NIOClientTCPBootstrap(NIOTSConnectionBootstrap(group: eventLoop), tls: tlsProvider)
68+
} else {
69+
bootstrap = try ClientBootstrap.makeBootstrap(on: eventLoop, host: host, requiresTLS: requiresTLS, configuration: configuration)
70+
}
71+
#else
72+
bootstrap = try ClientBootstrap.makeBootstrap(on: eventLoop, host: host, requiresTLS: requiresTLS, configuration: configuration)
73+
#endif
74+
75+
if requiresTLS {
76+
return bootstrap.enableTLS()
77+
}
78+
return bootstrap
79+
}
80+
81+
static func makeHTTPClientBootstrapBase(on eventLoop: EventLoop, host: String, port: Int, requiresTLS: Bool, configuration: HTTPClient.Configuration) throws -> NIOClientTCPBootstrap {
82+
return try makeBootstrap(on: eventLoop, host: host, requiresTLS: requiresTLS, configuration: configuration)
83+
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
84+
.channelInitializer { channel in
85+
let channelAddedFuture: EventLoopFuture<Void>
86+
switch configuration.proxy {
87+
case .none:
88+
channelAddedFuture = eventLoop.makeSucceededFuture(())
89+
case .some:
90+
channelAddedFuture = channel.pipeline.addProxyHandler(host: host, port: port, authorization: configuration.proxy?.authorization)
91+
}
92+
return channelAddedFuture
93+
}
94+
}
95+
}
96+
4997
extension CircularBuffer {
5098
@discardableResult
5199
mutating func swapWithFirstAndRemove(at index: Index) -> Element? {

0 commit comments

Comments
 (0)