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 <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-20 12:24:35 +01:00
parent ce265908eb
commit bbfa93af3d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -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.