@@ -108,63 +108,43 @@ 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 " "
123
+ static func deconstructURL(
124
+ _ url: URL
125
+ ) throws -> ( kind: Kind , hostname: String , socketPath: String , uri: String ) {
126
+ let scheme = url. scheme? . lowercased ( )
127
+ switch scheme {
128
+ case " http " , " https " :
129
+ guard let host = url. host, !host. isEmpty else {
130
+ throw HTTPClientError . emptyHost
145
131
}
146
- }
147
-
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
132
+ return ( . host, host, " " , url. uri)
133
+ case " http+unix " , " https+unix " :
134
+ guard let socketPath = url. host, !socketPath. isEmpty else {
135
+ throw HTTPClientError . missingSocketPath
156
136
}
157
- }
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)
137
+ let unixKind : Kind . UnixScheme = ( scheme == " http+unix " ) ? . http_unix : . https_unix
138
+ return ( . unixSocket( unixKind) , " " , socketPath, url. uri)
139
+ case " unix " :
140
+ let socketPath = url. baseURL? . path ?? url. path
141
+ let uri = url. baseURL != nil ? url. uri : " / "
142
+ guard !socketPath. isEmpty else {
143
+ throw HTTPClientError . missingSocketPath
167
144
}
145
+ return ( . unixSocket( . baseURL) , " " , socketPath, uri)
146
+ default :
147
+ throw HTTPClientError . unsupportedScheme ( url. scheme ?? " " )
168
148
}
169
149
}
170
150
@@ -267,12 +247,7 @@ extension HTTPClient {
267
247
guard let scheme = url. scheme? . lowercased ( ) else {
268
248
throw HTTPClientError . emptyScheme
269
249
}
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
-
250
+ ( self . kind, self . host, self . socketPath, self . uri) = try Self . deconstructURL ( url)
276
251
self . redirectState = nil
277
252
self . url = url
278
253
self . method = method
0 commit comments