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

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/url.js
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary change, please undo.

},
querystring = require('querystring');

Expand Down Expand Up @@ -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 : '') +
Expand Down Expand Up @@ -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;

Expand Down
37 changes: 24 additions & 13 deletions test/simple/test-url.js
Expand Up @@ -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/',
Expand Down Expand Up @@ -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
Expand Down