mirror of
https://github.com/swift-server/async-http-client.git
synced 2026-06-02 07:37:34 +00:00
Support UNIX Domain Sockets (#151)
Adds support for UNIX Domain Socket requests. Usage: ``` let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) let socketURL = URL(string: "unix:///var/run/docker.sock")! let req = try HTTPClient.Request(url: URL(string: "/users/list", relativeTo: socketURL)!, method: .GET) let response = try httpClient.execute(request: req).wait() ```
This commit is contained in:
committed by
Johannes Weiss
parent
a8e6f8de43
commit
e90f5fd03d
@@ -281,15 +281,22 @@ public class HTTPClient {
|
||||
bootstrap = bootstrap.connectTimeout(timeout)
|
||||
}
|
||||
|
||||
let address = self.resolveAddress(request: request, proxy: self.configuration.proxy)
|
||||
bootstrap.connect(host: address.host, port: address.port)
|
||||
.map { channel in
|
||||
task.setChannel(channel)
|
||||
}
|
||||
.flatMap { channel in
|
||||
channel.writeAndFlush(request)
|
||||
}
|
||||
.cascadeFailure(to: task.promise)
|
||||
let eventLoopChannel: EventLoopFuture<Channel>
|
||||
if request.kind == .unixSocket, let baseURL = request.url.baseURL {
|
||||
eventLoopChannel = bootstrap.connect(unixDomainSocketPath: baseURL.path)
|
||||
} else {
|
||||
let address = self.resolveAddress(request: request, proxy: self.configuration.proxy)
|
||||
eventLoopChannel = bootstrap.connect(host: address.host, port: address.port)
|
||||
}
|
||||
|
||||
eventLoopChannel.map { channel in
|
||||
task.setChannel(channel)
|
||||
}
|
||||
.flatMap { channel in
|
||||
channel.writeAndFlush(request)
|
||||
}
|
||||
.cascadeFailure(to: task.promise)
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
@@ -481,12 +488,12 @@ private extension ChannelPipeline {
|
||||
func addProxyHandler(for request: HTTPClient.Request, decoder: ByteToMessageHandler<HTTPResponseDecoder>, encoder: HTTPRequestEncoder, tlsConfiguration: TLSConfiguration?, proxy: HTTPClient.Configuration.Proxy?) -> EventLoopFuture<Void> {
|
||||
let handler = HTTPClientProxyHandler(host: request.host, port: request.port, authorization: proxy?.authorization, onConnect: { channel in
|
||||
channel.pipeline.removeHandler(decoder).flatMap {
|
||||
return channel.pipeline.addHandler(
|
||||
channel.pipeline.addHandler(
|
||||
ByteToMessageHandler(HTTPResponseDecoder(leftOverBytesStrategy: .forwardBytes)),
|
||||
position: .after(encoder)
|
||||
)
|
||||
}.flatMap {
|
||||
return channel.pipeline.addSSLHandlerIfNeeded(for: request, tlsConfiguration: tlsConfiguration)
|
||||
channel.pipeline.addSSLHandlerIfNeeded(for: request, tlsConfiguration: tlsConfiguration)
|
||||
}
|
||||
})
|
||||
return self.addHandler(handler)
|
||||
|
||||
Reference in New Issue
Block a user