Skip to content

Commit be4576d

Browse files
seebeeskoichik
authored andcommitted
url.resolveObject(url.parse(x), y) == url.parse(url.resolve(x, y));
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
1 parent ed744ec commit be4576d

2 files changed

Lines changed: 294 additions & 75 deletions

File tree

lib/url.js

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
295295
out.pathname = '/';
296296
}
297297

298+
//to support http.request
299+
if (out.pathname || out.search) {
300+
out.path = (out.pathname ? out.pathname : '') +
301+
(out.search ? out.search : '');
302+
}
303+
298304
// finally, reconstruct the href based on what has been validated.
299305
out.href = urlFormat(out);
300-
301306
return out;
302307
}
303308

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

369-
if (relative.href === '') return source;
374+
if (relative.href === '') {
375+
source.href = urlFormat(source);
376+
return source;
377+
}
370378

371379
// hrefs like //foo/bar always cut to the protocol.
372380
if (relative.slashes && !relative.protocol) {
373381
relative.protocol = source.protocol;
382+
//urlParse appends trailing / to urls like http://www.example.com
383+
if (slashedProtocol[relative.protocol] &&
384+
relative.hostname && !relative.pathname) {
385+
relative.path = relative.pathname = '/';
386+
}
387+
relative.href = urlFormat(relative);
374388
return relative;
375389
}
376390

@@ -383,14 +397,16 @@ function urlResolveObject(source, relative) {
383397
// if it is file:, then the host is dropped,
384398
// because that's known to be hostless.
385399
// anything else is assumed to be absolute.
386-
387-
if (!slashedProtocol[relative.protocol]) return relative;
388-
400+
if (!slashedProtocol[relative.protocol]) {
401+
relative.href = urlFormat(relative);
402+
return relative;
403+
}
389404
source.protocol = relative.protocol;
390405
if (!relative.host && !hostlessProtocol[relative.protocol]) {
391406
var relPath = (relative.pathname || '').split('/');
392407
while (relPath.length && !(relative.host = relPath.shift()));
393408
if (!relative.host) relative.host = '';
409+
if (!relative.hostname) relative.hostname = '';
394410
if (relPath[0] !== '') relPath.unshift('');
395411
if (relPath.length < 2) relPath.unshift('');
396412
relative.pathname = relPath.join('/');
@@ -399,9 +415,16 @@ function urlResolveObject(source, relative) {
399415
source.search = relative.search;
400416
source.query = relative.query;
401417
source.host = relative.host || '';
402-
delete source.auth;
403-
delete source.hostname;
418+
source.auth = relative.auth;
419+
source.hostname = relative.hostname || relative.host;
404420
source.port = relative.port;
421+
//to support http.request
422+
if (source.pathname !== undefined || source.search !== undefined) {
423+
source.path = (source.pathname ? source.pathname : '') +
424+
(source.search ? source.search : '');
425+
}
426+
source.slashes = source.slashes || relative.slashes;
427+
source.href = urlFormat(source);
405428
return source;
406429
}
407430

@@ -416,8 +439,7 @@ function urlResolveObject(source, relative) {
416439
srcPath = source.pathname && source.pathname.split('/') || [],
417440
relPath = relative.pathname && relative.pathname.split('/') || [],
418441
psychotic = source.protocol &&
419-
!slashedProtocol[source.protocol] &&
420-
source.host !== undefined;
442+
!slashedProtocol[source.protocol];
421443

422444
// if the url is a non-slashed url, then relative
423445
// links like ../.. should be able
@@ -452,6 +474,8 @@ function urlResolveObject(source, relative) {
452474
// it's absolute.
453475
source.host = (relative.host || relative.host === '') ?
454476
relative.host : source.host;
477+
source.hostname = (relative.hostname || relative.hostname === '') ?
478+
relative.hostname : source.hostname;
455479
source.search = relative.search;
456480
source.query = relative.query;
457481
srcPath = relPath;
@@ -469,19 +493,40 @@ function urlResolveObject(source, relative) {
469493
// like href='?foo'.
470494
// Put this after the other two cases because it simplifies the booleans
471495
if (psychotic) {
472-
source.host = srcPath.shift();
496+
source.hostname = source.host = srcPath.shift();
497+
//occationaly the auth can get stuck only in host
498+
//this especialy happens in cases like
499+
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
500+
var authInHost = source.host && source.host.indexOf('@') > 0 ?
501+
source.host.split('@') : false;
502+
if (authInHost) {
503+
source.auth = authInHost.shift();
504+
source.hostname = authInHost.shift();
505+
}
473506
}
474507
source.search = relative.search;
475508
source.query = relative.query;
509+
//to support http.request
510+
if (source.pathname !== undefined || source.search !== undefined) {
511+
source.path = (source.pathname ? source.pathname : '') +
512+
(source.search ? source.search : '');
513+
}
514+
source.href = urlFormat(source);
476515
return source;
477516
}
478517
if (!srcPath.length) {
479518
// no path at all. easy.
480519
// we've already handled the other stuff above.
481520
delete source.pathname;
521+
//to support http.request
522+
if (!source.search) {
523+
source.path = '/' + source.search;
524+
} else {
525+
delete source.path;
526+
}
527+
source.href = urlFormat(source);
482528
return source;
483529
}
484-
485530
// if a url ENDs in . or .., then it must get a trailing slash.
486531
// however, if it ends in anything else non-slashy,
487532
// then it must NOT get a trailing slash.
@@ -527,7 +572,17 @@ function urlResolveObject(source, relative) {
527572

528573
// put the host back
529574
if (psychotic) {
530-
source.host = isAbsolute ? '' : srcPath.shift();
575+
source.hostname = source.host = isAbsolute ? '' :
576+
srcPath.length ? srcPath.shift() : '';
577+
//occationaly the auth can get stuck only in host
578+
//this especialy happens in cases like
579+
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
580+
var authInHost = source.host && source.host.indexOf('@') > 0 ?
581+
source.host.split('@') : false;
582+
if (authInHost) {
583+
relative.auth = authInHost.shift();
584+
source.hostname = authInHost.shift();
585+
}
531586
}
532587

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

539594
source.pathname = srcPath.join('/');
540-
541-
595+
//to support request.http
596+
if (source.pathname !== undefined || source.search !== undefined) {
597+
source.path = (source.pathname ? source.pathname : '') +
598+
(source.search ? source.search : '');
599+
}
600+
source.auth = relative.auth;
601+
source.slashes = source.slashes || relative.slashes;
602+
source.href = urlFormat(source);
542603
return source;
543604
}
544605

0 commit comments

Comments
 (0)