Skip to content

Commit

Permalink
url: refactor validateHostname
Browse files Browse the repository at this point in the history
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: #26809
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR committed Mar 27, 2019
1 parent ce26590 commit bbfa93a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) ||
Expand All @@ -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.
Expand Down

0 comments on commit bbfa93a

Please sign in to comment.