Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
url.resolveObject(url.parse(x), y) == url.parse(url.resolve(x, y));
Browse files Browse the repository at this point in the history
added a .path property = .pathname + .search for use with http.request

And tests to verify everything.
With the tests, I changed over to deepEqual, and I would note the comment on the test
['.//g', 'f:/a', 'f://g'], which I think is a fundamental problem

This supersedes pull 1596
  • Loading branch information
seebees authored and koichik committed Oct 22, 2011
1 parent ed744ec commit be4576d
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 75 deletions.
89 changes: 75 additions & 14 deletions lib/url.js
Expand Up @@ -295,9 +295,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
out.pathname = '/';
}

//to support http.request
if (out.pathname || out.search) {
out.path = (out.pathname ? out.pathname : '') +
(out.search ? out.search : '');
}

// finally, reconstruct the href based on what has been validated.
out.href = urlFormat(out);

return out;
}

Expand Down Expand Up @@ -366,11 +371,20 @@ function urlResolveObject(source, relative) {
// hash is always overridden, no matter what.
source.hash = relative.hash;

if (relative.href === '') return source;
if (relative.href === '') {
source.href = urlFormat(source);
return source;
}

// hrefs like //foo/bar always cut to the protocol.
if (relative.slashes && !relative.protocol) {
relative.protocol = source.protocol;
//urlParse appends trailing / to urls like http://www.example.com
if (slashedProtocol[relative.protocol] &&
relative.hostname && !relative.pathname) {
relative.path = relative.pathname = '/';
}
relative.href = urlFormat(relative);
return relative;
}

Expand All @@ -383,14 +397,16 @@ function urlResolveObject(source, relative) {
// if it is file:, then the host is dropped,
// because that's known to be hostless.
// anything else is assumed to be absolute.

if (!slashedProtocol[relative.protocol]) return relative;

if (!slashedProtocol[relative.protocol]) {
relative.href = urlFormat(relative);
return relative;
}
source.protocol = relative.protocol;
if (!relative.host && !hostlessProtocol[relative.protocol]) {
var relPath = (relative.pathname || '').split('/');
while (relPath.length && !(relative.host = relPath.shift()));
if (!relative.host) relative.host = '';
if (!relative.hostname) relative.hostname = '';
if (relPath[0] !== '') relPath.unshift('');
if (relPath.length < 2) relPath.unshift('');
relative.pathname = relPath.join('/');
Expand All @@ -399,9 +415,16 @@ function urlResolveObject(source, relative) {
source.search = relative.search;
source.query = relative.query;
source.host = relative.host || '';
delete source.auth;
delete source.hostname;
source.auth = relative.auth;
source.hostname = relative.hostname || relative.host;
source.port = relative.port;
//to support http.request
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
(source.search ? source.search : '');
}
source.slashes = source.slashes || relative.slashes;
source.href = urlFormat(source);
return source;
}

Expand All @@ -416,8 +439,7 @@ function urlResolveObject(source, relative) {
srcPath = source.pathname && source.pathname.split('/') || [],
relPath = relative.pathname && relative.pathname.split('/') || [],
psychotic = source.protocol &&
!slashedProtocol[source.protocol] &&
source.host !== undefined;
!slashedProtocol[source.protocol];

// if the url is a non-slashed url, then relative
// links like ../.. should be able
Expand Down Expand Up @@ -452,6 +474,8 @@ function urlResolveObject(source, relative) {
// it's absolute.
source.host = (relative.host || relative.host === '') ?
relative.host : source.host;
source.hostname = (relative.hostname || relative.hostname === '') ?
relative.hostname : source.hostname;
source.search = relative.search;
source.query = relative.query;
srcPath = relPath;
Expand All @@ -469,19 +493,40 @@ function urlResolveObject(source, relative) {
// like href='?foo'.
// Put this after the other two cases because it simplifies the booleans
if (psychotic) {
source.host = srcPath.shift();
source.hostname = source.host = srcPath.shift();
//occationaly the auth can get stuck only in host
//this especialy happens in cases like
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
var authInHost = source.host && source.host.indexOf('@') > 0 ?
source.host.split('@') : false;
if (authInHost) {
source.auth = authInHost.shift();
source.hostname = authInHost.shift();
}
}
source.search = relative.search;
source.query = relative.query;
//to support http.request
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
(source.search ? source.search : '');
}
source.href = urlFormat(source);
return source;
}
if (!srcPath.length) {
// no path at all. easy.
// we've already handled the other stuff above.
delete source.pathname;
//to support http.request
if (!source.search) {
source.path = '/' + source.search;
} else {
delete source.path;
}
source.href = urlFormat(source);
return source;
}

// 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.
Expand Down Expand Up @@ -527,7 +572,17 @@ function urlResolveObject(source, relative) {

// put the host back
if (psychotic) {
source.host = isAbsolute ? '' : srcPath.shift();
source.hostname = source.host = isAbsolute ? '' :
srcPath.length ? srcPath.shift() : '';
//occationaly the auth can get stuck only in host
//this especialy happens in cases like
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
var authInHost = source.host && source.host.indexOf('@') > 0 ?
source.host.split('@') : false;
if (authInHost) {
relative.auth = authInHost.shift();
source.hostname = authInHost.shift();
}
}

mustEndAbs = mustEndAbs || (source.host && srcPath.length);
Expand All @@ -537,8 +592,14 @@ function urlResolveObject(source, relative) {
}

source.pathname = srcPath.join('/');


//to support request.http
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
(source.search ? source.search : '');
}
source.auth = relative.auth;
source.slashes = source.slashes || relative.slashes;
source.href = urlFormat(source);
return source;
}

Expand Down

0 comments on commit be4576d

Please sign in to comment.