url: do not truncate long hostnames
Currently, around line 417 lib/url.js is truncating hostname and put the rest of hostname to the path if hostname length after `.` is equal or more than 63. This behavior is different from browser behavior. I changed the code so that it doesn’t truncate. I also added the test example which has more than 63 length in after `.` in hostname in test url. PR-URL: https://github.com/nodejs/node/pull/9292 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
parent
c732bd1d93
commit
c65d55f087
39
lib/url.js
39
lib/url.js
@ -408,33 +408,22 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function validateHostname(self, rest, hostname) {
|
function validateHostname(self, rest, hostname) {
|
||||||
for (var i = 0, lastPos; i <= hostname.length; ++i) {
|
for (var i = 0; i < hostname.length; ++i) {
|
||||||
var code;
|
const code = hostname.charCodeAt(i);
|
||||||
if (i < hostname.length)
|
const isValid = (code >= 97/*a*/ && code <= 122/*z*/) ||
|
||||||
code = hostname.charCodeAt(i);
|
code === 46/*.*/ ||
|
||||||
if (code === 46/*.*/ || i === hostname.length) {
|
(code >= 65/*A*/ && code <= 90/*Z*/) ||
|
||||||
if (i - lastPos > 0) {
|
(code >= 48/*0*/ && code <= 57/*9*/) ||
|
||||||
if (i - lastPos > 63) {
|
code === 45/*-*/ ||
|
||||||
self.hostname = hostname.slice(0, lastPos + 63);
|
code === 43/*+*/ ||
|
||||||
return '/' + hostname.slice(lastPos + 63) + rest;
|
code === 95/*_*/ ||
|
||||||
}
|
code > 127;
|
||||||
}
|
|
||||||
lastPos = i + 1;
|
|
||||||
continue;
|
|
||||||
} else if ((code >= 48/*0*/ && code <= 57/*9*/) ||
|
|
||||||
(code >= 97/*a*/ && code <= 122/*z*/) ||
|
|
||||||
code === 45/*-*/ ||
|
|
||||||
(code >= 65/*A*/ && code <= 90/*Z*/) ||
|
|
||||||
code === 43/*+*/ ||
|
|
||||||
code === 95/*_*/ ||
|
|
||||||
code > 127) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Invalid host character
|
// Invalid host character
|
||||||
self.hostname = hostname.slice(0, i);
|
if (!isValid) {
|
||||||
if (i < hostname.length)
|
self.hostname = hostname.slice(0, i);
|
||||||
return '/' + hostname.slice(i) + rest;
|
return '/' + hostname.slice(i) + rest;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1225,6 +1225,17 @@ var formatTests = {
|
|||||||
path: '/node'
|
path: '/node'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// greater than or equal to 63 characters after `.` in hostname
|
||||||
|
[`http://www.${'z'.repeat(63)}example.com/node`]: {
|
||||||
|
href: `http://www.${'z'.repeat(63)}example.com/node`,
|
||||||
|
protocol: 'http:',
|
||||||
|
slashes: true,
|
||||||
|
host: `www.${'z'.repeat(63)}example.com`,
|
||||||
|
hostname: `www.${'z'.repeat(63)}example.com`,
|
||||||
|
pathname: '/node',
|
||||||
|
path: '/node'
|
||||||
|
},
|
||||||
|
|
||||||
// https://github.com/nodejs/node/issues/3361
|
// https://github.com/nodejs/node/issues/3361
|
||||||
'file:///home/user': {
|
'file:///home/user': {
|
||||||
href: 'file:///home/user',
|
href: 'file:///home/user',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user