UNPKG

23.5 kBJavaScriptView Raw
1/*
2 * Copyright Joyent, Inc. and other Node contributors.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24'use strict';
25
26var punycode = require('punycode/');
27
28function Url() {
29 this.protocol = null;
30 this.slashes = null;
31 this.auth = null;
32 this.host = null;
33 this.port = null;
34 this.hostname = null;
35 this.hash = null;
36 this.search = null;
37 this.query = null;
38 this.pathname = null;
39 this.path = null;
40 this.href = null;
41}
42
43// Reference: RFC 3986, RFC 1808, RFC 2396
44
45/*
46 * define these here so at least they only have to be
47 * compiled once on the first module load.
48 */
49var protocolPattern = /^([a-z0-9.+-]+:)/i,
50 portPattern = /:[0-9]*$/,
51
52 // Special case for a simple path URL
53 simplePathPattern = /^(\/\/?(?!\/)[^?\s]*)(\?[^\s]*)?$/,
54
55 /*
56 * RFC 2396: characters reserved for delimiting URLs.
57 * We actually just auto-escape these.
58 */
59 delims = [
60 '<', '>', '"', '`', ' ', '\r', '\n', '\t'
61 ],
62
63 // RFC 2396: characters not allowed for various reasons.
64 unwise = [
65 '{', '}', '|', '\\', '^', '`'
66 ].concat(delims),
67
68 // Allowed by RFCs, but cause of XSS attacks. Always escape these.
69 autoEscape = ['\''].concat(unwise),
70 /*
71 * Characters that are never ever allowed in a hostname.
72 * Note that any invalid chars are also handled, but these
73 * are the ones that are *expected* to be seen, so we fast-path
74 * them.
75 */
76 nonHostChars = [
77 '%', '/', '?', ';', '#'
78 ].concat(autoEscape),
79 hostEndingChars = [
80 '/', '?', '#'
81 ],
82 hostnameMaxLen = 255,
83 hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/,
84 hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/,
85 // protocols that can allow "unsafe" and "unwise" chars.
86 unsafeProtocol = {
87 javascript: true,
88 'javascript:': true
89 },
90 // protocols that never have a hostname.
91 hostlessProtocol = {
92 javascript: true,
93 'javascript:': true
94 },
95 // protocols that always contain a // bit.
96 slashedProtocol = {
97 http: true,
98 https: true,
99 ftp: true,
100 gopher: true,
101 file: true,
102 'http:': true,
103 'https:': true,
104 'ftp:': true,
105 'gopher:': true,
106 'file:': true
107 },
108 querystring = require('qs');
109
110function urlParse(url, parseQueryString, slashesDenoteHost) {
111 if (url && typeof url === 'object' && url instanceof Url) { return url; }
112
113 var u = new Url();
114 u.parse(url, parseQueryString, slashesDenoteHost);
115 return u;
116}
117
118Url.prototype.parse = function (url, parseQueryString, slashesDenoteHost) {
119 if (typeof url !== 'string') {
120 throw new TypeError("Parameter 'url' must be a string, not " + typeof url);
121 }
122
123 /*
124 * Copy chrome, IE, opera backslash-handling behavior.
125 * Back slashes before the query string get converted to forward slashes
126 * See: https://code.google.com/p/chromium/issues/detail?id=25916
127 */
128 var queryIndex = url.indexOf('?'),
129 splitter = queryIndex !== -1 && queryIndex < url.indexOf('#') ? '?' : '#',
130 uSplit = url.split(splitter),
131 slashRegex = /\\/g;
132 uSplit[0] = uSplit[0].replace(slashRegex, '/');
133 url = uSplit.join(splitter);
134
135 var rest = url;
136
137 /*
138 * trim before proceeding.
139 * This is to support parse stuff like " http://foo.com \n"
140 */
141 rest = rest.trim();
142
143 if (!slashesDenoteHost && url.split('#').length === 1) {
144 // Try fast path regexp
145 var simplePath = simplePathPattern.exec(rest);
146 if (simplePath) {
147 this.path = rest;
148 this.href = rest;
149 this.pathname = simplePath[1];
150 if (simplePath[2]) {
151 this.search = simplePath[2];
152 if (parseQueryString) {
153 this.query = querystring.parse(this.search.substr(1));
154 } else {
155 this.query = this.search.substr(1);
156 }
157 } else if (parseQueryString) {
158 this.search = '';
159 this.query = {};
160 }
161 return this;
162 }
163 }
164
165 var proto = protocolPattern.exec(rest);
166 if (proto) {
167 proto = proto[0];
168 var lowerProto = proto.toLowerCase();
169 this.protocol = lowerProto;
170 rest = rest.substr(proto.length);
171 }
172
173 /*
174 * figure out if it's got a host
175 * user@server is *always* interpreted as a hostname, and url
176 * resolution will treat //foo/bar as host=foo,path=bar because that's
177 * how the browser resolves relative URLs.
178 */
179 if (slashesDenoteHost || proto || rest.match(/^\/\/[^@/]+@[^@/]+/)) {
180 var slashes = rest.substr(0, 2) === '//';
181 if (slashes && !(proto && hostlessProtocol[proto])) {
182 rest = rest.substr(2);
183 this.slashes = true;
184 }
185 }
186
187 if (!hostlessProtocol[proto] && (slashes || (proto && !slashedProtocol[proto]))) {
188
189 /*
190 * there's a hostname.
191 * the first instance of /, ?, ;, or # ends the host.
192 *
193 * If there is an @ in the hostname, then non-host chars *are* allowed
194 * to the left of the last @ sign, unless some host-ending character
195 * comes *before* the @-sign.
196 * URLs are obnoxious.
197 *
198 * ex:
199 * http://a@b@c/ => user:a@b host:c
200 * http://a@b?@c => user:a host:c path:/?@c
201 */
202
203 /*
204 * v0.12 TODO(isaacs): This is not quite how Chrome does things.
205 * Review our test case against browsers more comprehensively.
206 */
207
208 // find the first instance of any hostEndingChars
209 var hostEnd = -1;
210 for (var i = 0; i < hostEndingChars.length; i++) {
211 var hec = rest.indexOf(hostEndingChars[i]);
212 if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) { hostEnd = hec; }
213 }
214
215 /*
216 * at this point, either we have an explicit point where the
217 * auth portion cannot go past, or the last @ char is the decider.
218 */
219 var auth, atSign;
220 if (hostEnd === -1) {
221 // atSign can be anywhere.
222 atSign = rest.lastIndexOf('@');
223 } else {
224 /*
225 * atSign must be in auth portion.
226 * http://a@b/c@d => host:b auth:a path:/c@d
227 */
228 atSign = rest.lastIndexOf('@', hostEnd);
229 }
230
231 /*
232 * Now we have a portion which is definitely the auth.
233 * Pull that off.
234 */
235 if (atSign !== -1) {
236 auth = rest.slice(0, atSign);
237 rest = rest.slice(atSign + 1);
238 this.auth = decodeURIComponent(auth);
239 }
240
241 // the host is the remaining to the left of the first non-host char
242 hostEnd = -1;
243 for (var i = 0; i < nonHostChars.length; i++) {
244 var hec = rest.indexOf(nonHostChars[i]);
245 if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) { hostEnd = hec; }
246 }
247 // if we still have not hit it, then the entire thing is a host.
248 if (hostEnd === -1) { hostEnd = rest.length; }
249
250 this.host = rest.slice(0, hostEnd);
251 rest = rest.slice(hostEnd);
252
253 // pull out port.
254 this.parseHost();
255
256 /*
257 * we've indicated that there is a hostname,
258 * so even if it's empty, it has to be present.
259 */
260 this.hostname = this.hostname || '';
261
262 /*
263 * if hostname begins with [ and ends with ]
264 * assume that it's an IPv6 address.
265 */
266 var ipv6Hostname = this.hostname[0] === '[' && this.hostname[this.hostname.length - 1] === ']';
267
268 // validate a little.
269 if (!ipv6Hostname) {
270 var hostparts = this.hostname.split(/\./);
271 for (var i = 0, l = hostparts.length; i < l; i++) {
272 var part = hostparts[i];
273 if (!part) { continue; }
274 if (!part.match(hostnamePartPattern)) {
275 var newpart = '';
276 for (var j = 0, k = part.length; j < k; j++) {
277 if (part.charCodeAt(j) > 127) {
278 /*
279 * we replace non-ASCII char with a temporary placeholder
280 * we need this to make sure size of hostname is not
281 * broken by replacing non-ASCII by nothing
282 */
283 newpart += 'x';
284 } else {
285 newpart += part[j];
286 }
287 }
288 // we test again with ASCII char only
289 if (!newpart.match(hostnamePartPattern)) {
290 var validParts = hostparts.slice(0, i);
291 var notHost = hostparts.slice(i + 1);
292 var bit = part.match(hostnamePartStart);
293 if (bit) {
294 validParts.push(bit[1]);
295 notHost.unshift(bit[2]);
296 }
297 if (notHost.length) {
298 rest = '/' + notHost.join('.') + rest;
299 }
300 this.hostname = validParts.join('.');
301 break;
302 }
303 }
304 }
305 }
306
307 if (this.hostname.length > hostnameMaxLen) {
308 this.hostname = '';
309 } else {
310 // hostnames are always lower case.
311 this.hostname = this.hostname.toLowerCase();
312 }
313
314 if (!ipv6Hostname) {
315 /*
316 * IDNA Support: Returns a punycoded representation of "domain".
317 * It only converts parts of the domain name that
318 * have non-ASCII characters, i.e. it doesn't matter if
319 * you call it with a domain that already is ASCII-only.
320 */
321 this.hostname = punycode.toASCII(this.hostname);
322 }
323
324 var p = this.port ? ':' + this.port : '';
325 var h = this.hostname || '';
326 this.host = h + p;
327 this.href += this.host;
328
329 /*
330 * strip [ and ] from the hostname
331 * the host field still retains them, though
332 */
333 if (ipv6Hostname) {
334 this.hostname = this.hostname.substr(1, this.hostname.length - 2);
335 if (rest[0] !== '/') {
336 rest = '/' + rest;
337 }
338 }
339 }
340
341 /*
342 * now rest is set to the post-host stuff.
343 * chop off any delim chars.
344 */
345 if (!unsafeProtocol[lowerProto]) {
346
347 /*
348 * First, make 100% sure that any "autoEscape" chars get
349 * escaped, even if encodeURIComponent doesn't think they
350 * need to be.
351 */
352 for (var i = 0, l = autoEscape.length; i < l; i++) {
353 var ae = autoEscape[i];
354 if (rest.indexOf(ae) === -1) { continue; }
355 var esc = encodeURIComponent(ae);
356 if (esc === ae) {
357 esc = escape(ae);
358 }
359 rest = rest.split(ae).join(esc);
360 }
361 }
362
363 // chop off from the tail first.
364 var hash = rest.indexOf('#');
365 if (hash !== -1) {
366 // got a fragment string.
367 this.hash = rest.substr(hash);
368 rest = rest.slice(0, hash);
369 }
370 var qm = rest.indexOf('?');
371 if (qm !== -1) {
372 this.search = rest.substr(qm);
373 this.query = rest.substr(qm + 1);
374 if (parseQueryString) {
375 this.query = querystring.parse(this.query);
376 }
377 rest = rest.slice(0, qm);
378 } else if (parseQueryString) {
379 // no query string, but parseQueryString still requested
380 this.search = '';
381 this.query = {};
382 }
383 if (rest) { this.pathname = rest; }
384 if (slashedProtocol[lowerProto] && this.hostname && !this.pathname) {
385 this.pathname = '/';
386 }
387
388 // to support http.request
389 if (this.pathname || this.search) {
390 var p = this.pathname || '';
391 var s = this.search || '';
392 this.path = p + s;
393 }
394
395 // finally, reconstruct the href based on what has been validated.
396 this.href = this.format();
397 return this;
398};
399
400// format a parsed object into a url string
401function urlFormat(obj) {
402 /*
403 * ensure it's an object, and not a string url.
404 * If it's an obj, this is a no-op.
405 * this way, you can call url_format() on strings
406 * to clean up potentially wonky urls.
407 */
408 if (typeof obj === 'string') { obj = urlParse(obj); }
409 if (!(obj instanceof Url)) { return Url.prototype.format.call(obj); }
410 return obj.format();
411}
412
413Url.prototype.format = function () {
414 var auth = this.auth || '';
415 if (auth) {
416 auth = encodeURIComponent(auth);
417 auth = auth.replace(/%3A/i, ':');
418 auth += '@';
419 }
420
421 var protocol = this.protocol || '',
422 pathname = this.pathname || '',
423 hash = this.hash || '',
424 host = false,
425 query = '';
426
427 if (this.host) {
428 host = auth + this.host;
429 } else if (this.hostname) {
430 host = auth + (this.hostname.indexOf(':') === -1 ? this.hostname : '[' + this.hostname + ']');
431 if (this.port) {
432 host += ':' + this.port;
433 }
434 }
435
436 if (this.query && typeof this.query === 'object' && Object.keys(this.query).length) {
437 query = querystring.stringify(this.query, {
438 arrayFormat: 'repeat',
439 addQueryPrefix: false
440 });
441 }
442
443 var search = this.search || (query && ('?' + query)) || '';
444
445 if (protocol && protocol.substr(-1) !== ':') { protocol += ':'; }
446
447 /*
448 * only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
449 * unless they had them to begin with.
450 */
451 if (this.slashes || (!protocol || slashedProtocol[protocol]) && host !== false) {
452 host = '//' + (host || '');
453 if (pathname && pathname.charAt(0) !== '/') { pathname = '/' + pathname; }
454 } else if (!host) {
455 host = '';
456 }
457
458 if (hash && hash.charAt(0) !== '#') { hash = '#' + hash; }
459 if (search && search.charAt(0) !== '?') { search = '?' + search; }
460
461 pathname = pathname.replace(/[?#]/g, function (match) {
462 return encodeURIComponent(match);
463 });
464 search = search.replace('#', '%23');
465
466 return protocol + host + pathname + search + hash;
467};
468
469function urlResolve(source, relative) {
470 return urlParse(source, false, true).resolve(relative);
471}
472
473Url.prototype.resolve = function (relative) {
474 return this.resolveObject(urlParse(relative, false, true)).format();
475};
476
477function urlResolveObject(source, relative) {
478 if (!source) { return relative; }
479 return urlParse(source, false, true).resolveObject(relative);
480}
481
482Url.prototype.resolveObject = function (relative) {
483 if (typeof relative === 'string') {
484 var rel = new Url();
485 rel.parse(relative, false, true);
486 relative = rel;
487 }
488
489 var result = new Url();
490 var tkeys = Object.keys(this);
491 for (var tk = 0; tk < tkeys.length; tk++) {
492 var tkey = tkeys[tk];
493 result[tkey] = this[tkey];
494 }
495
496 /*
497 * hash is always overridden, no matter what.
498 * even href="" will remove it.
499 */
500 result.hash = relative.hash;
501
502 // if the relative url is empty, then there's nothing left to do here.
503 if (relative.href === '') {
504 result.href = result.format();
505 return result;
506 }
507
508 // hrefs like //foo/bar always cut to the protocol.
509 if (relative.slashes && !relative.protocol) {
510 // take everything except the protocol from relative
511 var rkeys = Object.keys(relative);
512 for (var rk = 0; rk < rkeys.length; rk++) {
513 var rkey = rkeys[rk];
514 if (rkey !== 'protocol') { result[rkey] = relative[rkey]; }
515 }
516
517 // urlParse appends trailing / to urls like http://www.example.com
518 if (slashedProtocol[result.protocol] && result.hostname && !result.pathname) {
519 result.pathname = '/';
520 result.path = result.pathname;
521 }
522
523 result.href = result.format();
524 return result;
525 }
526
527 if (relative.protocol && relative.protocol !== result.protocol) {
528 /*
529 * if it's a known url protocol, then changing
530 * the protocol does weird things
531 * first, if it's not file:, then we MUST have a host,
532 * and if there was a path
533 * to begin with, then we MUST have a path.
534 * if it is file:, then the host is dropped,
535 * because that's known to be hostless.
536 * anything else is assumed to be absolute.
537 */
538 if (!slashedProtocol[relative.protocol]) {
539 var keys = Object.keys(relative);
540 for (var v = 0; v < keys.length; v++) {
541 var k = keys[v];
542 result[k] = relative[k];
543 }
544 result.href = result.format();
545 return result;
546 }
547
548 result.protocol = relative.protocol;
549 if (!relative.host && !hostlessProtocol[relative.protocol]) {
550 var relPath = (relative.pathname || '').split('/');
551 while (relPath.length && !(relative.host = relPath.shift())) { }
552 if (!relative.host) { relative.host = ''; }
553 if (!relative.hostname) { relative.hostname = ''; }
554 if (relPath[0] !== '') { relPath.unshift(''); }
555 if (relPath.length < 2) { relPath.unshift(''); }
556 result.pathname = relPath.join('/');
557 } else {
558 result.pathname = relative.pathname;
559 }
560 result.search = relative.search;
561 result.query = relative.query;
562 result.host = relative.host || '';
563 result.auth = relative.auth;
564 result.hostname = relative.hostname || relative.host;
565 result.port = relative.port;
566 // to support http.request
567 if (result.pathname || result.search) {
568 var p = result.pathname || '';
569 var s = result.search || '';
570 result.path = p + s;
571 }
572 result.slashes = result.slashes || relative.slashes;
573 result.href = result.format();
574 return result;
575 }
576
577 var isSourceAbs = result.pathname && result.pathname.charAt(0) === '/',
578 isRelAbs = relative.host || relative.pathname && relative.pathname.charAt(0) === '/',
579 mustEndAbs = isRelAbs || isSourceAbs || (result.host && relative.pathname),
580 removeAllDots = mustEndAbs,
581 srcPath = result.pathname && result.pathname.split('/') || [],
582 relPath = relative.pathname && relative.pathname.split('/') || [],
583 psychotic = result.protocol && !slashedProtocol[result.protocol];
584
585 /*
586 * if the url is a non-slashed url, then relative
587 * links like ../.. should be able
588 * to crawl up to the hostname, as well. This is strange.
589 * result.protocol has already been set by now.
590 * Later on, put the first path part into the host field.
591 */
592 if (psychotic) {
593 result.hostname = '';
594 result.port = null;
595 if (result.host) {
596 if (srcPath[0] === '') { srcPath[0] = result.host; } else { srcPath.unshift(result.host); }
597 }
598 result.host = '';
599 if (relative.protocol) {
600 relative.hostname = null;
601 relative.port = null;
602 if (relative.host) {
603 if (relPath[0] === '') { relPath[0] = relative.host; } else { relPath.unshift(relative.host); }
604 }
605 relative.host = null;
606 }
607 mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
608 }
609
610 if (isRelAbs) {
611 // it's absolute.
612 result.host = relative.host || relative.host === '' ? relative.host : result.host;
613 result.hostname = relative.hostname || relative.hostname === '' ? relative.hostname : result.hostname;
614 result.search = relative.search;
615 result.query = relative.query;
616 srcPath = relPath;
617 // fall through to the dot-handling below.
618 } else if (relPath.length) {
619 /*
620 * it's relative
621 * throw away the existing file, and take the new path instead.
622 */
623 if (!srcPath) { srcPath = []; }
624 srcPath.pop();
625 srcPath = srcPath.concat(relPath);
626 result.search = relative.search;
627 result.query = relative.query;
628 } else if (relative.search != null) {
629 /*
630 * just pull out the search.
631 * like href='?foo'.
632 * Put this after the other two cases because it simplifies the booleans
633 */
634 if (psychotic) {
635 result.host = srcPath.shift();
636 result.hostname = result.host;
637 /*
638 * occationaly the auth can get stuck only in host
639 * this especially happens in cases like
640 * url.resolveObject('mailto:local1@domain1', 'local2@domain2')
641 */
642 var authInHost = result.host && result.host.indexOf('@') > 0 ? result.host.split('@') : false;
643 if (authInHost) {
644 result.auth = authInHost.shift();
645 result.hostname = authInHost.shift();
646 result.host = result.hostname;
647 }
648 }
649 result.search = relative.search;
650 result.query = relative.query;
651 // to support http.request
652 if (result.pathname !== null || result.search !== null) {
653 result.path = (result.pathname ? result.pathname : '') + (result.search ? result.search : '');
654 }
655 result.href = result.format();
656 return result;
657 }
658
659 if (!srcPath.length) {
660 /*
661 * no path at all. easy.
662 * we've already handled the other stuff above.
663 */
664 result.pathname = null;
665 // to support http.request
666 if (result.search) {
667 result.path = '/' + result.search;
668 } else {
669 result.path = null;
670 }
671 result.href = result.format();
672 return result;
673 }
674
675 /*
676 * if a url ENDs in . or .., then it must get a trailing slash.
677 * however, if it ends in anything else non-slashy,
678 * then it must NOT get a trailing slash.
679 */
680 var last = srcPath.slice(-1)[0];
681 var hasTrailingSlash = (result.host || relative.host || srcPath.length > 1) && (last === '.' || last === '..') || last === '';
682
683 /*
684 * strip single dots, resolve double dots to parent dir
685 * if the path tries to go above the root, `up` ends up > 0
686 */
687 var up = 0;
688 for (var i = srcPath.length; i >= 0; i--) {
689 last = srcPath[i];
690 if (last === '.') {
691 srcPath.splice(i, 1);
692 } else if (last === '..') {
693 srcPath.splice(i, 1);
694 up++;
695 } else if (up) {
696 srcPath.splice(i, 1);
697 up--;
698 }
699 }
700
701 // if the path is allowed to go above the root, restore leading ..s
702 if (!mustEndAbs && !removeAllDots) {
703 for (; up--; up) {
704 srcPath.unshift('..');
705 }
706 }
707
708 if (mustEndAbs && srcPath[0] !== '' && (!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
709 srcPath.unshift('');
710 }
711
712 if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
713 srcPath.push('');
714 }
715
716 var isAbsolute = srcPath[0] === '' || (srcPath[0] && srcPath[0].charAt(0) === '/');
717
718 // put the host back
719 if (psychotic) {
720 result.hostname = isAbsolute ? '' : srcPath.length ? srcPath.shift() : '';
721 result.host = result.hostname;
722 /*
723 * occationaly the auth can get stuck only in host
724 * this especially happens in cases like
725 * url.resolveObject('mailto:local1@domain1', 'local2@domain2')
726 */
727 var authInHost = result.host && result.host.indexOf('@') > 0 ? result.host.split('@') : false;
728 if (authInHost) {
729 result.auth = authInHost.shift();
730 result.hostname = authInHost.shift();
731 result.host = result.hostname;
732 }
733 }
734
735 mustEndAbs = mustEndAbs || (result.host && srcPath.length);
736
737 if (mustEndAbs && !isAbsolute) {
738 srcPath.unshift('');
739 }
740
741 if (srcPath.length > 0) {
742 result.pathname = srcPath.join('/');
743 } else {
744 result.pathname = null;
745 result.path = null;
746 }
747
748 // to support request.http
749 if (result.pathname !== null || result.search !== null) {
750 result.path = (result.pathname ? result.pathname : '') + (result.search ? result.search : '');
751 }
752 result.auth = relative.auth || result.auth;
753 result.slashes = result.slashes || relative.slashes;
754 result.href = result.format();
755 return result;
756};
757
758Url.prototype.parseHost = function () {
759 var host = this.host;
760 var port = portPattern.exec(host);
761 if (port) {
762 port = port[0];
763 if (port !== ':') {
764 this.port = port.substr(1);
765 }
766 host = host.substr(0, host.length - port.length);
767 }
768 if (host) { this.hostname = host; }
769};
770
771exports.parse = urlParse;
772exports.resolve = urlResolve;
773exports.resolveObject = urlResolveObject;
774exports.format = urlFormat;
775
776exports.Url = Url;