Skip to content

Commit

Permalink
url: fix windows drive letter handling
Browse files Browse the repository at this point in the history
Address issue with Windows drive letter handling that was
causing es-module test suite to fail.

PR-URL: nodejs/node#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>
  • Loading branch information
Benjamin Coe authored and addaleax committed Sep 30, 2017
1 parent 0a60621 commit f8e15b4
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,19 @@ static inline bool IsSpecial(std::string scheme) {
return false;
}

// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
static inline bool StartsWithWindowsDriveLetter(const char* p,
const char* end) {
const size_t length = end - p;
return length >= 2 &&
IsWindowsDriveLetter(p[0], p[1]) &&
(length == 2 ||
p[2] == '/' ||
p[2] == '\\' ||
p[2] == '?' ||
p[2] == '#');
}

static inline int NormalizePort(std::string scheme, int p) {
#define XX(name, port) if (scheme == name && p == port) return -1;
SPECIALS(XX);
Expand Down Expand Up @@ -1198,6 +1211,7 @@ void URL::Parse(const char* input,
bool special = (url->flags & URL_FLAGS_SPECIAL);
bool cannot_be_base;
const bool special_back_slash = (special && ch == '\\');

switch (state) {
case kSchemeStart:
if (IsASCIIAlpha(ch)) {
Expand Down Expand Up @@ -1667,13 +1681,7 @@ void URL::Parse(const char* input,
state = kFragment;
break;
default:
if ((remaining == 0 ||
!IsWindowsDriveLetter(ch, p[1]) ||
(remaining >= 2 &&
p[2] != '/' &&
p[2] != '\\' &&
p[2] != '?' &&
p[2] != '#'))) {
if (!StartsWithWindowsDriveLetter(p, end)) {
if (base->flags & URL_FLAGS_HAS_HOST) {
url->flags |= URL_FLAGS_HAS_HOST;
url->host = base->host;
Expand All @@ -1697,7 +1705,8 @@ void URL::Parse(const char* input,
state = kFileHost;
} else {
if (has_base &&
base->scheme == "file:") {
base->scheme == "file:" &&
!StartsWithWindowsDriveLetter(p, end)) {
if (IsNormalizedWindowsDriveLetter(base->path[0])) {
url->flags |= URL_FLAGS_HAS_PATH;
url->path.push_back(base->path[0]);
Expand Down
Loading

0 comments on commit f8e15b4

Please sign in to comment.