租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
22 KiB

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