@@ -108,63 +108,52 @@ extension HTTPClient {
108
108
private static var hostRestrictedSchemes : Set = [ " http " , " https " ]
109
109
private static var allSupportedSchemes : Set = [ " http " , " https " , " unix " , " http+unix " , " https+unix " ]
110
110
111
- init ( forScheme scheme: String ) throws {
112
- switch scheme {
113
- case " http " , " https " : self = . host
114
- case " unix " : self = . unixSocket( . baseURL)
115
- case " http+unix " : self = . unixSocket( . http_unix)
116
- case " https+unix " : self = . unixSocket( . https_unix)
117
- default :
118
- throw HTTPClientError . unsupportedScheme ( scheme)
119
- }
120
- }
111
+ func supportsRedirects( to scheme: String ? ) -> Bool {
112
+ guard let scheme = scheme? . lowercased ( ) else { return false }
121
113
122
- func hostFromURL( _ url: URL ) throws -> String {
123
114
switch self {
124
115
case . host:
125
- guard let host = url. host else {
126
- throw HTTPClientError . emptyHost
127
- }
128
- return host
116
+ return Kind . hostRestrictedSchemes. contains ( scheme)
129
117
case . unixSocket:
130
- return " "
118
+ return Kind . allSupportedSchemes . contains ( scheme )
131
119
}
132
120
}
121
+ }
133
122
134
- func socketPathFromURL( _ url: URL ) throws -> String {
135
- switch self {
136
- case . unixSocket( . baseURL) :
137
- return url. baseURL? . path ?? url. path
138
- case . unixSocket:
139
- guard let socketPath = url. host else {
140
- throw HTTPClientError . missingSocketPath
141
- }
142
- return socketPath
143
- case . host:
144
- return " "
145
- }
146
- }
123
+ static func useTLS( _ scheme: String ) -> Bool {
124
+ return scheme == " https " || scheme == " https+unix "
125
+ }
147
126
148
- func uriFromURL( _ url: URL ) -> String {
149
- switch self {
150
- case . host:
151
- return url. uri
152
- case . unixSocket( . baseURL) :
153
- return url. baseURL != nil ? url. uri : " / "
154
- case . unixSocket:
155
- return url. uri
156
- }
127
+ static func deconstructURL(
128
+ _ url: URL
129
+ ) throws -> (
130
+ kind: Kind , scheme: String , hostname: String , port: Int , socketPath: String , uri: String
131
+ ) {
132
+ guard let scheme = url. scheme? . lowercased ( ) else {
133
+ throw HTTPClientError . emptyScheme
157
134
}
158
-
159
- func supportsRedirects( to scheme: String ? ) -> Bool {
160
- guard let scheme = scheme? . lowercased ( ) else { return false }
161
-
162
- switch self {
163
- case . host:
164
- return Kind . hostRestrictedSchemes. contains ( scheme)
165
- case . unixSocket:
166
- return Kind . allSupportedSchemes. contains ( scheme)
135
+ switch scheme {
136
+ case " http " , " https " :
137
+ guard let host = url. host, !host. isEmpty else {
138
+ throw HTTPClientError . emptyHost
139
+ }
140
+ let defaultPort = self . useTLS ( scheme) ? 443 : 80
141
+ return ( . host, scheme, host, url. port ?? defaultPort, " " , url. uri)
142
+ case " http+unix " , " https+unix " :
143
+ guard let socketPath = url. host, !socketPath. isEmpty else {
144
+ throw HTTPClientError . missingSocketPath
145
+ }
146
+ let ( kind, defaultPort) = self . useTLS ( scheme) ? ( Kind . UnixScheme. https_unix, 443 ) : ( . http_unix, 80 )
147
+ return ( . unixSocket( kind) , scheme, " " , url. port ?? defaultPort, socketPath, url. uri)
148
+ case " unix " :
149
+ let socketPath = url. baseURL? . path ?? url. path
150
+ let uri = url. baseURL != nil ? url. uri : " / "
151
+ guard !socketPath. isEmpty else {
152
+ throw HTTPClientError . missingSocketPath
167
153
}
154
+ return ( . unixSocket( . baseURL) , scheme, " " , url. port ?? 80 , socketPath, uri)
155
+ default :
156
+ throw HTTPClientError . unsupportedScheme ( url. scheme!)
168
157
}
169
158
}
170
159
@@ -176,6 +165,8 @@ extension HTTPClient {
176
165
public let scheme : String
177
166
/// Remote host, resolved from `URL`.
178
167
public let host : String
168
+ /// Resolved port.
169
+ public let port : Int
179
170
/// Socket path, resolved from `URL`.
180
171
let socketPath : String
181
172
/// URI composed of the path and query, resolved from `URL`.
@@ -264,32 +255,18 @@ extension HTTPClient {
264
255
/// - `emptyHost` if URL does not contains a host.
265
256
/// - `missingSocketPath` if URL does not contains a socketPath as an encoded host.
266
257
public init ( url: URL , method: HTTPMethod = . GET, headers: HTTPHeaders = HTTPHeaders ( ) , body: Body ? = nil , tlsConfiguration: TLSConfiguration ? ) throws {
267
- guard let scheme = url. scheme? . lowercased ( ) else {
268
- throw HTTPClientError . emptyScheme
269
- }
270
-
271
- self . kind = try Kind ( forScheme: scheme)
272
- self . host = try self . kind. hostFromURL ( url)
273
- self . socketPath = try self . kind. socketPathFromURL ( url)
274
- self . uri = self . kind. uriFromURL ( url)
275
-
258
+ ( self . kind, self . scheme, self . host, self . port, self . socketPath, self . uri) = try Request . deconstructURL ( url)
276
259
self . redirectState = nil
277
260
self . url = url
278
261
self . method = method
279
- self . scheme = scheme
280
262
self . headers = headers
281
263
self . body = body
282
264
self . tlsConfiguration = tlsConfiguration
283
265
}
284
266
285
267
/// Whether request will be executed using secure socket.
286
268
public var useTLS : Bool {
287
- return self . scheme == " https " || self . scheme == " https+unix "
288
- }
289
-
290
- /// Resolved port.
291
- public var port : Int {
292
- return self . url. port ?? ( self . useTLS ? 443 : 80 )
269
+ return Request . useTLS ( self . scheme)
293
270
}
294
271
295
272
func createRequestHead( ) throws -> ( HTTPRequestHead , RequestFramingMetadata ) {
0 commit comments