Skip to content

Commit e1f44a6

Browse files
XadillaXtargos
authored andcommitted
http: fix request when setHost is true
Fixes: #19457 PR-URL: #19502 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 42671f2 commit e1f44a6

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/api/http.md

+2
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@ changes:
18091809
details. Any [`Duplex`][] stream is a valid return value.
18101810
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
18111811
This will set the timeout before the socket is connected.
1812+
* `setHost` {boolean}: Specifies whether or not to automatically add the
1813+
`Host` header. Defaults to `true`.
18121814
* `callback` {Function}
18131815
* Returns: {http.ClientRequest}
18141816

lib/_http_client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function ClientRequest(options, cb) {
139139
var host = options.host = validateHost(options.hostname, 'hostname') ||
140140
validateHost(options.host, 'host') || 'localhost';
141141

142-
var setHost = (options.setHost === undefined);
142+
var setHost = (options.setHost === undefined || Boolean(options.setHost));
143143

144144
this.socketPath = options.socketPath;
145145
this.timeout = options.timeout;

test/parallel/test-https-host-headers.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const httpsServer = https.createServer(options, reqHandler);
1616

1717
function reqHandler(req, res) {
1818
console.log(`Got request: ${req.headers.host} ${req.url}`);
19-
if (req.url === '/setHostFalse5') {
19+
if (req.url.startsWith('/setHostFalse')) {
2020
assert.strictEqual(req.headers.host, undefined);
2121
} else {
2222
assert.strictEqual(
@@ -103,6 +103,34 @@ function testHttps() {
103103
setHost: false,
104104
port: this.address().port,
105105
rejectUnauthorized: false
106+
}, cb).on('error', thrower);
107+
108+
https.request({
109+
method: 'GET',
110+
path: `/${counter++}`,
111+
host: 'localhost',
112+
setHost: true,
113+
//agent: false,
114+
port: this.address().port,
115+
rejectUnauthorized: false
106116
}, cb).on('error', thrower).end();
117+
118+
https.get({
119+
method: 'GET',
120+
path: `/setHostFalse${counter++}`,
121+
host: 'localhost',
122+
setHost: 0,
123+
port: this.address().port,
124+
rejectUnauthorized: false
125+
}, cb).on('error', thrower);
126+
127+
https.get({
128+
method: 'GET',
129+
path: `/setHostFalse${counter++}`,
130+
host: 'localhost',
131+
setHost: null,
132+
port: this.address().port,
133+
rejectUnauthorized: false
134+
}, cb).on('error', thrower);
107135
});
108136
}

0 commit comments

Comments
 (0)