Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: fix request when setHost is true #19502

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add the document.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: It would be great if you could change varlet / const while you are at this line.

Copy link
Contributor Author

@XadillaX XadillaX Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should open another PR to fix all vars in this file but not in this PR.


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);
});
}