Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Ignore an empty port component when parsing URLs.
  • Loading branch information
morkai authored and isaacs committed Mar 12, 2012
1 parent a10cfba commit 677c2c1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/url.js
Expand Up @@ -31,7 +31,7 @@ exports.format = urlFormat;
// define these here so at least they only have to be
// compiled once on the first module load.
var protocolPattern = /^([a-z0-9.+-]+:)/i,
portPattern = /:[0-9]+$/,
portPattern = /:[0-9]*$/,
// RFC 2396: characters reserved for delimiting URLs.
delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
// RFC 2396: characters not allowed for various reasons.
Expand Down Expand Up @@ -625,7 +625,9 @@ function parseHost(host) {
var port = portPattern.exec(host);
if (port) {
port = port[0];
out.port = port.substr(1);
if (port !== ':') {
out.port = port.substr(1);
}
host = host.substr(0, host.length - port.length);
}
if (host) out.hostname = host;
Expand Down
52 changes: 52 additions & 0 deletions test/simple/test-url.js
Expand Up @@ -545,6 +545,58 @@ var parseTests = {
'query': 'n=Temperature',
'pathname': '/.well-known/r',
'path': '/.well-known/r?n=Temperature'
},
// empty port
'http://example.com:': {
'protocol': 'http:',
'slashes': true,
'host': 'example.com',
'hostname': 'example.com',
'href': 'http://example.com/',
'pathname': '/',
'path': '/'
},
'http://example.com:/a/b.html': {
'protocol': 'http:',
'slashes': true,
'host': 'example.com',
'hostname': 'example.com',
'href': 'http://example.com/a/b.html',
'pathname': '/a/b.html',
'path': '/a/b.html'
},
'http://example.com:?a=b': {
'protocol': 'http:',
'slashes': true,
'host': 'example.com',
'hostname': 'example.com',
'href': 'http://example.com/?a=b',
'search': '?a=b',
'query': 'a=b',
'pathname': '/',
'path': '/?a=b'
},
'http://example.com:#abc': {
'protocol': 'http:',
'slashes': true,
'host': 'example.com',
'hostname': 'example.com',
'href': 'http://example.com/#abc',
'hash': '#abc',
'pathname': '/',
'path': '/'
},
'http://[fe80::1]:/a/b?a=b#abc': {
'protocol': 'http:',
'slashes': true,
'host': '[fe80::1]',
'hostname': 'fe80::1',
'href': 'http://[fe80::1]/a/b?a=b#abc',
'search': '?a=b',
'query': 'a=b',
'hash': '#abc',
'pathname': '/a/b',
'path': '/a/b?a=b'
}
};

Expand Down

0 comments on commit 677c2c1

Please sign in to comment.