-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: add type checking for hostname #12494
Conversation
lib/_http_client.js
Outdated
@@ -65,6 +65,14 @@ function isInvalidPath(s) { | |||
return false; | |||
} | |||
|
|||
function validateHost(host) { | |||
if (host !== undefined && host !== null && typeof host !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first two conditionals can be shortened to just host != null
or host != undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... that's fine. I'm not a fan of !=
for that but shorter is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be more efficient too, I have not checked how V8 compiles the two different methods of checking.
lib/_http_client.js
Outdated
function validateHost(host) { | ||
if (host !== undefined && host !== null && typeof host !== 'string') { | ||
throw new TypeError( | ||
'hostname must either be a string, undefined or null'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we might use: "hostname"/"host" must either be a string, undefined, or null
to keep with the theme of quoting property names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right... good catch :-)
@mscdex Updated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a question.
|
||
['', undefined, null].forEach((v) => { | ||
assert.doesNotThrow(() => { | ||
http.request({hostname: v}).on('error', common.noop); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the common.noop
here? I'd think we'd either want mustCall()
or mustNotCall()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main part that this is testing is that the call to http.request()
does not throw synchronously. The fact that it will emit the error
event is non-sequential to this particular test. This is set simply to avoid the unhandled error
exception.
lib/_http_client.js
Outdated
throw new TypeError( | ||
'hostname must either be a string, undefined or null'); | ||
'"hostname" must either be a string, undefined or null'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include "host" as well, since this same function is used for both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was stewing over that. Not sure. I'm fine with doing so just not sure if we should
lib/_http_client.js
Outdated
@@ -65,6 +65,15 @@ function isInvalidPath(s) { | |||
return false; | |||
} | |||
|
|||
function validateHost(host) { | |||
// eslint-disable-next-line eqeqeq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not wrong this can be removed if host != null
is used instead of host != undefined
.
97f9dc6
to
46c75f1
Compare
@nodejs/ctc ... PTAL |
|
||
// All of these values should cause http.request() to throw synchronously | ||
// when passed as the value of either options.hostname or options.host | ||
const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, new Date()]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe include 0
as well, since it would have been treated as a false-y value?
// when passed as the value of either options.hostname or options.host | ||
const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, new Date()]; | ||
|
||
function errCheck(name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is not doing any checks actually
// These values are OK and should not throw synchronously | ||
['', undefined, null].forEach((v) => { | ||
assert.doesNotThrow(() => { | ||
http.request({hostname: v}).on('error', common.noop); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not common.mustNotCall()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell's reply on a comment above for same question
The main part that this is testing is that the call to http.request() does not throw synchronously. The fact that it will emit the error event is non-sequential to this particular test. This is set simply to avoid the unhandled error exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm, makes sense. Cool then.
46c75f1
to
0b5dc3f
Compare
Add type checking for options.hostname / options.host Maintains the use of `undefined` and `null` for default `localhost`, but other falsy values (like `false`, `0` and `NaN`) are rejected.
0b5dc3f
to
9ec4133
Compare
Linting error was from an unrelated commit |
Add type checking for options.hostname / options.host Maintains the use of `undefined` and `null` for default `localhost`, but other falsy values (like `false`, `0` and `NaN`) are rejected. PR-URL: #12494 Ref: #12488 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]>
Landed in 85a4e25 |
This test would currently create HTTP requests to localhost:80 and would time out on machines that actually had an server listening there. To address that, `end()` the requests that are generated. PR-URL: nodejs#12627 Ref: nodejs#12494
This test would currently create HTTP requests to localhost:80 and would time out on machines that actually had an server listening there. To address that, `end()` the requests that are generated. PR-URL: #12627 Ref: #12494 Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
Add type checking for options.hostname / options.host Maintains the use of
undefined
andnull
for defaultlocalhost
, but other falsy values (likefalse
,0
andNaN
) are rejected.Refs: #12488
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
http