Skip to content

Commit

Permalink
url: introduce URL_FLAGS_IS_DEFAULT_SCHEME_PORT flag
Browse files Browse the repository at this point in the history
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: #20479
Fixes: #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>
  • Loading branch information
AyushG3112 authored and MylesBorins committed May 22, 2018
1 parent ed5f253 commit 526163c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
URL_FLAGS_HAS_PATH,
URL_FLAGS_HAS_QUERY,
URL_FLAGS_HAS_USERNAME,
URL_FLAGS_IS_DEFAULT_SCHEME_PORT,
URL_FLAGS_SPECIAL,
kFragment,
kHost,
Expand Down Expand Up @@ -276,7 +277,7 @@ function onParsePortComplete(flags, protocol, username, password,
function onParseHostComplete(flags, protocol, username, password,
host, port, path, query, fragment) {
onParseHostnameComplete.apply(this, arguments);
if (port !== null)
if (port !== null || ((flags & URL_FLAGS_IS_DEFAULT_SCHEME_PORT) !== 0))
onParsePortComplete.apply(this, arguments);
}

Expand Down
2 changes: 2 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,8 @@ void URL::Parse(const char* input,
}
// the port is valid
url->port = NormalizePort(url->scheme, static_cast<int>(port));
if (url->port == -1)
url->flags |= URL_FLAGS_IS_DEFAULT_SCHEME_PORT;
buffer.clear();
} else if (has_state_override) {
// TODO(TimothyGu): Similar case as above.
Expand Down
3 changes: 2 additions & 1 deletion src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ using v8::Value;
XX(URL_FLAGS_HAS_HOST, 0x80) \
XX(URL_FLAGS_HAS_PATH, 0x100) \
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 {
kUnknownState = -1,
Expand Down
13 changes: 12 additions & 1 deletion test/fixtures/url-setter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* The following tests are copied from WPT. Modifications to them should be
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
*/
module.exports =
Expand Down Expand Up @@ -727,6 +727,17 @@ module.exports =
"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",
"href": "http://example.net/path",
Expand Down

0 comments on commit 526163c

Please sign in to comment.