Skip to content

Commit

Permalink
http: fix request when setHost is true
Browse files Browse the repository at this point in the history
Fixes: #19457

PR-URL: #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>
  • Loading branch information
XadillaX authored and targos committed Apr 4, 2018
1 parent 42671f2 commit e1f44a6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,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}

Expand Down
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,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;
Expand Down
30 changes: 29 additions & 1 deletion test/parallel/test-https-host-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -103,6 +103,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);
});
}

0 comments on commit e1f44a6

Please sign in to comment.