Skip to content

Commit

Permalink
fixing URI.parseHost() and URI.buildHost() to properly parse and …
Browse files Browse the repository at this point in the history
…build IPv6 hosts - closes #144
  • Loading branch information
rodneyrehm committed Apr 15, 2014
1 parent 5c2d822 commit b57c5b6
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Documents specifying how URLs work:
* [URL - Living Standard](http://url.spec.whatwg.org/)
* [RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax](http://tools.ietf.org/html/rfc3986)
* [RFC 3987 - Internationalized Resource Identifiers (IRI)](http://tools.ietf.org/html/rfc3987)
* [RFC 2732 - Format for Literal IPv6 Addresses in URL's](http://tools.ietf.org/html/rfc2732)
* [Punycode: A Bootstring encoding of Unicode for Internationalized Domain Names in Applications (IDNA)](http://tools.ietf.org/html/rfc3492)
* [application/x-www-form-urlencoded](http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type) (Query String Parameters) and [application/x-www-form-urlencoded encoding algorithm](http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#application/x-www-form-urlencoded-encoding-algorithm)
* [What every web developer must know about URL encoding](http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding)
Expand Down Expand Up @@ -227,6 +228,10 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### master ###

* fixing [`URI.parseHost()`](http://medialize.github.io/URI.js/docs.html#static-parseHost) and [`URI.buildHost()`](http://medialize.github.io/URI.js/docs.html#static-buildHost) to properly parse and build the IPv6 examples given in [RFC2732 Format for Literal IPv6 Addresses in URL's](http://tools.ietf.org/html/rfc2732#section-2) - ([Issue #144](https://github.com/medialize/URI.js/issues/144))

### 1.12.1 (March 8th 2014) ###

* fixing [`.encodeQuery()`](http://medialize.github.io/URI.js/docs.html#static-encodeQuery) and [`.decodeQuery()`](http://medialize.github.io/URI.js/docs.html#static-decodeQuery) to respect [`URI.escapeQuerySpace`](http://medialize.github.io/URI.js/docs.html#setting-escapeQuerySpace) - ([Issue #137](https://github.com/medialize/URI.js/issues/137))
Expand Down
20 changes: 9 additions & 11 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ URI.parseHost = function(string, parts) {
// IPv6+port in the format [2001:db8::1]:80 (for the time being)
bracketPos = string.indexOf(']');
parts.hostname = string.substring(1, bracketPos) || null;
parts.port = string.substring(bracketPos+2, pos) || null;
parts.port = string.substring(bracketPos + 2, pos) || null;
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
Expand Down Expand Up @@ -565,18 +568,13 @@ URI.buildHost = function(parts) {
if (!parts.hostname) {
return "";
} else if (URI.ip6_expression.test(parts.hostname)) {
if (parts.port) {
t += "[" + parts.hostname + "]:" + parts.port;
} else {
// don't know if we should always wrap IPv6 in []
// the RFC explicitly says SHOULD, not MUST.
t += parts.hostname;
}
t += "[" + parts.hostname + "]";
} else {
t += parts.hostname;
if (parts.port) {
t += ':' + parts.port;
}
}

if (parts.port) {
t += ':' + parts.port;
}

return t;
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ test("normalizeHost", function() {
}

if (window.IPv6) {
u = new URI("http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html");
u = new URI("http://[fe80:0000:0000:0000:0204:61ff:fe9d:f156]/foobar.html");
u.normalizeHostname();
equal(u+"", "http://fe80::204:61ff:fe9d:f156/foobar.html", "best IPv6 representation");
equal(u+"", "http://[fe80::204:61ff:fe9d:f156]/foobar.html", "best IPv6 representation");
}

u = new URI("http://wWw.eXamplE.Org/foobar.html");
Expand Down
Loading

0 comments on commit b57c5b6

Please sign in to comment.