Skip to content

Commit

Permalink
feature(parser): fixing .parseHost() to not interpret colon in path…
Browse files Browse the repository at this point in the history
… as IPv6 hostname - closing #190
  • Loading branch information
rodneyrehm committed Feb 24, 2015
1 parent 8289ff2 commit 6d24083
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
### master / development ###

* fixing inclusion of LICENSE in packages - ([Issue #174](https://github.com/medialize/URI.js/issues/174))
* Adding meta data for [SPM](http://www.spmjs.io/) package manager - ([Issue #176](https://github.com/medialize/URI.js/issues/176))
* 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))
* adding meta data for [SPM](http://www.spmjs.io/) package manager - ([Issue #176](https://github.com/medialize/URI.js/issues/176))
* adding license meta to `bower.json`

### 1.14.1 (October 1st 2014) ###
Expand Down
21 changes: 13 additions & 8 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,20 @@
if (parts.port === '/') {
parts.port = null;
}
} else if (string.indexOf(':') !== string.lastIndexOf(':')) {
// IPv6 host contains multiple colons - but no port
// this notation is actually not allowed by RFC 3986, but we're a liberal parser
parts.hostname = string.substring(0, pos) || null;
parts.port = null;
} else {
t = string.substring(0, pos).split(':');
parts.hostname = t[0] || null;
parts.port = t[1] || null;
var firstColon = string.indexOf(':');
var firstSlash = string.indexOf('/');
var nextColon = string.indexOf(':', firstColon + 1);
if (nextColon !== -1 && (firstSlash === -1 || nextColon < firstSlash)) {
// IPv6 host contains multiple colons - but no port
// this notation is actually not allowed by RFC 3986, but we're a liberal parser
parts.hostname = string.substring(0, pos) || null;
parts.port = null;
} else {
t = string.substring(0, pos).split(':');
parts.hostname = t[0] || null;
parts.port = t[1] || null;
}
}

if (parts.hostname && string.substring(pos).charAt(0) !== '/') {
Expand Down
49 changes: 48 additions & 1 deletion test/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,53 @@ var urls = [{
idn: false,
punycode: false
}
}
}, {
name: 'colon in path',
url: 'http://www.example.org:8080/hello:world',
parts: {
protocol: 'http',
username: null,
password: null,
hostname: 'www.example.org',
port: '8080',
path: '/hello:world',
query: null,
fragment: null
},
accessors: {
protocol: 'http',
username: '',
password: '',
port: '8080',
path: '/hello:world',
query: '',
fragment: '',
resource: '/hello:world',
authority: 'www.example.org:8080',
userinfo: '',
subdomain: 'www',
domain: 'example.org',
tld: 'org',
directory: '/',
filename: 'hello:world',
suffix: '',
hash: '', // location.hash style
search: '', // location.search style
host: 'www.example.org:8080',
hostname: 'www.example.org'
},
is: {
urn: false,
url: true,
relative: false,
name: true,
sld: false,
ip: false,
ip4: false,
ip6: false,
idn: false,
punycode: false
}
}
];

0 comments on commit 6d24083

Please sign in to comment.