Skip to content

Commit a99090f

Browse files
authored
KTOR-4925 Fix CIO tunneling. (#3177)
1 parent 69792c3 commit a99090f

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

ktor-client/ktor-client-cio/jvmAndNix/src/io/ktor/client/engine/cio/Endpoint.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ internal class Endpoint(
177177
if (proxy?.type == ProxyType.HTTP) {
178178
startTunnel(requestData, connection.output, connection.input)
179179
}
180+
val realAddress = when (proxy) {
181+
null -> address
182+
else -> InetSocketAddress(requestData.url.host, requestData.url.port)
183+
}
180184
val tlsSocket = connection.tls(coroutineContext) {
181185
takeFrom(config.https)
182-
serverName = serverName ?: address.hostname
186+
serverName = serverName ?: realAddress.hostname
183187
}
184188
return tlsSocket.connection()
185189
} catch (cause: Throwable) {

ktor-client/ktor-client-cio/jvmAndNix/src/io/ktor/client/engine/cio/utils.kt

+3-9
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,9 @@ internal suspend fun startTunnel(
173173
val builder = RequestResponseBuilder()
174174

175175
try {
176-
builder.requestLine(HttpMethod("CONNECT"), request.url.hostWithPort, HttpProtocolVersion.HTTP_1_1.toString())
177-
// this will only add the port to the host header if the port is non-standard for the protocol
178-
val host = if (request.url.protocol.defaultPort == request.url.port) {
179-
request.url.host
180-
} else {
181-
request.url.hostWithPort
182-
}
183-
184-
builder.headerLine(HttpHeaders.Host, host)
176+
val hostWithPort = request.url.hostWithPort
177+
builder.requestLine(HttpMethod("CONNECT"), hostWithPort, HttpProtocolVersion.HTTP_1_1.toString())
178+
builder.headerLine(HttpHeaders.Host, hostWithPort)
185179
builder.headerLine("Proxy-Connection", "Keep-Alive") // For HTTP/1.0 proxies like Squid.
186180
request.headers[HttpHeaders.UserAgent]?.let {
187181
builder.headerLine(HttpHeaders.UserAgent, it)

0 commit comments

Comments
 (0)