From 677c2c112c8f92910b85e14575c9c941799f5916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Walukiewicz?= Date: Mon, 12 Mar 2012 19:59:43 +0100 Subject: [PATCH] Ignore an empty port component when parsing URLs. --- lib/url.js | 6 +++-- test/simple/test-url.js | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/url.js b/lib/url.js index 8608a53fd8..31eb536e19 100644 --- a/lib/url.js +++ b/lib/url.js @@ -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. @@ -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; diff --git a/test/simple/test-url.js b/test/simple/test-url.js index ee15d4450d..7287d0f572 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -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' } };