Skip to content

Commit 5aba80d

Browse files
authored
read proxy from env
1 parent 0ce6cc5 commit 5aba80d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,40 @@ public protocol HTTPRequestExecutor {
1111

1212
/// An `HTTPRequestExecutor` backed by the shared `HTTPClient`.
1313
internal struct HTTPRequestExecutorImpl: HTTPRequestExecutor {
14+
let httpClient: HTTPClient
15+
16+
public init() {
17+
var proxy: HTTPClient.Configuration.Proxy?
18+
19+
func getProxyFromEnv(keys: [String]) -> HTTPClient.Configuration.Proxy? {
20+
let environment = ProcessInfo.processInfo.environment
21+
for key in keys {
22+
if let proxyString = environment[key],
23+
let url = URL(string: proxyString),
24+
let host = url.host,
25+
let port = url.port {
26+
return .server(host: host, port: port)
27+
}
28+
}
29+
return nil
30+
}
31+
32+
if let httpProxy = getProxyFromEnv(keys: ["http_proxy", "HTTP_PROXY"]) {
33+
proxy = httpProxy
34+
}
35+
if let httpsProxy = getProxyFromEnv(keys: ["https_proxy", "HTTPS_PROXY"]) {
36+
proxy = httpsProxy
37+
}
38+
39+
if proxy != nil {
40+
self.httpClient = HTTPClient(eventLoopGroupProvider: .singleton, configuration: HTTPClient.Configuration(proxy: proxy))
41+
} else {
42+
self.httpClient = HTTPClient.shared
43+
}
44+
}
45+
1446
public func execute(_ request: HTTPClientRequest, timeout: TimeAmount) async throws -> HTTPClientResponse {
15-
try await HTTPClient.shared.execute(request, timeout: timeout)
47+
try await httpClient.execute(request, timeout: timeout)
1648
}
1749
}
1850

0 commit comments

Comments
 (0)