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