* URL parser. * * @param uri - url * @param path - the request path of the connection * @param loc - An object meant to mimic window.location. * Defaults to window.location. * @public
(uri)
| 2435 | * @public |
| 2436 | */ |
| 2437 | function url(uri) { |
| 2438 | var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; |
| 2439 | var loc = arguments.length > 2 ? arguments[2] : undefined; |
| 2440 | var obj = uri; |
| 2441 | // default to window.location |
| 2442 | loc = loc || typeof location !== "undefined" && location; |
| 2443 | if (null == uri) uri = loc.protocol + "//" + loc.host; |
| 2444 | // relative path support |
| 2445 | if (typeof uri === "string") { |
| 2446 | if ("/" === uri.charAt(0)) { |
| 2447 | if ("/" === uri.charAt(1)) { |
| 2448 | uri = loc.protocol + uri; |
| 2449 | } else { |
| 2450 | uri = loc.host + uri; |
| 2451 | } |
| 2452 | } |
| 2453 | if (!/^(https?|wss?):\/\//.test(uri)) { |
| 2454 | if ("undefined" !== typeof loc) { |
| 2455 | uri = loc.protocol + "//" + uri; |
| 2456 | } else { |
| 2457 | uri = "https://" + uri; |
| 2458 | } |
| 2459 | } |
| 2460 | // parse |
| 2461 | obj = parse(uri); |
| 2462 | } |
| 2463 | // make sure we treat `localhost:80` and `localhost` equally |
| 2464 | if (!obj.port) { |
| 2465 | if (/^(http|ws)$/.test(obj.protocol)) { |
| 2466 | obj.port = "80"; |
| 2467 | } else if (/^(http|ws)s$/.test(obj.protocol)) { |
| 2468 | obj.port = "443"; |
| 2469 | } |
| 2470 | } |
| 2471 | obj.path = obj.path || "/"; |
| 2472 | var ipv6 = obj.host.indexOf(":") !== -1; |
| 2473 | var host = ipv6 ? "[" + obj.host + "]" : obj.host; |
| 2474 | // define unique id |
| 2475 | obj.id = obj.protocol + "://" + host + ":" + obj.port + path; |
| 2476 | // define href |
| 2477 | obj.href = obj.protocol + "://" + host + (loc && loc.port === obj.port ? "" : ":" + obj.port); |
| 2478 | return obj; |
| 2479 | } |
| 2480 | |
| 2481 | var withNativeArrayBuffer = typeof ArrayBuffer === "function"; |
| 2482 | var isView = function isView(obj) { |