Browse Source

url: reslove urls with . and ..

'.' 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: https://github.com/joyent/node/issues/8992
PR-URL: https://github.com/iojs/io.js/pull/278
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v1.8.0-commit
Amir Saboury 10 years ago
committed by cjihrig
parent
commit
faa687b4be
  1. 4
      lib/url.js
  2. 8
      test/parallel/test-url.js

4
lib/url.js

@ -619,8 +619,8 @@ Url.prototype.resolveObject = function(relative) {
// then it must NOT get a trailing slash. // then it must NOT get a trailing slash.
var last = srcPath.slice(-1)[0]; var last = srcPath.slice(-1)[0];
var hasTrailingSlash = ( var hasTrailingSlash = (
(result.host || relative.host) && (last === '.' || last === '..') || (result.host || relative.host || srcPath.length > 1) &&
last === ''); (last === '.' || last === '..') || last === '');
// strip single dots, resolve double dots to parent dir // strip single dots, resolve double dots to parent dir
// if the path tries to go above the root, `up` ends up > 0 // if the path tries to go above the root, `up` ends up > 0

8
test/parallel/test-url.js

@ -1157,6 +1157,14 @@ var relativeTests = [
['/foo/bar/baz/', 'quux/baz', '/foo/bar/baz/quux/baz'], ['/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/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'],
['foo/bar/', '../../../baz', '../baz'], ['foo/bar/', '../../../baz', '../baz'],
['http://example.com/b//c//d;p?q#blarg', 'https:#hash2', 'https:///#hash2'], ['http://example.com/b//c//d;p?q#blarg', 'https:#hash2', 'https:///#hash2'],

Loading…
Cancel
Save