Skip to content

Commit eacfbd0

Browse files
Archkonrichardlau
authored andcommitted
http: add CONNECT method handling for default Host header with proxy
Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> PR-URL: #64114 Fixes: #63945 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent f49704b commit eacfbd0

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

lib/_http_client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ function ClientRequest(input, options, cb) {
526526
const userHostHeader = this.getHeader('host');
527527

528528
if (host && !this.getHeader('host') && setHost) {
529-
this.setHeader('Host', hostHeaderFromOptions);
529+
const hostHeader = method === 'CONNECT' && options.path ?
530+
String(this.path) : hostHeaderFromOptions;
531+
this.setHeader('Host', hostHeader);
530532
}
531533

532534
if (options.auth && !this.getHeader('Authorization')) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
const net = require('net');
7+
8+
const target = 'target.example.com:443';
9+
10+
const server = net.createServer(common.mustCall((socket) => {
11+
socket.once('data', common.mustCall((data) => {
12+
const rawRequest = data.toString();
13+
const requestLines = rawRequest.split('\r\n');
14+
15+
assert.strictEqual(requestLines[0], `CONNECT ${target} HTTP/1.1`);
16+
assert(requestLines.includes(`Host: ${target}`));
17+
18+
socket.end('HTTP/1.1 200 Connection established\r\n\r\n');
19+
}));
20+
}));
21+
22+
server.listen(0, common.localhostIPv4, common.mustCall(() => {
23+
const req = http.request({
24+
host: common.localhostIPv4,
25+
port: server.address().port,
26+
method: 'CONNECT',
27+
path: target,
28+
}, common.mustNotCall());
29+
30+
req.on('connect', common.mustCall((res, socket) => {
31+
assert.strictEqual(res.statusCode, 200);
32+
socket.destroy();
33+
server.close();
34+
}));
35+
36+
req.end();
37+
}));

test/parallel/test-tls-over-http-tunnel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const proxy = net.createServer(common.mustCall((clientSocket) => {
6161
`CONNECT localhost:${server.address().port} ` +
6262
'HTTP/1.1\r\n' +
6363
'Proxy-Connections: keep-alive\r\n' +
64-
`Host: localhost:${proxy.address().port}\r\n` +
64+
`Host: localhost:${server.address().port}\r\n` +
6565
'Connection: keep-alive\r\n\r\n');
6666

6767
console.log('PROXY: got CONNECT request');

0 commit comments

Comments
 (0)