Skip to content

Commit

Permalink
url: fix resolving from non-file to file URLs.
Browse files Browse the repository at this point in the history
When resolving a reference URL with the 'file' scheme an no host
against a base URL without the 'file' scheme, the first path element
of the reference URL is used as the host for the target URL. This
results in an invalid target URL.

This change makes an exception for file URLs so that the host is not
mangled during URL resolution.

PR-URL: #1277
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>
  • Loading branch information
jagoda authored and petkaantonov committed Apr 4, 2015
1 parent 8c6c376 commit 1e94057
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ Url.prototype.resolveObject = function(relative) {
}

result.protocol = relative.protocol;
if (!relative.host && !hostlessProtocol[relative.protocol]) {
if (!relative.host &&
!/^file:?$/.test(relative.protocol) &&
!hostlessProtocol[relative.protocol]) {
var relPath = (relative.pathname || '').split('/');
while (relPath.length && !(relative.host = relPath.shift()));
if (!relative.host) relative.host = '';
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,9 @@ var relativeTests = [
['http://example.com/b//c//d;p?q#blarg',
'http:/a/b/c/d',
'http://example.com/a/b/c/d'],
['/foo/bar/baz', '/../etc/passwd', '/etc/passwd']
['/foo/bar/baz', '/../etc/passwd', '/etc/passwd'],
['http://localhost', 'file:///Users/foo', 'file:///Users/foo'],
['http://localhost', 'file://foo/Users', 'file://foo/Users']
];
relativeTests.forEach(function(relativeTest) {
var a = url.resolve(relativeTest[0], relativeTest[1]),
Expand Down

0 comments on commit 1e94057

Please sign in to comment.