Skip to content

Commit bbfa93a

Browse files
committed
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: #26809 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ce26590 commit bbfa93a

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

lib/url.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
359359

360360
// validate a little.
361361
if (!ipv6Hostname) {
362-
const result = validateHostname(this, rest, hostname);
363-
if (result !== undefined)
364-
rest = result;
362+
rest = getHostname(this, rest, hostname);
365363
}
366364

367365
if (this.hostname.length > hostnameMaxLen) {
@@ -462,7 +460,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
462460
return this;
463461
};
464462

465-
function validateHostname(self, rest, hostname) {
463+
function getHostname(self, rest, hostname) {
466464
for (var i = 0; i < hostname.length; ++i) {
467465
const code = hostname.charCodeAt(i);
468466
const isValid = (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) ||
@@ -477,9 +475,10 @@ function validateHostname(self, rest, hostname) {
477475
// Invalid host character
478476
if (!isValid) {
479477
self.hostname = hostname.slice(0, i);
480-
return '/' + hostname.slice(i) + rest;
478+
return `/${hostname.slice(i)}${rest}`;
481479
}
482480
}
481+
return rest;
483482
}
484483

485484
// Escaped characters. Use empty strings to fill up unused entries.

0 commit comments

Comments
 (0)