@@ -88,12 +88,54 @@ extension HTTPClient {
8888
8989 /// Represent HTTP request.
9090 public struct Request {
91+ /// Represent kind of Request
92+ enum Kind {
93+ /// Remote host request.
94+ case host
95+ /// UNIX Domain Socket HTTP request.
96+ case unixSocket
97+
98+ private static var hostSchemes = [ " http " , " https " ]
99+ private static var unixSchemes = [ " unix " ]
100+
101+ init ( forScheme scheme: String ) throws {
102+ if Kind . host. supports ( scheme: scheme) {
103+ self = . host
104+ } else if Kind . unixSocket. supports ( scheme: scheme) {
105+ self = . unixSocket
106+ } else {
107+ throw HTTPClientError . unsupportedScheme ( scheme)
108+ }
109+ }
110+
111+ func hostFromURL( _ url: URL ) throws -> String {
112+ switch self {
113+ case . host:
114+ guard let host = url. host else {
115+ throw HTTPClientError . emptyHost
116+ }
117+ return host
118+ case . unixSocket:
119+ return " "
120+ }
121+ }
122+
123+ func supports( scheme: String ) -> Bool {
124+ switch self {
125+ case . host:
126+ return Kind . hostSchemes. contains ( scheme)
127+ case . unixSocket:
128+ return Kind . unixSchemes. contains ( scheme)
129+ }
130+ }
131+ }
132+
91133 /// Request HTTP method, defaults to `GET`.
92- public let method : HTTPMethod
134+ public var method : HTTPMethod
93135 /// Remote URL.
94- public let url : URL
136+ public var url : URL
95137 /// Remote HTTP scheme, resolved from `URL`.
96- public let scheme : String
138+ public var scheme : String
97139 /// Remote host, resolved from `URL`.
98140 public let host : String
99141 /// Request custom HTTP Headers, defaults to no headers.
@@ -107,6 +149,7 @@ extension HTTPClient {
107149 }
108150
109151 var redirectState : RedirectState ?
152+ let kind : Kind
110153
111154 /// Create HTTP request.
112155 ///
@@ -133,7 +176,6 @@ extension HTTPClient {
133176 ///
134177 /// - parameters:
135178 /// - url: Remote `URL`.
136- /// - version: HTTP version.
137179 /// - method: HTTP method.
138180 /// - headers: Custom HTTP headers.
139181 /// - body: Request body.
@@ -146,22 +188,15 @@ extension HTTPClient {
146188 throw HTTPClientError . emptyScheme
147189 }
148190
149- guard Request . isSchemeSupported ( scheme: scheme) else {
150- throw HTTPClientError . unsupportedScheme ( scheme)
151- }
152-
153- guard let host = url. host else {
154- throw HTTPClientError . emptyHost
155- }
191+ self . kind = try Kind ( forScheme: scheme)
192+ self . host = try self . kind. hostFromURL ( url)
156193
157- self . method = method
194+ self . redirectState = nil
158195 self . url = url
196+ self . method = method
159197 self . scheme = scheme
160- self . host = host
161198 self . headers = headers
162199 self . body = body
163-
164- self . redirectState = nil
165200 }
166201
167202 /// Whether request will be executed using secure socket.
@@ -173,10 +208,6 @@ extension HTTPClient {
173208 public var port : Int {
174209 return self . url. port ?? ( self . useTLS ? 443 : 80 )
175210 }
176-
177- static func isSchemeSupported( scheme: String ) -> Bool {
178- return scheme == " http " || scheme == " https "
179- }
180211 }
181212
182213 /// Represent HTTP response.
@@ -812,7 +843,7 @@ internal struct RedirectHandler<ResponseType> {
812843 return nil
813844 }
814845
815- guard HTTPClient . Request . isSchemeSupported ( scheme: self . request. scheme) else {
846+ guard self . request . kind . supports ( scheme: self . request. scheme) else {
816847 return nil
817848 }
818849
0 commit comments