From 9b534e2e87489d219f0d0cf5be7f9119704fe424 Mon Sep 17 00:00:00 2001 From: Amir Saboury Date: Sun, 11 Jan 2015 17:55:12 -0500 Subject: [PATCH] url: resolve 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 #8992. Reviewed-By: Colin Ihrig PR-URL: https://github.com/joyent/node/pull/9427 --- lib/url.js | 4 ++-- test/simple/test-url.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/url.js b/lib/url.js index 1c7506023cc..c5a3793b9a9 100644 --- a/lib/url.js +++ b/lib/url.js @@ -641,8 +641,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 diff --git a/test/simple/test-url.js b/test/simple/test-url.js index e81908a883f..d0ddaf20318 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -1178,6 +1178,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'],