url: fix resolving from non-file to file URLs.
authorJeffrey Jagoda <jeffrey.jagoda@gmail.com>
Thu, 26 Mar 2015 19:34:16 +0000 (15:34 -0400)
committerPetka Antonov <petka_antonov@hotmail.com>
Sat, 4 Apr 2015 10:25:32 +0000 (13:25 +0300)
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: https://github.com/iojs/io.js/pull/1277
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>
lib/url.js
test/parallel/test-url.js

index 82209db..1683f90 100644 (file)
@@ -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 = '';
index 77d2242..121d6ca 100644 (file)
@@ -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]),