Skip to content

Commit 6c221b8

Browse files
Benjamin Coejasnell
authored andcommitted
url: fix windows drive letter handling
Address issue with Windows drive letter handling that was causing es-module test suite to fail. PR-URL: #15490 Ref: whatwg/url#343 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 409576e commit 6c221b8

File tree

3 files changed

+389
-10
lines changed

3 files changed

+389
-10
lines changed

src/node_url.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,19 @@ static inline bool IsSpecial(std::string scheme) {
552552
return false;
553553
}
554554

555+
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
556+
static inline bool StartsWithWindowsDriveLetter(const char* p,
557+
const char* end) {
558+
const size_t length = end - p;
559+
return length >= 2 &&
560+
IsWindowsDriveLetter(p[0], p[1]) &&
561+
(length == 2 ||
562+
p[2] == '/' ||
563+
p[2] == '\\' ||
564+
p[2] == '?' ||
565+
p[2] == '#');
566+
}
567+
555568
static inline int NormalizePort(std::string scheme, int p) {
556569
#define XX(name, port) if (scheme == name && p == port) return -1;
557570
SPECIALS(XX);
@@ -1198,6 +1211,7 @@ void URL::Parse(const char* input,
11981211
bool special = (url->flags & URL_FLAGS_SPECIAL);
11991212
bool cannot_be_base;
12001213
const bool special_back_slash = (special && ch == '\\');
1214+
12011215
switch (state) {
12021216
case kSchemeStart:
12031217
if (IsASCIIAlpha(ch)) {
@@ -1667,13 +1681,7 @@ void URL::Parse(const char* input,
16671681
state = kFragment;
16681682
break;
16691683
default:
1670-
if ((remaining == 0 ||
1671-
!IsWindowsDriveLetter(ch, p[1]) ||
1672-
(remaining >= 2 &&
1673-
p[2] != '/' &&
1674-
p[2] != '\\' &&
1675-
p[2] != '?' &&
1676-
p[2] != '#'))) {
1684+
if (!StartsWithWindowsDriveLetter(p, end)) {
16771685
if (base->flags & URL_FLAGS_HAS_HOST) {
16781686
url->flags |= URL_FLAGS_HAS_HOST;
16791687
url->host = base->host;
@@ -1697,7 +1705,8 @@ void URL::Parse(const char* input,
16971705
state = kFileHost;
16981706
} else {
16991707
if (has_base &&
1700-
base->scheme == "file:") {
1708+
base->scheme == "file:" &&
1709+
!StartsWithWindowsDriveLetter(p, end)) {
17011710
if (IsNormalizedWindowsDriveLetter(base->path[0])) {
17021711
url->flags |= URL_FLAGS_HAS_PATH;
17031712
url->path.push_back(base->path[0]);

0 commit comments

Comments
 (0)