ip: improve parsing.
This commit is contained in:
parent
3e3e4c84ba
commit
37128240b3
@ -30,38 +30,61 @@ var ipv6Regex =
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
IP.parseHost = function parseHost(addr, fallback) {
|
IP.parseHost = function parseHost(addr, fallback) {
|
||||||
var port = fallback || 0;
|
var parts, host, port, version;
|
||||||
var parts, host, version;
|
|
||||||
|
|
||||||
assert(typeof addr === 'string');
|
assert(typeof addr === 'string');
|
||||||
assert(typeof port === 'number');
|
|
||||||
|
|
||||||
assert(addr.length > 0, 'Bad address.');
|
assert(addr.length > 0, 'Bad address.');
|
||||||
|
|
||||||
// [ipv6]:port
|
|
||||||
if (addr[0] === '[') {
|
if (addr[0] === '[') {
|
||||||
addr = addr.substring(1);
|
if (addr[addr.length - 1] === ']') {
|
||||||
parts = addr.split(/\]:?/);
|
// Case:
|
||||||
assert(parts.length === 2, 'Bad IPv6 address.');
|
// [::1]
|
||||||
|
host = addr.slice(1, -1);
|
||||||
|
port = null;
|
||||||
|
} else {
|
||||||
|
// Case:
|
||||||
|
// [::1]:80
|
||||||
|
addr = addr.slice(1);
|
||||||
|
parts = addr.split(']:');
|
||||||
|
assert(parts.length === 2, 'Bad IPv6 address.');
|
||||||
|
host = parts[0];
|
||||||
|
port = parts[1];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
parts = addr.split(':');
|
parts = addr.split(':');
|
||||||
if (parts.length > 2) {
|
switch (parts.length) {
|
||||||
// ipv6 - no port
|
case 2:
|
||||||
assert(IP.isV6Format(addr), 'Bad IPv6 address.');
|
// Cases:
|
||||||
parts = [addr];
|
// 127.0.0.1:80
|
||||||
} else {
|
// localhost:80
|
||||||
// domain/ipv4:port?
|
host = parts[0];
|
||||||
assert(parts.length <= 2, 'Bad host.');
|
port = parts[1];
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// Cases:
|
||||||
|
// 127.0.0.1
|
||||||
|
// localhost
|
||||||
|
host = parts[0];
|
||||||
|
port = null;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Case:
|
||||||
|
// ::1
|
||||||
|
assert(IP.isV6Format(addr), 'Bad IPv6 address.');
|
||||||
|
host = addr;
|
||||||
|
port = null;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
host = parts[0];
|
|
||||||
assert(host.length > 0, 'Bad host.');
|
assert(host.length > 0, 'Bad host.');
|
||||||
|
|
||||||
if (parts.length === 2) {
|
if (port != null) {
|
||||||
port = parts[1];
|
|
||||||
assert(/^\d+$/.test(port), 'Bad port.');
|
assert(/^\d+$/.test(port), 'Bad port.');
|
||||||
port = parseInt(port, 10);
|
port = parseInt(port, 10);
|
||||||
|
} else {
|
||||||
|
port = fallback || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
version = IP.version(host);
|
version = IP.version(host);
|
||||||
@ -90,6 +113,9 @@ IP.hostname = function hostname(host, port) {
|
|||||||
|
|
||||||
version = IP.version(host);
|
version = IP.version(host);
|
||||||
|
|
||||||
|
if (host.indexOf(':') !== -1)
|
||||||
|
assert(version === 6, 'Bad host.');
|
||||||
|
|
||||||
if (version !== -1)
|
if (version !== -1)
|
||||||
host = IP.normalize(host);
|
host = IP.normalize(host);
|
||||||
|
|
||||||
@ -126,6 +152,10 @@ IP.version = function version(str) {
|
|||||||
IP.isV4Format = function(str) {
|
IP.isV4Format = function(str) {
|
||||||
if (str.length < 7)
|
if (str.length < 7)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (str.length > 15)
|
||||||
|
return false;
|
||||||
|
|
||||||
return ipv4Regex.test(str);
|
return ipv4Regex.test(str);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -136,6 +166,12 @@ IP.isV4Format = function(str) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
IP.isV6Format = function(str) {
|
IP.isV6Format = function(str) {
|
||||||
|
if (str.length < 2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (str.length > 39)
|
||||||
|
return false;
|
||||||
|
|
||||||
return ipv6Regex.test(str);
|
return ipv6Regex.test(str);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user