From 37bfadc452e2055ca7349d161b81d7ae6b7d78c2 Mon Sep 17 00:00:00 2001 From: Archkon <180910180+Archkon@users.noreply.github.com> Date: Wed, 24 Jun 2026 22:32:15 +0800 Subject: [PATCH 1/2] http: add CONNECT method handling for default Host header with proxy Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> --- lib/_http_client.js | 26 +++++++------ .../test-http-connect-default-host-header.js | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 test/parallel/test-http-connect-default-host-header.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 73d7b84c17a8fd..4729e87393f0a1 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -375,18 +375,22 @@ function ClientRequest(input, options, cb) { if (host && !this.getHeader('host') && setHost) { let hostHeader = host; - // For the Host header, ensure that IPv6 addresses are enclosed - // in square brackets, as defined by URI formatting - // https://tools.ietf.org/html/rfc3986#section-3.2.2 - const posColon = hostHeader.indexOf(':'); - if (posColon !== -1 && - hostHeader.includes(':', posColon + 1) && - hostHeader.charCodeAt(0) !== 91/* '[' */) { - hostHeader = `[${hostHeader}]`; - } + if (method === 'CONNECT' && options.path) { + hostHeader = String(this.path); + } else { + // For the Host header, ensure that IPv6 addresses are enclosed + // in square brackets, as defined by URI formatting + // https://tools.ietf.org/html/rfc3986#section-3.2.2 + const posColon = hostHeader.indexOf(':'); + if (posColon !== -1 && + hostHeader.includes(':', posColon + 1) && + hostHeader.charCodeAt(0) !== 91/* '[' */) { + hostHeader = `[${hostHeader}]`; + } - if (port && +port !== defaultPort) { - hostHeader += ':' + port; + if (port && +port !== defaultPort) { + hostHeader += ':' + port; + } } this.setHeader('Host', hostHeader); } diff --git a/test/parallel/test-http-connect-default-host-header.js b/test/parallel/test-http-connect-default-host-header.js new file mode 100644 index 00000000000000..9522f028f5165b --- /dev/null +++ b/test/parallel/test-http-connect-default-host-header.js @@ -0,0 +1,37 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); +const net = require('net'); + +const target = 'target.example.com:443'; + +const server = net.createServer(common.mustCall((socket) => { + socket.once('data', common.mustCall((data) => { + const rawRequest = data.toString(); + const requestLines = rawRequest.split('\r\n'); + + assert.strictEqual(requestLines[0], `CONNECT ${target} HTTP/1.1`); + assert(requestLines.includes(`Host: ${target}`)); + + socket.end('HTTP/1.1 200 Connection established\r\n\r\n'); + })); +})); + +server.listen(0, common.localhostIPv4, common.mustCall(() => { + const req = http.request({ + host: common.localhostIPv4, + port: server.address().port, + method: 'CONNECT', + path: target, + }, common.mustNotCall()); + + req.on('connect', common.mustCall((res, socket) => { + assert.strictEqual(res.statusCode, 200); + socket.destroy(); + server.close(); + })); + + req.end(); +})); From ffa487ece02232c230f8ce81a6359a8492436b1d Mon Sep 17 00:00:00 2001 From: Archkon <180910180+Archkon@users.noreply.github.com> Date: Fri, 26 Jun 2026 23:32:16 +0800 Subject: [PATCH 2/2] test: update CONNECT Host header expectation Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> --- test/parallel/test-tls-over-http-tunnel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-tls-over-http-tunnel.js b/test/parallel/test-tls-over-http-tunnel.js index baef7a56f6884a..e16400d06aece9 100644 --- a/test/parallel/test-tls-over-http-tunnel.js +++ b/test/parallel/test-tls-over-http-tunnel.js @@ -61,7 +61,7 @@ const proxy = net.createServer(common.mustCall((clientSocket) => { `CONNECT localhost:${server.address().port} ` + 'HTTP/1.1\r\n' + 'Proxy-Connections: keep-alive\r\n' + - `Host: localhost:${proxy.address().port}\r\n` + + `Host: localhost:${server.address().port}\r\n` + 'Connection: keep-alive\r\n\r\n'); console.log('PROXY: got CONNECT request');