@@ -92,82 +92,16 @@ extension HTTPClient {
92
92
93
93
/// Represent HTTP request.
94
94
public struct Request {
95
- /// Represent kind of Request
96
- enum Kind : Equatable {
97
- enum UnixScheme : Equatable {
98
- case baseURL
99
- case http_unix
100
- case https_unix
101
- }
102
-
103
- /// Remote host request.
104
- case host
105
- /// UNIX Domain Socket HTTP request.
106
- case unixSocket( _ scheme: UnixScheme )
107
-
108
- private static var hostRestrictedSchemes : Set = [ " http " , " https " ]
109
- private static var allSupportedSchemes : Set = [ " http " , " https " , " unix " , " http+unix " , " https+unix " ]
110
-
111
- func supportsRedirects( to scheme: String ? ) -> Bool {
112
- guard let scheme = scheme? . lowercased ( ) else { return false }
113
-
114
- switch self {
115
- case . host:
116
- return Kind . hostRestrictedSchemes. contains ( scheme)
117
- case . unixSocket:
118
- return Kind . allSupportedSchemes. contains ( scheme)
119
- }
120
- }
121
- }
122
-
123
- static func useTLS( _ scheme: String ) -> Bool {
124
- return scheme == " https " || scheme == " https+unix "
125
- }
126
-
127
- static func deconstructURL(
128
- _ url: URL
129
- ) throws -> ( kind: Kind , scheme: String , connectionTarget: ConnectionTarget , uri: String ) {
130
- guard let scheme = url. scheme? . lowercased ( ) else {
131
- throw HTTPClientError . emptyScheme
132
- }
133
- switch scheme {
134
- case " http " , " https " :
135
- guard let host = url. host, !host. isEmpty else {
136
- throw HTTPClientError . emptyHost
137
- }
138
- let defaultPort = self . useTLS ( scheme) ? 443 : 80
139
- let hostTarget = ConnectionTarget ( remoteHost: host, port: url. port ?? defaultPort)
140
- return ( . host, scheme, hostTarget, url. uri)
141
- case " http+unix " , " https+unix " :
142
- guard let socketPath = url. host, !socketPath. isEmpty else {
143
- throw HTTPClientError . missingSocketPath
144
- }
145
- let socketTarget = ConnectionTarget . unixSocket ( path: socketPath)
146
- let kind = self . useTLS ( scheme) ? Kind . UnixScheme. https_unix : . http_unix
147
- return ( . unixSocket( kind) , scheme, socketTarget, 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
153
- }
154
- let socketTarget = ConnectionTarget . unixSocket ( path: socketPath)
155
- return ( . unixSocket( . baseURL) , scheme, socketTarget, uri)
156
- default :
157
- throw HTTPClientError . unsupportedScheme ( url. scheme!)
158
- }
159
- }
160
-
161
95
/// Request HTTP method, defaults to `GET`.
162
96
public let method : HTTPMethod
163
97
/// Remote URL.
164
98
public let url : URL
99
+
165
100
/// Remote HTTP scheme, resolved from `URL`.
166
- public let scheme : String
167
- /// The connection target, resolved from `URL`.
168
- let connectionTarget : ConnectionTarget
169
- /// URI composed of the path and query, resolved from `URL`.
170
- let uri : String
101
+ public var scheme : String {
102
+ self . deconstructedURL. scheme. rawValue
103
+ }
104
+
171
105
/// Request custom HTTP Headers, defaults to no headers.
172
106
public var headers : HTTPHeaders
173
107
/// Request body, defaults to no body.
@@ -180,8 +114,10 @@ extension HTTPClient {
180
114
var visited : Set < URL > ?
181
115
}
182
116
117
+ /// Parsed, validated and deconstructed URL.
118
+ let deconstructedURL : DeconstructedURL
119
+
183
120
var redirectState : RedirectState ?
184
- let kind : Kind
185
121
186
122
/// Create HTTP request.
187
123
///
@@ -252,7 +188,8 @@ extension HTTPClient {
252
188
/// - `emptyHost` if URL does not contains a host.
253
189
/// - `missingSocketPath` if URL does not contains a socketPath as an encoded host.
254
190
public init ( url: URL , method: HTTPMethod = . GET, headers: HTTPHeaders = HTTPHeaders ( ) , body: Body ? = nil , tlsConfiguration: TLSConfiguration ? ) throws {
255
- ( self . kind, self . scheme, self . connectionTarget, self . uri) = try Request . deconstructURL ( url)
191
+ self . deconstructedURL = try DeconstructedURL ( url: url)
192
+
256
193
self . redirectState = nil
257
194
self . url = url
258
195
self . method = method
@@ -261,14 +198,9 @@ extension HTTPClient {
261
198
self . tlsConfiguration = tlsConfiguration
262
199
}
263
200
264
- /// Whether request will be executed using secure socket.
265
- public var useTLS : Bool {
266
- return Request . useTLS ( self . scheme)
267
- }
268
-
269
201
/// Remote host, resolved from `URL`.
270
202
public var host : String {
271
- switch self . connectionTarget {
203
+ switch self . deconstructedURL . connectionTarget {
272
204
case . ipAddress( let serialization, _) : return serialization
273
205
case . domain( let name, _) : return name
274
206
case . unixSocket: return " "
@@ -277,25 +209,28 @@ extension HTTPClient {
277
209
278
210
/// Resolved port.
279
211
public var port : Int {
280
- switch self . connectionTarget {
212
+ switch self . deconstructedURL . connectionTarget {
281
213
case . ipAddress( _, let address) : return address. port!
282
214
case . domain( _, let port) : return port
283
- case . unixSocket: return Request . useTLS ( self . scheme) ? 443 : 80
215
+ case . unixSocket: return self . deconstructedURL . scheme. defaultPort
284
216
}
285
217
}
286
218
219
+ /// Whether request will be executed using secure socket.
220
+ public var useTLS : Bool { self . deconstructedURL. scheme. usesTLS }
221
+
287
222
func createRequestHead( ) throws -> ( HTTPRequestHead , RequestFramingMetadata ) {
288
223
var head = HTTPRequestHead (
289
224
version: . http1_1,
290
225
method: self . method,
291
- uri: self . uri,
226
+ uri: self . deconstructedURL . uri,
292
227
headers: self . headers
293
228
)
294
229
295
230
if !head. headers. contains ( name: " host " ) {
296
231
let port = self . port
297
232
var host = self . host
298
- if !( port == 80 && self . scheme == " http " ) , !( port == 443 && self . scheme == " https " ) {
233
+ if !( port == 80 && self . deconstructedURL . scheme == . http) , !( port == 443 && self . deconstructedURL . scheme == . https) {
299
234
host += " : \( port) "
300
235
}
301
236
head. headers. add ( name: " host " , value: host)
@@ -740,7 +675,7 @@ internal struct RedirectHandler<ResponseType> {
740
675
return nil
741
676
}
742
677
743
- guard self . request. kind . supportsRedirects ( to: url. scheme) else {
678
+ guard self . request. deconstructedURL . scheme . supportsRedirects ( to: url. scheme) else {
744
679
return nil
745
680
}
746
681
0 commit comments