Skip to content

Commit d27bda2

Browse files
Refactored and consolidated existing socket path handling to prepare for the new schemes
Motivation: Before I start adding support for new URL schemes, I consolidated the existing use of unix sockets and https TLS handling. Modifications: Consolidate socket path and uri handling to one place, added an associated type to the unixSocket case, and added a computed requiresTLS property to the Key Scheme. Result: Much cleaner code that is now ready for some new schemes!
1 parent c4e9fdf commit d27bda2

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool.swift

+13-4
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,32 @@ final class ConnectionPool {
113113
self.scheme = .https
114114
case "unix":
115115
self.scheme = .unix
116-
self.unixPath = request.url.baseURL?.path ?? request.url.path
117116
default:
118117
fatalError("HTTPClient.Request scheme should already be a valid one")
119118
}
120119
self.port = request.port
121120
self.host = request.host
121+
self.unixPath = request.socketPath
122122
}
123123

124124
var scheme: Scheme
125125
var host: String
126126
var port: Int
127-
var unixPath: String = ""
127+
var unixPath: String
128128

129129
enum Scheme: Hashable {
130130
case http
131131
case https
132132
case unix
133+
134+
var requiresTLS: Bool {
135+
switch self {
136+
case .https:
137+
return true
138+
default:
139+
return false
140+
}
141+
}
133142
}
134143
}
135144
}
@@ -433,7 +442,7 @@ class HTTP1ConnectionProvider {
433442

434443
private func makeChannel(preference: HTTPClient.EventLoopPreference) -> EventLoopFuture<Channel> {
435444
let eventLoop = preference.bestEventLoop ?? self.eventLoop
436-
let requiresTLS = self.key.scheme == .https
445+
let requiresTLS = self.key.scheme.requiresTLS
437446
let bootstrap: NIOClientTCPBootstrap
438447
do {
439448
bootstrap = try NIOClientTCPBootstrap.makeHTTPClientBootstrapBase(on: eventLoop, host: self.key.host, port: self.key.port, requiresTLS: requiresTLS, configuration: self.configuration)
@@ -451,7 +460,7 @@ class HTTP1ConnectionProvider {
451460
}
452461

453462
return channel.flatMap { channel in
454-
let requiresSSLHandler = self.configuration.proxy != nil && self.key.scheme == .https
463+
let requiresSSLHandler = self.configuration.proxy != nil && self.key.scheme.requiresTLS
455464
let handshakePromise = channel.eventLoop.makePromise(of: Void.self)
456465

457466
channel.pipeline.addSSLHandlerIfNeeded(for: self.key, tlsConfiguration: self.configuration.tlsConfiguration, addSSLClient: requiresSSLHandler, handshakePromise: handshakePromise)

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ extension ChannelPipeline {
654654
}
655655

656656
func addSSLHandlerIfNeeded(for key: ConnectionPool.Key, tlsConfiguration: TLSConfiguration?, addSSLClient: Bool, handshakePromise: EventLoopPromise<Void>) {
657-
guard key.scheme == .https else {
657+
guard key.scheme.requiresTLS else {
658658
handshakePromise.succeed(())
659659
return
660660
}

Diff for: Sources/AsyncHTTPClient/HTTPHandler.swift

+34-15
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,23 @@ extension HTTPClient {
9999
public struct Request {
100100
/// Represent kind of Request
101101
enum Kind {
102+
enum UnixScheme {
103+
case baseURL
104+
}
105+
102106
/// Remote host request.
103107
case host
104108
/// UNIX Domain Socket HTTP request.
105-
case unixSocket
109+
case unixSocket(_ scheme: UnixScheme)
106110

107111
private static var hostSchemes = ["http", "https"]
108112
private static var unixSchemes = ["unix"]
109113

110114
init(forScheme scheme: String) throws {
111-
if Kind.host.supports(scheme: scheme) {
112-
self = .host
113-
} else if Kind.unixSocket.supports(scheme: scheme) {
114-
self = .unixSocket
115-
} else {
115+
switch scheme {
116+
case "http", "https": self = .host
117+
case "unix": self = .unixSocket(.baseURL)
118+
default:
116119
throw HTTPClientError.unsupportedScheme(scheme)
117120
}
118121
}
@@ -129,6 +132,24 @@ extension HTTPClient {
129132
}
130133
}
131134

135+
func socketPathFromURL(_ url: URL) throws -> String {
136+
switch self {
137+
case .unixSocket(.baseURL):
138+
return url.baseURL?.path ?? url.path
139+
case .host:
140+
return ""
141+
}
142+
}
143+
144+
func uriFromURL(_ url: URL) -> String {
145+
switch self {
146+
case .host:
147+
return url.uri
148+
case .unixSocket(.baseURL):
149+
return url.baseURL != nil ? url.uri : "/"
150+
}
151+
}
152+
132153
func supports(scheme: String) -> Bool {
133154
switch self {
134155
case .host:
@@ -147,6 +168,10 @@ extension HTTPClient {
147168
public let scheme: String
148169
/// Remote host, resolved from `URL`.
149170
public let host: String
171+
/// Socket path, resolved from `URL`.
172+
let socketPath: String
173+
/// URI composed of the path and query, resolved from `URL`.
174+
let uri: String
150175
/// Request custom HTTP Headers, defaults to no headers.
151176
public var headers: HTTPHeaders
152177
/// Request body, defaults to no body.
@@ -199,6 +224,8 @@ extension HTTPClient {
199224

200225
self.kind = try Kind(forScheme: scheme)
201226
self.host = try self.kind.hostFromURL(url)
227+
self.socketPath = try self.kind.socketPathFromURL(url)
228+
self.uri = self.kind.uriFromURL(url)
202229

203230
self.redirectState = nil
204231
self.url = url
@@ -712,15 +739,7 @@ extension TaskHandler: ChannelDuplexHandler {
712739
self.state = .idle
713740
let request = self.unwrapOutboundIn(data)
714741

715-
let uri: String
716-
switch (self.kind, request.url.baseURL) {
717-
case (.host, _):
718-
uri = request.url.uri
719-
case (.unixSocket, .none):
720-
uri = "/" // we don't have a real path, the path we have is the path of the UNIX Domain Socket.
721-
case (.unixSocket, .some(_)):
722-
uri = request.url.uri
723-
}
742+
let uri: String = request.uri
724743

725744
var head = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1),
726745
method: request.method,

0 commit comments

Comments
 (0)