From d816988c8ec5d0c3988c368af50a37006ed35150 Mon Sep 17 00:00:00 2001 From: Rajaram Gaunker Date: Sat, 13 May 2017 18:08:48 -0700 Subject: [PATCH] url: fast path ascii domains, do not run ToASCII To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: https://github.com/nodejs/node/issues/12965 Refs: https://github.com/nodejs/node/pull/12966 Refs: https://github.com/whatwg/url/pull/309 --- src/node_url.cc | 6 ++++- test/fixtures/url-domains-with-hyphens.js | 27 +++++++++++++++++++++++ test/parallel/test-whatwg-url-domainto.js | 15 +++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/url-domains-with-hyphens.js diff --git a/src/node_url.cc b/src/node_url.cc index ed9b7dd89f26c4..3353ba7d899046 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -133,6 +133,8 @@ enum url_error_cb_args { // https://infra.spec.whatwg.org/#ascii-code-point CHAR_TEST(8, IsASCIICodePoint, (ch >= '\0' && ch <= '\x7f')) +CHAR_TEST(8, IsLowerCaseASCII, (ch >='a' && ch <= 'z')) + // https://infra.spec.whatwg.org/#ascii-tab-or-newline CHAR_TEST(8, IsASCIITabOrNewline, (ch == '\t' || ch == '\n' || ch == '\r')) @@ -871,7 +873,9 @@ static url_host_type ParseHost(url_host* host, if (IsAllASCII(decoded)) { // Lowercase ASCII domains for (size_t n = 0; n < decoded.size(); n++) { - decoded[n] = ASCIILowercase(decoded[n]); + if (!IsLowerCaseASCII(decoded[n])) { + decoded[n] = ASCIILowercase(decoded[n]); + } } } else { // Then we have to Unicode IDNA toASCII diff --git a/test/fixtures/url-domains-with-hyphens.js b/test/fixtures/url-domains-with-hyphens.js new file mode 100644 index 00000000000000..5bd1136d1e6809 --- /dev/null +++ b/test/fixtures/url-domains-with-hyphens.js @@ -0,0 +1,27 @@ +'use strict'; + +module.exports = { + valid: [ + // URLs with hyphen + { + ascii: 'r4---sn-a5mlrn7s.gevideo.com', + unicode: 'r4---sn-a5mlrn7s.gevideo.com' + }, + { + ascii: '-sn-a5mlrn7s.gevideo.com', + unicode: '-sn-a5mlrn7s.gevideo.com' + }, + { + ascii: 'sn-a5mlrn7s-.gevideo.com', + unicode: 'sn-a5mlrn7s-.gevideo.com' + }, + { + ascii: '-sn-a5mlrn7s-.gevideo.com', + unicode: '-sn-a5mlrn7s-.gevideo.com' + }, + { + ascii: '-sn--a5mlrn7s-.gevideo.com', + unicode: '-sn--a5mlrn7s-.gevideo.com' + } + ] +} diff --git a/test/parallel/test-whatwg-url-domainto.js b/test/parallel/test-whatwg-url-domainto.js index 9811e3c9975f7a..4e1bee2ab55ed8 100644 --- a/test/parallel/test-whatwg-url-domainto.js +++ b/test/parallel/test-whatwg-url-domainto.js @@ -11,6 +11,7 @@ const { domainToASCII, domainToUnicode } = require('url'); // Tests below are not from WPT. const tests = require('../fixtures/url-idna.js'); +const testsHyphenDomains = require('../fixtures/url-domains-with-hyphens.js'); { const expectedError = common.expectsError( @@ -35,16 +36,10 @@ const tests = require('../fixtures/url-idna.js'); } { - [ - 'r4---sn-a5mlrn7s.gevideo.com', - '-sn-a5mlrn7s.gevideo.com', - 'sn-a5mlrn7s-.gevideo.com', - '-sn-a5mlrn7s-.gevideo.com', - '-sn--a5mlrn7s-.gevideo.com' - ].forEach((domain) => { - assert.strictEqual(domain, domainToASCII(domain), - `domainToASCII(${domain})`); - }) + for (const [i, { ascii, unicode }] of testsHyphenDomains.valid.entries()) { + assert.strictEqual(ascii, domainToASCII(unicode), + `domainToASCII(${i + 1})`); + } } {