From bbfa93af3d1577fbf96622c9100c07d45ff8998a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 20 Mar 2019 12:24:35 +0100 Subject: [PATCH] url: refactor validateHostname This function did not only validate the input but it returned a new value in case the hostname was valid. This simplifies the function by always returning the required value, no matter if it is valid or invalid, so the callee site does not have to check that anymore. On top the function is renamed to `getHostname` to make clear that the function does not only validate the input but it also returns a new value. PR-URL: https://github.com/nodejs/node/pull/26809 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/url.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/url.js b/lib/url.js index ade0007171ee48..f50d180c7a1585 100644 --- a/lib/url.js +++ b/lib/url.js @@ -359,9 +359,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { // validate a little. if (!ipv6Hostname) { - const result = validateHostname(this, rest, hostname); - if (result !== undefined) - rest = result; + rest = getHostname(this, rest, hostname); } if (this.hostname.length > hostnameMaxLen) { @@ -462,7 +460,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { return this; }; -function validateHostname(self, rest, hostname) { +function getHostname(self, rest, hostname) { for (var i = 0; i < hostname.length; ++i) { const code = hostname.charCodeAt(i); const isValid = (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) || @@ -477,9 +475,10 @@ function validateHostname(self, rest, hostname) { // Invalid host character if (!isValid) { self.hostname = hostname.slice(0, i); - return '/' + hostname.slice(i) + rest; + return `/${hostname.slice(i)}${rest}`; } } + return rest; } // Escaped characters. Use empty strings to fill up unused entries.