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 1 commit
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: 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 || options.setHost);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just Boolean(options.setHost)?

Also this is technically a breaking (semver) change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about !!options.setHost?

Copy link
Member

Choose a reason for hiding this comment

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

I prefer Boolean since it's more explicit (we are converting this to a boolean) rather than "we are negating this, which is coercing it to a boolean, and then negating it again to get the boolean value".

Copy link
Member

@lpinca lpinca Mar 21, 2018

Choose a reason for hiding this comment

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

Using only Boolean(options.setHost) is not ok as undefined should set setHost to true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't it a bugfix but breaking?

Copy link
Member

Choose a reason for hiding this comment

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

Technically yes but Imho it's semver-patch as I doubt anyone is relying on the current behavior. The option is currently not documented.


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 @@ -104,5 +104,33 @@ function testHttps() {
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower).end();

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).end();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: .end() is automatically called when using https.get().


https.get({
method: 'GET',
path: `/setHostFalse${counter++}`,
host: 'localhost',
setHost: null,
port: this.address().port,
rejectUnauthorized: false
}, cb).on('error', thrower).end();
});
}