@@ -99,20 +99,27 @@ extension HTTPClient {
99
99
public struct Request {
100
100
/// Represent kind of Request
101
101
enum Kind {
102
+ enum UnixScheme {
103
+ case baseURL
104
+ case http_unix
105
+ case https_unix
106
+ }
107
+
102
108
/// Remote host request.
103
109
case host
104
110
/// UNIX Domain Socket HTTP request.
105
- case unixSocket
111
+ case unixSocket( _ scheme : UnixScheme )
106
112
107
113
private static var hostSchemes = [ " http " , " https " ]
108
- private static var unixSchemes = [ " unix " ]
114
+ private static var unixSchemes = [ " unix " , " http+unix " , " https+unix " ]
109
115
110
116
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 {
117
+ switch scheme {
118
+ case " http " , " https " : self = . host
119
+ case " unix " : self = . unixSocket( . baseURL)
120
+ case " http+unix " : self = . unixSocket( . http_unix)
121
+ case " https+unix " : self = . unixSocket( . https_unix)
122
+ default :
116
123
throw HTTPClientError . unsupportedScheme ( scheme)
117
124
}
118
125
}
@@ -129,6 +136,31 @@ extension HTTPClient {
129
136
}
130
137
}
131
138
139
+ func socketPathFromURL( _ url: URL ) throws -> String {
140
+ switch self {
141
+ case . unixSocket( . baseURL) :
142
+ return url. baseURL? . path ?? url. path
143
+ case . unixSocket:
144
+ guard let socketPath = url. host else {
145
+ throw HTTPClientError . missingSocketPath
146
+ }
147
+ return socketPath
148
+ case . host:
149
+ return " "
150
+ }
151
+ }
152
+
153
+ func uriFromURL( _ url: URL ) -> String {
154
+ switch self {
155
+ case . host:
156
+ return url. uri
157
+ case . unixSocket( . baseURL) :
158
+ return url. baseURL != nil ? url. uri : " / "
159
+ case . unixSocket:
160
+ return url. uri
161
+ }
162
+ }
163
+
132
164
func supports( scheme: String ) -> Bool {
133
165
switch self {
134
166
case . host:
@@ -147,6 +179,10 @@ extension HTTPClient {
147
179
public let scheme : String
148
180
/// Remote host, resolved from `URL`.
149
181
public let host : String
182
+ /// Socket path, resolved from `URL`.
183
+ let socketPath : String
184
+ /// URI composed of the path and query, resolved from `URL`.
185
+ let uri : String
150
186
/// Request custom HTTP Headers, defaults to no headers.
151
187
public var headers : HTTPHeaders
152
188
/// Request body, defaults to no body.
@@ -192,13 +228,16 @@ extension HTTPClient {
192
228
/// - `emptyScheme` if URL does not contain HTTP scheme.
193
229
/// - `unsupportedScheme` if URL does contains unsupported HTTP scheme.
194
230
/// - `emptyHost` if URL does not contains a host.
231
+ /// - `missingSocketPath` if URL does not contains a socketPath as an encoded host.
195
232
public init ( url: URL , method: HTTPMethod = . GET, headers: HTTPHeaders = HTTPHeaders ( ) , body: Body ? = nil ) throws {
196
233
guard let scheme = url. scheme? . lowercased ( ) else {
197
234
throw HTTPClientError . emptyScheme
198
235
}
199
236
200
237
self . kind = try Kind ( forScheme: scheme)
201
238
self . host = try self . kind. hostFromURL ( url)
239
+ self . socketPath = try self . kind. socketPathFromURL ( url)
240
+ self . uri = self . kind. uriFromURL ( url)
202
241
203
242
self . redirectState = nil
204
243
self . url = url
@@ -210,7 +249,7 @@ extension HTTPClient {
210
249
211
250
/// Whether request will be executed using secure socket.
212
251
public var useTLS : Bool {
213
- return self . scheme == " https "
252
+ return self . scheme == " https " || self . scheme == " https+unix "
214
253
}
215
254
216
255
/// Resolved port.
@@ -712,19 +751,9 @@ extension TaskHandler: ChannelDuplexHandler {
712
751
self . state = . idle
713
752
let request = self . unwrapOutboundIn ( data)
714
753
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
- }
724
-
725
754
var head = HTTPRequestHead ( version: HTTPVersion ( major: 1 , minor: 1 ) ,
726
755
method: request. method,
727
- uri: uri)
756
+ uri: request . uri)
728
757
var headers = request. headers
729
758
730
759
if !request. headers. contains ( name: " Host " ) {
0 commit comments