Handle protocols that seperate the host from path with colon #3471

Closed
wants to merge 3 commits into
from
View
@@ -74,18 +74,25 @@ var protocolPattern = /^([a-z0-9.+-]+:)/i,
'gopher:': true,
'file:': true
},
+ // protocols that seperat host from path with colon.
+ colonProtocol = {
+ 'ssh': true,
+ 'ssh:': true
+ },
// protocols that always contain a // bit.
slashedProtocol = {
'http': true,
'https': true,
'ftp': true,
'gopher': true,
'file': true,
+ 'ssh': true,
'http:': true,
'https:': true,
'ftp:': true,
'gopher:': true,
- 'file:': true
+ 'file:': true,
+ 'ssh:': true
bnoordhuis
bnoordhuis Jun 28, 2012 Owner

Unnecessary change, please undo.

},
querystring = require('querystring');
@@ -300,6 +307,12 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
out.pathname = '/';
}
+ // protocols that seperate host from path with ':'
+ if (colonProtocol[proto] &&
+ out.pathname && out.pathname.slice(0, 2) == '/:') {
+ out.pathname = out.pathname.substr(2);
+ }
+
//to support http.request
if (out.pathname || out.search) {
out.path = (out.pathname ? out.pathname : '') +
@@ -357,11 +370,16 @@ function urlFormat(obj) {
if (obj.slashes ||
(!protocol || slashedProtocol[protocol]) && host !== false) {
host = '//' + (host || '');
- if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
+ if (pathname && pathname.charAt(0) !== '/' &&
+ !colonProtocol[protocol]) {
+ pathname = '/' + pathname;
+ }
} else if (!host) {
host = '';
}
+ if (colonProtocol[protocol] && pathname) pathname = ':' + pathname;
+
if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
if (search && search.charAt(0) !== '?') search = '?' + search;
View
@@ -430,6 +430,18 @@ var parseTests = {
'path': '/bar'
},
+ // protocols that seperate the host from path with ':'
+ 'ssh://user@foo.com:folder': {
+ 'href': 'ssh://user@foo.com:folder',
+ 'host': 'foo.com',
+ 'auth': 'user',
+ 'hostname': 'foo.com',
+ 'protocol': 'ssh:',
+ 'pathname': 'folder',
+ 'path': 'folder',
+ 'slashes': true
+ },
+
// IDNA tests
'http://www.日本語.com/' : {
'href': 'http://www.xn--wgv71a119e.com/',
@@ -505,24 +517,23 @@ var parseTests = {
},
'http://bucket_name.s3.amazonaws.com/image.jpg': {
- protocol: 'http:',
+ 'protocol': 'http:',
'slashes': true,
- slashes: true,
- host: 'bucket_name.s3.amazonaws.com',
- hostname: 'bucket_name.s3.amazonaws.com',
- pathname: '/image.jpg',
- href: 'http://bucket_name.s3.amazonaws.com/image.jpg',
+ 'host': 'bucket_name.s3.amazonaws.com',
+ 'hostname': 'bucket_name.s3.amazonaws.com',
+ 'pathname': '/image.jpg',
+ 'href': 'http://bucket_name.s3.amazonaws.com/image.jpg',
'path': '/image.jpg'
},
'git+http://github.com/joyent/node.git': {
- protocol: 'git+http:',
- slashes: true,
- host: 'github.com',
- hostname: 'github.com',
- pathname: '/joyent/node.git',
- path: '/joyent/node.git',
- href: 'git+http://github.com/joyent/node.git'
+ 'protocol': 'git+http:',
+ 'slashes': true,
+ 'host': 'github.com',
+ 'hostname': 'github.com',
+ 'pathname': '/joyent/node.git',
+ 'path': '/joyent/node.git',
+ 'href': 'git+http://github.com/joyent/node.git'
},
//if local1@domain1 is uses as a relative URL it may