Skip to content

Commit 526163c

Browse files
AyushG3112MylesBorins
authored andcommitted
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: #20479 Fixes: #20465 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ed5f253 commit 526163c

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

lib/internal/url.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
URL_FLAGS_HAS_PATH,
4747
URL_FLAGS_HAS_QUERY,
4848
URL_FLAGS_HAS_USERNAME,
49+
URL_FLAGS_IS_DEFAULT_SCHEME_PORT,
4950
URL_FLAGS_SPECIAL,
5051
kFragment,
5152
kHost,
@@ -276,7 +277,7 @@ function onParsePortComplete(flags, protocol, username, password,
276277
function onParseHostComplete(flags, protocol, username, password,
277278
host, port, path, query, fragment) {
278279
onParseHostnameComplete.apply(this, arguments);
279-
if (port !== null)
280+
if (port !== null || ((flags & URL_FLAGS_IS_DEFAULT_SCHEME_PORT) !== 0))
280281
onParsePortComplete.apply(this, arguments);
281282
}
282283

src/node_url.cc

+2
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,8 @@ void URL::Parse(const char* input,
17491749
}
17501750
// the port is valid
17511751
url->port = NormalizePort(url->scheme, static_cast<int>(port));
1752+
if (url->port == -1)
1753+
url->flags |= URL_FLAGS_IS_DEFAULT_SCHEME_PORT;
17521754
buffer.clear();
17531755
} else if (has_state_override) {
17541756
// TODO(TimothyGu): Similar case as above.

src/node_url.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ using v8::Value;
5050
XX(URL_FLAGS_HAS_HOST, 0x80) \
5151
XX(URL_FLAGS_HAS_PATH, 0x100) \
5252
XX(URL_FLAGS_HAS_QUERY, 0x200) \
53-
XX(URL_FLAGS_HAS_FRAGMENT, 0x400)
53+
XX(URL_FLAGS_HAS_FRAGMENT, 0x400) \
54+
XX(URL_FLAGS_IS_DEFAULT_SCHEME_PORT, 0x800) \
5455

5556
enum url_parse_state {
5657
kUnknownState = -1,

test/fixtures/url-setter-tests.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* The following tests are copied from WPT. Modifications to them should be
44
upstreamed first. Refs:
5-
https://github.com/w3c/web-platform-tests/blob/ed4bb727ed/url/setters_tests.json
5+
https://github.com/w3c/web-platform-tests/blob/f0fe479/url/setters_tests.json
66
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
77
*/
88
module.exports =
@@ -727,6 +727,17 @@ module.exports =
727727
"port": "80"
728728
}
729729
},
730+
{
731+
"comment": "Port number is removed if new port is scheme default and existing URL has a non-default port",
732+
"href": "http://example.net:8080",
733+
"new_value": "example.com:80",
734+
"expected": {
735+
"href": "http://example.com/",
736+
"host": "example.com",
737+
"hostname": "example.com",
738+
"port": ""
739+
}
740+
},
730741
{
731742
"comment": "Stuff after a / delimiter is ignored",
732743
"href": "http://example.net/path",

0 commit comments

Comments
 (0)