* URL parser. * * @param {String} url * @param {Object} An object meant to mimic window.location. * Defaults to window.location. * @api public
(uri, loc)
| 195 | */ |
| 196 | |
| 197 | function url(uri, loc) { |
| 198 | var obj = uri; |
| 199 | |
| 200 | // default to window.location |
| 201 | loc = loc || global.location; |
| 202 | if (null == uri) uri = loc.protocol + '//' + loc.host; |
| 203 | |
| 204 | // relative path support |
| 205 | if ('string' === typeof uri) { |
| 206 | if ('/' === uri.charAt(0)) { |
| 207 | if ('/' === uri.charAt(1)) { |
| 208 | uri = loc.protocol + uri; |
| 209 | } else { |
| 210 | uri = loc.host + uri; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!/^(https?|wss?):\/\//.test(uri)) { |
| 215 | debug('protocol-less url %s', uri); |
| 216 | if ('undefined' !== typeof loc) { |
| 217 | uri = loc.protocol + '//' + uri; |
| 218 | } else { |
| 219 | uri = 'https://' + uri; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // parse |
| 224 | debug('parse %s', uri); |
| 225 | obj = parseuri(uri); |
| 226 | } |
| 227 | |
| 228 | // make sure we treat `localhost:80` and `localhost` equally |
| 229 | if (!obj.port) { |
| 230 | if (/^(http|ws)$/.test(obj.protocol)) { |
| 231 | obj.port = '80'; |
| 232 | } else if (/^(http|ws)s$/.test(obj.protocol)) { |
| 233 | obj.port = '443'; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | obj.path = obj.path || '/'; |
| 238 | |
| 239 | var ipv6 = obj.host.indexOf(':') !== -1; |
| 240 | var host = ipv6 ? '[' + obj.host + ']' : obj.host; |
| 241 | |
| 242 | // define unique id |
| 243 | obj.id = obj.protocol + '://' + host + ':' + obj.port; |
| 244 | // define href |
| 245 | obj.href = obj.protocol + '://' + host + (loc && loc.port === obj.port ? '' : ':' + obj.port); |
| 246 | |
| 247 | return obj; |
| 248 | } |
| 249 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 250 | |
| 251 | /***/ }, |