Skip to content

Commit

Permalink
url: fast path ascii domains, do not run ToASCII
Browse files Browse the repository at this point in the history
To match browser behavior fast path ascii only domains and
do not run ToASCII on them.

Fixes: nodejs#12965
Refs: nodejs#12966
Refs: whatwg/url#309
  • Loading branch information
zimbabao committed May 15, 2017
1 parent 8233c34 commit 35e901d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ enum url_error_cb_args {
return str.length() >= 2 && name(str[0], str[1]); \
}

// https://infra.spec.whatwg.org/#ascii-code-point
CHAR_TEST(8, IsASCIICodePoint, (ch >= '\0' && ch <= '\x7f'))

// https://infra.spec.whatwg.org/#ascii-tab-or-newline
CHAR_TEST(8, IsASCIITabOrNewline, (ch == '\t' || ch == '\n' || ch == '\r'))

Expand Down Expand Up @@ -829,10 +832,10 @@ static url_host_type ParseOpaqueHost(url_host* host,
return type;
}

static inline bool IsAllASCII(std::string* input) {
for (size_t n = 0; n < input->size(); n++) {
const char ch = (*input)[n];
if (ch & 0x80) {
static inline bool IsAllASCII(const std::string& input) {
for (size_t n = 0; n < input.size(); n++) {
const char ch = input[n];
if (!IsASCIICodePoint(ch)) {
return false;
}
}
Expand Down Expand Up @@ -865,13 +868,13 @@ static url_host_type ParseHost(url_host* host,

// Match browser behavior for ASCII only domains
// and do not run them through ToASCII algorithm.
if (IsAllASCII(&decoded)) {
// Lowercase aschii domains
if (IsAllASCII(decoded)) {
// Lowercase ASCII domains
for (size_t n = 0; n < decoded.size(); n++) {
decoded[n] = std::tolower(decoded[n]);
decoded[n] = ASCIILowercase(decoded[n]);
}
} else {
// Then we have to punycode toASCII
// Then we have to Unicode IDNA toASCII
if (!ToASCII(&decoded, &decoded))
goto end;
}
Expand Down

0 comments on commit 35e901d

Please sign in to comment.