url: introduce URL_FLAGS_IS_DEFAULT_SCHEME_PORT flag

Introduce `URL_FLAGS_IS_DEFAULT_SCHEME_PORT` flag which is retured
when the parser detects that the port passed is the default port
for that scheme.

PR-URL: https://github.com/nodejs/node/pull/20479
Fixes: https://github.com/nodejs/node/issues/20465
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Ayush Gupta 2018-05-03 00:21:20 +05:30 committed by Ruben Bridgewater
parent 9b24be1340
commit 11892b0b64
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
4 changed files with 18 additions and 3 deletions

View File

@ -46,6 +46,7 @@ const {
URL_FLAGS_HAS_PATH, URL_FLAGS_HAS_PATH,
URL_FLAGS_HAS_QUERY, URL_FLAGS_HAS_QUERY,
URL_FLAGS_HAS_USERNAME, URL_FLAGS_HAS_USERNAME,
URL_FLAGS_IS_DEFAULT_SCHEME_PORT,
URL_FLAGS_SPECIAL, URL_FLAGS_SPECIAL,
kFragment, kFragment,
kHost, kHost,
@ -276,7 +277,7 @@ function onParsePortComplete(flags, protocol, username, password,
function onParseHostComplete(flags, protocol, username, password, function onParseHostComplete(flags, protocol, username, password,
host, port, path, query, fragment) { host, port, path, query, fragment) {
onParseHostnameComplete.apply(this, arguments); onParseHostnameComplete.apply(this, arguments);
if (port !== null) if (port !== null || ((flags & URL_FLAGS_IS_DEFAULT_SCHEME_PORT) !== 0))
onParsePortComplete.apply(this, arguments); onParsePortComplete.apply(this, arguments);
} }

View File

@ -1749,6 +1749,8 @@ void URL::Parse(const char* input,
} }
// the port is valid // the port is valid
url->port = NormalizePort(url->scheme, static_cast<int>(port)); url->port = NormalizePort(url->scheme, static_cast<int>(port));
if (url->port == -1)
url->flags |= URL_FLAGS_IS_DEFAULT_SCHEME_PORT;
buffer.clear(); buffer.clear();
} else if (has_state_override) { } else if (has_state_override) {
// TODO(TimothyGu): Similar case as above. // TODO(TimothyGu): Similar case as above.

View File

@ -50,7 +50,8 @@ using v8::Value;
XX(URL_FLAGS_HAS_HOST, 0x80) \ XX(URL_FLAGS_HAS_HOST, 0x80) \
XX(URL_FLAGS_HAS_PATH, 0x100) \ XX(URL_FLAGS_HAS_PATH, 0x100) \
XX(URL_FLAGS_HAS_QUERY, 0x200) \ XX(URL_FLAGS_HAS_QUERY, 0x200) \
XX(URL_FLAGS_HAS_FRAGMENT, 0x400) XX(URL_FLAGS_HAS_FRAGMENT, 0x400) \
XX(URL_FLAGS_IS_DEFAULT_SCHEME_PORT, 0x800) \
enum url_parse_state { enum url_parse_state {
kUnknownState = -1, kUnknownState = -1,

View File

@ -2,7 +2,7 @@
/* The following tests are copied from WPT. Modifications to them should be /* The following tests are copied from WPT. Modifications to them should be
upstreamed first. Refs: upstreamed first. Refs:
https://github.com/w3c/web-platform-tests/blob/ed4bb727ed/url/setters_tests.json https://github.com/w3c/web-platform-tests/blob/f0fe479/url/setters_tests.json
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/ */
module.exports = module.exports =
@ -727,6 +727,17 @@ module.exports =
"port": "80" "port": "80"
} }
}, },
{
"comment": "Port number is removed if new port is scheme default and existing URL has a non-default port",
"href": "http://example.net:8080",
"new_value": "example.com:80",
"expected": {
"href": "http://example.com/",
"host": "example.com",
"hostname": "example.com",
"port": ""
}
},
{ {
"comment": "Stuff after a / delimiter is ignored", "comment": "Stuff after a / delimiter is ignored",
"href": "http://example.net/path", "href": "http://example.net/path",