Skip to content

Commit 6d24083

Browse files
committed
feature(parser): fixing .parseHost() to not interpret colon in path as IPv6 hostname - closing #190
1 parent 8289ff2 commit 6d24083

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
241241
### master / development ###
242242

243243
* fixing inclusion of LICENSE in packages - ([Issue #174](https://github.com/medialize/URI.js/issues/174))
244-
* Adding meta data for [SPM](http://www.spmjs.io/) package manager - ([Issue #176](https://github.com/medialize/URI.js/issues/176))
244+
* fixing [`URI.parseHost()`](http://medialize.github.io/URI.js/docs.html#static-parseHost) to interpret colon in path as IPv6 hostname - ([Issue #190](https://github.com/medialize/URI.js/issues/190))
245+
* adding meta data for [SPM](http://www.spmjs.io/) package manager - ([Issue #176](https://github.com/medialize/URI.js/issues/176))
245246
* adding license meta to `bower.json`
246247

247248
### 1.14.1 (October 1st 2014) ###

src/URI.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,20 @@
462462
if (parts.port === '/') {
463463
parts.port = null;
464464
}
465-
} else if (string.indexOf(':') !== string.lastIndexOf(':')) {
466-
// IPv6 host contains multiple colons - but no port
467-
// this notation is actually not allowed by RFC 3986, but we're a liberal parser
468-
parts.hostname = string.substring(0, pos) || null;
469-
parts.port = null;
470465
} else {
471-
t = string.substring(0, pos).split(':');
472-
parts.hostname = t[0] || null;
473-
parts.port = t[1] || null;
466+
var firstColon = string.indexOf(':');
467+
var firstSlash = string.indexOf('/');
468+
var nextColon = string.indexOf(':', firstColon + 1);
469+
if (nextColon !== -1 && (firstSlash === -1 || nextColon < firstSlash)) {
470+
// IPv6 host contains multiple colons - but no port
471+
// this notation is actually not allowed by RFC 3986, but we're a liberal parser
472+
parts.hostname = string.substring(0, pos) || null;
473+
parts.port = null;
474+
} else {
475+
t = string.substring(0, pos).split(':');
476+
parts.hostname = t[0] || null;
477+
parts.port = t[1] || null;
478+
}
474479
}
475480

476481
if (parts.hostname && string.substring(pos).charAt(0) !== '/') {

test/urls.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,53 @@ var urls = [{
17051705
idn: false,
17061706
punycode: false
17071707
}
1708-
}
1708+
}, {
1709+
name: 'colon in path',
1710+
url: 'http://www.example.org:8080/hello:world',
1711+
parts: {
1712+
protocol: 'http',
1713+
username: null,
1714+
password: null,
1715+
hostname: 'www.example.org',
1716+
port: '8080',
1717+
path: '/hello:world',
1718+
query: null,
1719+
fragment: null
1720+
},
1721+
accessors: {
1722+
protocol: 'http',
1723+
username: '',
1724+
password: '',
1725+
port: '8080',
1726+
path: '/hello:world',
1727+
query: '',
1728+
fragment: '',
1729+
resource: '/hello:world',
1730+
authority: 'www.example.org:8080',
1731+
userinfo: '',
1732+
subdomain: 'www',
1733+
domain: 'example.org',
1734+
tld: 'org',
1735+
directory: '/',
1736+
filename: 'hello:world',
1737+
suffix: '',
1738+
hash: '', // location.hash style
1739+
search: '', // location.search style
1740+
host: 'www.example.org:8080',
1741+
hostname: 'www.example.org'
1742+
},
1743+
is: {
1744+
urn: false,
1745+
url: true,
1746+
relative: false,
1747+
name: true,
1748+
sld: false,
1749+
ip: false,
1750+
ip4: false,
1751+
ip6: false,
1752+
idn: false,
1753+
punycode: false
1754+
}
1755+
}
17091756
];
17101757

0 commit comments

Comments
 (0)