Skip to content

Commit d3bbf37

Browse files
committed
Use Request.Kind to distinguish between host and unix socket kind of http request.
1 parent ac69f69 commit d3bbf37

File tree

3 files changed

+47
-163
lines changed

3 files changed

+47
-163
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public class HTTPClient {
282282
}
283283

284284
let eventLoopChannel: EventLoopFuture<Channel>
285-
if request.scheme == "unix", let baseURL = request.url.baseURL {
285+
if request.kind == .unixSocket, let baseURL = request.url.baseURL {
286286
eventLoopChannel = bootstrap.connect(unixDomainSocketPath: baseURL.path)
287287
} else {
288288
let address = self.resolveAddress(request: request, proxy: self.configuration.proxy)

Diff for: Sources/AsyncHTTPClient/HTTPHandler.swift

+46-136
Original file line numberDiff line numberDiff line change
@@ -88,48 +88,44 @@ extension HTTPClient {
8888

8989
/// Represent HTTP request.
9090
public struct Request {
91-
/// Request HTTP method, defaults to `GET`.
92-
public var method: HTTPMethod {
93-
return value.method
91+
92+
/// Represent kind of Request
93+
enum Kind {
94+
/// Remote host request.
95+
case host
96+
/// UNIX Domain Socket HTTP request.
97+
case unixSocket
98+
99+
func isSchemeSupported(scheme: String) -> Bool {
100+
switch self {
101+
case .host:
102+
return scheme == "http" || scheme == "https"
103+
case .unixSocket:
104+
return scheme == "unix"
105+
}
106+
}
94107
}
108+
109+
/// Request HTTP method, defaults to `GET`.
110+
public var method: HTTPMethod
95111
/// Remote URL.
96-
public var url: URL {
97-
return value.url
98-
}
112+
public var url: URL
99113
/// Remote HTTP scheme, resolved from `URL`.
100-
public var scheme: String {
101-
return value.scheme
102-
}
114+
public var scheme: String
103115
/// Remote host, resolved from `URL`.
104-
public var host: String {
105-
return value.host
106-
}
116+
public var host: String
107117
/// Request custom HTTP Headers, defaults to no headers.
108-
public var headers: HTTPHeaders {
109-
get {
110-
return value.headers
111-
}
112-
set {
113-
value.headers = newValue
114-
}
115-
}
118+
public var headers: HTTPHeaders
116119
/// Request body, defaults to no body.
117-
public var body: Body? {
118-
get {
119-
return value.body
120-
}
121-
set {
122-
value.body = newValue
123-
}
124-
}
120+
public var body: Body?
125121

126122
struct RedirectState {
127123
var count: Int
128124
var visited: Set<URL>?
129125
}
130126

131127
var redirectState: RedirectState?
132-
private var value: HTTPRequest
128+
let kind: Kind
133129

134130
/// Create HTTP request.
135131
///
@@ -156,58 +152,6 @@ extension HTTPClient {
156152
///
157153
/// - parameters:
158154
/// - url: Remote `URL`.
159-
/// - version: HTTP version.
160-
/// - method: HTTP method.
161-
/// - headers: Custom HTTP headers.
162-
/// - body: Request body.
163-
/// - throws:
164-
/// - `emptyScheme` if URL does not contain HTTP scheme.
165-
/// - `unsupportedScheme` if URL does contains unsupported HTTP scheme.
166-
/// - `emptyHost` if URL does not contains a host.
167-
public init(url: URL, method: HTTPMethod = .GET, headers: HTTPHeaders = HTTPHeaders(), body: Body? = nil) throws {
168-
if url.scheme?.lowercased() == "unix" {
169-
self.value = try UnixDomainRequest(url: url, method: method, headers: headers, body: body)
170-
} else {
171-
self.value = try HostRequest(url: url, method: method, headers: headers, body: body)
172-
}
173-
self.redirectState = nil
174-
}
175-
176-
/// Whether request will be executed using secure socket.
177-
public var useTLS: Bool {
178-
return self.scheme == "https"
179-
}
180-
181-
/// Resolved port.
182-
public var port: Int {
183-
return self.url.port ?? (self.useTLS ? 443 : 80)
184-
}
185-
186-
func isSchemeSupported(scheme: String) -> Bool {
187-
return type(of: self.value).isSchemeSupported(scheme: scheme)
188-
}
189-
}
190-
191-
/// Represent HTTP request.
192-
public struct HostRequest: HTTPRequest {
193-
/// Request HTTP method, defaults to `GET`.
194-
public let method: HTTPMethod
195-
/// Remote URL.
196-
public let url: URL
197-
/// Remote HTTP scheme, resolved from `URL`.
198-
public let scheme: String
199-
/// Remote host, resolved from `URL`.
200-
public let host: String
201-
/// Request custom HTTP Headers, defaults to no headers.
202-
public var headers: HTTPHeaders
203-
/// Request body, defaults to no body.
204-
public var body: Body?
205-
206-
/// Create an HTTP `Request`.
207-
///
208-
/// - parameters:
209-
/// - url: Remote `URL`.
210-
/// - version: HTTP version.
211155
/// - method: HTTP method.
212156
/// - headers: Custom HTTP headers.
213157
/// - body: Request body.
@@ -220,75 +164,41 @@ extension HTTPClient {
220164
throw HTTPClientError.emptyScheme
221165
}
222166

223-
guard Self.isSchemeSupported(scheme: scheme) else {
224-
throw HTTPClientError.unsupportedScheme(scheme)
225-
}
167+
if Kind.host.isSchemeSupported(scheme: scheme) {
168+
self.kind = .host
169+
guard let host = url.host else {
170+
throw HTTPClientError.emptyHost
171+
}
226172

227-
guard let host = url.host else {
228-
throw HTTPClientError.emptyHost
173+
self.host = host
174+
} else if Kind.unixSocket.isSchemeSupported(scheme: scheme) {
175+
self.kind = .unixSocket
176+
self.host = ""
177+
} else {
178+
throw HTTPClientError.unsupportedScheme(scheme)
229179
}
230180

231-
self.method = method
181+
self.redirectState = nil
232182
self.url = url
183+
self.method = method
233184
self.scheme = scheme
234-
self.host = host
235185
self.headers = headers
236186
self.body = body
237187
}
238188

239-
static func isSchemeSupported(scheme: String) -> Bool {
240-
return scheme == "http" || scheme == "https"
189+
/// Whether request will be executed using secure socket.
190+
public var useTLS: Bool {
191+
return self.scheme == "https"
241192
}
242-
}
243-
244-
/// Represent UNIX Domain Socket HTTP request.
245-
public struct UnixDomainRequest: HTTPRequest {
246-
/// Request HTTP method, defaults to `GET`.
247-
public let method: HTTPMethod
248-
/// UNIX Domain Socket file URL.
249-
public let url: URL
250-
/// Remote HTTP scheme, resolved from `URL`. Unused.
251-
public let scheme: String
252-
/// Remote host, resolved from `URL`. Unused.
253-
public let host: String
254-
/// Request custom HTTP Headers, defaults to no headers.
255-
public var headers: HTTPHeaders
256-
/// Request body, defaults to no body.
257-
public var body: Body?
258-
259-
/// Create an HTTP `Request`.
260-
///
261-
/// - parameters:
262-
/// - url: UNIX Domain Socket `URL`.
263-
/// - version: HTTP version.
264-
/// - method: HTTP method.
265-
/// - headers: Custom HTTP headers.
266-
/// - body: Request body.
267-
/// - throws:
268-
/// - `emptyScheme` if URL does not contain HTTP scheme.
269-
/// - `unsupportedScheme` if URL does contains unsupported HTTP scheme.
270-
/// - `emptyHost` if URL does not contains a host.
271-
public init(url: URL, method: HTTPMethod = .GET, headers: HTTPHeaders = HTTPHeaders(), body: Body? = nil) throws {
272-
guard let scheme = url.scheme?.lowercased() else {
273-
throw HTTPClientError.emptyScheme
274-
}
275193

276-
guard scheme == "unix" else {
277-
throw HTTPClientError.invalidURL
278-
}
279-
280-
self.method = method
281-
self.url = url
282-
self.scheme = scheme
283-
self.host = ""
284-
self.headers = headers
285-
self.body = body
194+
/// Resolved port.
195+
public var port: Int {
196+
return self.url.port ?? (self.useTLS ? 443 : 80)
286197
}
287198

288-
static func isSchemeSupported(scheme: String) -> Bool {
289-
return scheme == "unix"
199+
func isSchemeSupported(scheme: String) -> Bool {
200+
return kind.isSchemeSupported(scheme: scheme)
290201
}
291-
292202
}
293203

294204
/// Represent HTTP response.

Diff for: Sources/AsyncHTTPClient/HTTPRequest.swift

-26
This file was deleted.

0 commit comments

Comments
 (0)