diff --git a/Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift b/Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift index fbcd4f9c0..7e12a9ef2 100644 --- a/Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift +++ b/Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift @@ -35,6 +35,7 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand private var state: State = .initialized + private let scheme: Scheme private let targetHost: String private let targetPort: Int private let proxyAuthorization: HTTPClient.Authorization? @@ -46,6 +47,7 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand } convenience init( + scheme: Scheme, target: ConnectionTarget, proxyAuthorization: HTTPClient.Authorization?, deadline: NIODeadline @@ -63,6 +65,7 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand fatalError("Unix Domain Sockets do not support proxies") } self.init( + scheme: scheme, targetHost: targetHost, targetPort: targetPort, proxyAuthorization: proxyAuthorization, @@ -70,10 +73,12 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand ) } - init(targetHost: String, + init(scheme: Scheme, + targetHost: String, targetPort: Int, proxyAuthorization: HTTPClient.Authorization?, deadline: NIODeadline) { + self.scheme = scheme self.targetHost = targetHost self.targetPort = targetPort self.proxyAuthorization = proxyAuthorization @@ -155,7 +160,13 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand method: .CONNECT, uri: "\(self.targetHost):\(self.targetPort)" ) - head.headers.replaceOrAdd(name: "host", value: "\(self.targetHost)") + if !head.headers.contains(name: "host") { + var host = self.targetHost + if (self.targetPort != self.scheme.defaultPort) { + host += ":\(self.targetPort)" + } + head.headers.add(name: "host", value: host) + } if let authorization = self.proxyAuthorization { head.headers.replaceOrAdd(name: "proxy-authorization", value: authorization.headerValue) } diff --git a/Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift b/Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift index e94e967a6..e73fb480b 100644 --- a/Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift +++ b/Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift @@ -195,6 +195,7 @@ extension HTTPConnectionPool.ConnectionFactory { let encoder = HTTPRequestEncoder() let decoder = ByteToMessageHandler(HTTPResponseDecoder(leftOverBytesStrategy: .dropBytes)) let proxyHandler = HTTP1ProxyConnectHandler( + scheme: self.key.scheme, target: self.key.connectionTarget, proxyAuthorization: proxy.authorization, deadline: deadline diff --git a/Tests/AsyncHTTPClientTests/HTTP1ProxyConnectHandlerTests.swift b/Tests/AsyncHTTPClientTests/HTTP1ProxyConnectHandlerTests.swift index b3917173f..915071690 100644 --- a/Tests/AsyncHTTPClientTests/HTTP1ProxyConnectHandlerTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTP1ProxyConnectHandlerTests.swift @@ -27,6 +27,7 @@ class HTTP1ProxyConnectHandlerTests: XCTestCase { XCTAssertNoThrow(try embedded.connect(to: socketAddress).wait()) let proxyConnectHandler = HTTP1ProxyConnectHandler( + scheme: .https, targetHost: "swift.org", targetPort: 443, proxyAuthorization: .none, @@ -61,6 +62,7 @@ class HTTP1ProxyConnectHandlerTests: XCTestCase { XCTAssertNoThrow(try embedded.connect(to: socketAddress).wait()) let proxyConnectHandler = HTTP1ProxyConnectHandler( + scheme: .https, targetHost: "swift.org", targetPort: 443, proxyAuthorization: .basic(credentials: "abc123"), @@ -95,6 +97,7 @@ class HTTP1ProxyConnectHandlerTests: XCTestCase { XCTAssertNoThrow(try embedded.connect(to: socketAddress).wait()) let proxyConnectHandler = HTTP1ProxyConnectHandler( + scheme: .https, targetHost: "swift.org", targetPort: 443, proxyAuthorization: .none, @@ -135,6 +138,7 @@ class HTTP1ProxyConnectHandlerTests: XCTestCase { XCTAssertNoThrow(try embedded.connect(to: socketAddress).wait()) let proxyConnectHandler = HTTP1ProxyConnectHandler( + scheme: .https, targetHost: "swift.org", targetPort: 443, proxyAuthorization: .none, @@ -175,6 +179,7 @@ class HTTP1ProxyConnectHandlerTests: XCTestCase { XCTAssertNoThrow(try embedded.connect(to: socketAddress).wait()) let proxyConnectHandler = HTTP1ProxyConnectHandler( + scheme: .https, targetHost: "swift.org", targetPort: 443, proxyAuthorization: .none,