Skip to content

Commit

Permalink
url: reslove urls with . and ..
Browse files Browse the repository at this point in the history
'.' and '..' are directory specs and resolving urls with or
without the hostname with '.' and '..' should add a trailing
slash to the end of the url.

Fixes: nodejs/node-v0.x-archive#8992
PR-URL: #278
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
amir-s authored and cjihrig committed Feb 13, 2015
1 parent cca8de6 commit faa687b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ Url.prototype.resolveObject = function(relative) {
// then it must NOT get a trailing slash.
var last = srcPath.slice(-1)[0];
var hasTrailingSlash = (
(result.host || relative.host) && (last === '.' || last === '..') ||
last === '');
(result.host || relative.host || srcPath.length > 1) &&
(last === '.' || last === '..') || last === '');

// strip single dots, resolve double dots to parent dir
// if the path tries to go above the root, `up` ends up > 0
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,14 @@ var relativeTests = [
['/foo/bar/baz/', 'quux/baz', '/foo/bar/baz/quux/baz'],
['/foo/bar/baz', '../../../../../../../../quux/baz', '/quux/baz'],
['/foo/bar/baz', '../../../../../../../quux/baz', '/quux/baz'],
['/foo', '.', '/'],
['/foo', '..', '/'],
['/foo/', '.', '/foo/'],
['/foo/', '..', '/'],
['/foo/bar', '.', '/foo/'],
['/foo/bar', '..', '/'],
['/foo/bar/', '.', '/foo/bar/'],
['/foo/bar/', '..', '/foo/'],
['foo/bar', '../../../baz', '../../baz'],
['foo/bar/', '../../../baz', '../baz'],
['http://example.com/b//c//d;p?q#blarg', 'https:#hash2', 'https:///#hash2'],
Expand Down

0 comments on commit faa687b

Please sign in to comment.