0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

http: fix request when setHost is true

Fixes: https://github.com/nodejs/node/issues/19457

PR-URL: https://github.com/nodejs/node/pull/19502
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
XadillaX 2018-03-21 12:11:40 +08:00 committed by Trivikram Kamat
parent 2ec6995555
commit b06f686f88
3 changed files with 32 additions and 2 deletions

View File

@ -1858,6 +1858,8 @@ changes:
details. Any [`Duplex`][] stream is a valid return value.
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
This will set the timeout before the socket is connected.
* `setHost` {boolean}: Specifies whether or not to automatically add the
`Host` header. Defaults to `true`.
* `callback` {Function}
* Returns: {http.ClientRequest}

View File

@ -116,7 +116,7 @@ function ClientRequest(options, cb) {
var host = options.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';
var setHost = (options.setHost === undefined);
var setHost = (options.setHost === undefined || Boolean(options.setHost));
this.socketPath = options.socketPath;
this.timeout = options.timeout;

View File

@ -16,7 +16,7 @@ const httpsServer = https.createServer(options, reqHandler);
function reqHandler(req, res) {
console.log(`Got request: ${req.headers.host} ${req.url}`);
if (req.url === '/setHostFalse5') {
if (req.url.startsWith('/setHostFalse')) {
assert.strictEqual(req.headers.host, undefined);
} else {
assert.strictEqual(
@ -97,6 +97,34 @@ function testHttps() {
setHost: false,
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower);
https.request({
method: 'GET',
path: `/${counter++}`,
host: 'localhost',
setHost: true,
//agent: false,
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower).end();
https.get({
method: 'GET',
path: `/setHostFalse${counter++}`,
host: 'localhost',
setHost: 0,
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower);
https.get({
method: 'GET',
path: `/setHostFalse${counter++}`,
host: 'localhost',
setHost: null,
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower);
});
}