* URL parser. * * @param {String} url * @param {Object} An object meant to mimic window.location. * Defaults to window.location. * @api public
(uri, loc)
| 887 | */ |
| 888 | |
| 889 | function url(uri, loc){ |
| 890 | var obj = uri; |
| 891 | |
| 892 | // default to window.location |
| 893 | var loc = loc || global.location; |
| 894 | if (null == uri) uri = loc.protocol + '//' + loc.host; |
| 895 | |
| 896 | // relative path support |
| 897 | if ('string' == typeof uri) { |
| 898 | if ('/' == uri.charAt(0)) { |
| 899 | if ('/' == uri.charAt(1)) { |
| 900 | uri = loc.protocol + uri; |
| 901 | } else { |
| 902 | uri = loc.hostname + uri; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (!/^(https?|wss?):\/\//.test(uri)) { |
| 907 | debug('protocol-less url %s', uri); |
| 908 | if ('undefined' != typeof loc) { |
| 909 | uri = loc.protocol + '//' + uri; |
| 910 | } else { |
| 911 | uri = 'https://' + uri; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // parse |
| 916 | debug('parse %s', uri); |
| 917 | obj = parseuri(uri); |
| 918 | } |
| 919 | |
| 920 | // make sure we treat `localhost:80` and `localhost` equally |
| 921 | if (!obj.port) { |
| 922 | if (/^(http|ws)$/.test(obj.protocol)) { |
| 923 | obj.port = '80'; |
| 924 | } |
| 925 | else if (/^(http|ws)s$/.test(obj.protocol)) { |
| 926 | obj.port = '443'; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | obj.path = obj.path || '/'; |
| 931 | |
| 932 | // define unique id |
| 933 | obj.id = obj.protocol + '://' + obj.host + ':' + obj.port; |
| 934 | // define href |
| 935 | obj.href = obj.protocol + '://' + obj.host + (loc && loc.port == obj.port ? '' : (':' + obj.port)); |
| 936 | |
| 937 | return obj; |
| 938 | } |
| 939 | |
| 940 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 941 |