summaryrefslogtreecommitdiff
path: root/lib/url.js
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2011-01-05 16:32:04 +0100
committerRyan Dahl <ry@tinyclouds.org>2011-01-05 11:27:22 -0800
commitaf15b4e45a7affa911307b0ad20f6b2a0a67df9d (patch)
tree18ae71bbb55b43e511a2b48d951d499d50391ed9 /lib/url.js
parent9ddfcfecca21cc7fde7d6388f35d07007de2a3a1 (diff)
downloadandroid-node-v8-af15b4e45a7affa911307b0ad20f6b2a0a67df9d.tar.gz
android-node-v8-af15b4e45a7affa911307b0ad20f6b2a0a67df9d.tar.bz2
android-node-v8-af15b4e45a7affa911307b0ad20f6b2a0a67df9d.zip
Remove path module dependency from url module
Now the path module can be adapted to support windows paths without breaking the url module. It also allows the undocumented keepBlanks flag to be removed from path.join and path.normalizeArray.
Diffstat (limited to 'lib/url.js')
-rw-r--r--lib/url.js45
1 files changed, 25 insertions, 20 deletions
diff --git a/lib/url.js b/lib/url.js
index b8c1105a9f..d497116f58 100644
--- a/lib/url.js
+++ b/lib/url.js
@@ -24,7 +24,6 @@ var protocolPattern = /^([a-z0-9]+:)/,
'gopher:': true,
'file:': true
},
- path = require('path'), // internal module, guaranteed to be loaded already.
querystring = require('querystring');
function urlParse(url, parseQueryString, slashesDenoteHost) {
@@ -280,7 +279,6 @@ function urlResolveObject(source, relative) {
return source;
}
- // resolve dots.
// if a url ENDs in . or .., then it must get a trailing slash.
// however, if it ends in anything else non-slashy,
// then it must NOT get a trailing slash.
@@ -289,27 +287,34 @@ function urlResolveObject(source, relative) {
(source.host || relative.host) && (last === '.' || last === '..') ||
last === '');
- // Figure out if this has to end up as an absolute url,
- // or should continue to be relative.
- srcPath = path.normalizeArray(srcPath, true);
- if (srcPath.length === 1 && srcPath[0] === '.') srcPath = [];
- if (mustEndAbs || removeAllDots) {
- // all dots must go.
- var dirs = [];
- srcPath.forEach(function(dir, i) {
- if (dir === '..') {
- dirs.pop();
- } else if (dir !== '.') {
- dirs.push(dir);
- }
- });
+ // strip single dots, resolve double dots to parent dir
+ // if the path tries to go above the root, `up` ends up > 0
+ var up = 0;
+ for (var i = srcPath.length; i >= 0; i--) {
+ last = srcPath[i];
+ if (last == '.') {
+ srcPath.splice(i, 1);
+ } else if (last === '..') {
+ srcPath.splice(i, 1);
+ up++;
+ } else if (up) {
+ srcPath.splice(i, 1);
+ up--;
+ }
+ }
- if (mustEndAbs && dirs[0] !== '' &&
- (!dirs[0] || dirs[0].charAt(0) !== '/')) {
- dirs.unshift('');
+ // if the path is allowed to go above the root, restore leading ..s
+ if (!mustEndAbs && !removeAllDots) {
+ for ( ; up--; up) {
+ srcPath.unshift('..');
}
- srcPath = dirs;
}
+
+ if (mustEndAbs && srcPath[0] !== '' &&
+ (!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
+ srcPath.unshift('');
+ }
+
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
srcPath.push('');
}