Skip to content

Commit

Permalink
KTOR-4925 Fix CIO tunneling. (#3177)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov committed Oct 4, 2022
1 parent 69792c3 commit a99090f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ internal class Endpoint(
if (proxy?.type == ProxyType.HTTP) {
startTunnel(requestData, connection.output, connection.input)
}
val realAddress = when (proxy) {
null -> address
else -> InetSocketAddress(requestData.url.host, requestData.url.port)
}
val tlsSocket = connection.tls(coroutineContext) {
takeFrom(config.https)
serverName = serverName ?: address.hostname
serverName = serverName ?: realAddress.hostname
}
return tlsSocket.connection()
} catch (cause: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,9 @@ internal suspend fun startTunnel(
val builder = RequestResponseBuilder()

try {
builder.requestLine(HttpMethod("CONNECT"), request.url.hostWithPort, HttpProtocolVersion.HTTP_1_1.toString())
// this will only add the port to the host header if the port is non-standard for the protocol
val host = if (request.url.protocol.defaultPort == request.url.port) {
request.url.host
} else {
request.url.hostWithPort
}

builder.headerLine(HttpHeaders.Host, host)
val hostWithPort = request.url.hostWithPort
builder.requestLine(HttpMethod("CONNECT"), hostWithPort, HttpProtocolVersion.HTTP_1_1.toString())
builder.headerLine(HttpHeaders.Host, hostWithPort)
builder.headerLine("Proxy-Connection", "Keep-Alive") // For HTTP/1.0 proxies like Squid.
request.headers[HttpHeaders.UserAgent]?.let {
builder.headerLine(HttpHeaders.UserAgent, it)
Expand Down

0 comments on commit a99090f

Please sign in to comment.