url-parse.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.URLParse = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. (function (global){
  3. 'use strict';
  4. var required = require('requires-port')
  5. , qs = require('querystringify')
  6. , protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i
  7. , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//;
  8. /**
  9. * These are the parse rules for the URL parser, it informs the parser
  10. * about:
  11. *
  12. * 0. The char it Needs to parse, if it's a string it should be done using
  13. * indexOf, RegExp using exec and NaN means set as current value.
  14. * 1. The property we should set when parsing this value.
  15. * 2. Indication if it's backwards or forward parsing, when set as number it's
  16. * the value of extra chars that should be split off.
  17. * 3. Inherit from location if non existing in the parser.
  18. * 4. `toLowerCase` the resulting value.
  19. */
  20. var rules = [
  21. ['#', 'hash'], // Extract from the back.
  22. ['?', 'query'], // Extract from the back.
  23. ['/', 'pathname'], // Extract from the back.
  24. ['@', 'auth', 1], // Extract from the front.
  25. [NaN, 'host', undefined, 1, 1], // Set left over value.
  26. [/:(\d+)$/, 'port', undefined, 1], // RegExp the back.
  27. [NaN, 'hostname', undefined, 1, 1] // Set left over.
  28. ];
  29. /**
  30. * These properties should not be copied or inherited from. This is only needed
  31. * for all non blob URL's as a blob URL does not include a hash, only the
  32. * origin.
  33. *
  34. * @type {Object}
  35. * @private
  36. */
  37. var ignore = { hash: 1, query: 1 };
  38. /**
  39. * The location object differs when your code is loaded through a normal page,
  40. * Worker or through a worker using a blob. And with the blobble begins the
  41. * trouble as the location object will contain the URL of the blob, not the
  42. * location of the page where our code is loaded in. The actual origin is
  43. * encoded in the `pathname` so we can thankfully generate a good "default"
  44. * location from it so we can generate proper relative URL's again.
  45. *
  46. * @param {Object|String} loc Optional default location object.
  47. * @returns {Object} lolcation object.
  48. * @api public
  49. */
  50. function lolcation(loc) {
  51. var location = global && global.location || {};
  52. loc = loc || location;
  53. var finaldestination = {}
  54. , type = typeof loc
  55. , key;
  56. if ('blob:' === loc.protocol) {
  57. finaldestination = new URL(unescape(loc.pathname), {});
  58. } else if ('string' === type) {
  59. finaldestination = new URL(loc, {});
  60. for (key in ignore) delete finaldestination[key];
  61. } else if ('object' === type) {
  62. for (key in loc) {
  63. if (key in ignore) continue;
  64. finaldestination[key] = loc[key];
  65. }
  66. if (finaldestination.slashes === undefined) {
  67. finaldestination.slashes = slashes.test(loc.href);
  68. }
  69. }
  70. return finaldestination;
  71. }
  72. /**
  73. * @typedef ProtocolExtract
  74. * @type Object
  75. * @property {String} protocol Protocol matched in the URL, in lowercase.
  76. * @property {Boolean} slashes `true` if protocol is followed by "//", else `false`.
  77. * @property {String} rest Rest of the URL that is not part of the protocol.
  78. */
  79. /**
  80. * Extract protocol information from a URL with/without double slash ("//").
  81. *
  82. * @param {String} address URL we want to extract from.
  83. * @return {ProtocolExtract} Extracted information.
  84. * @api private
  85. */
  86. function extractProtocol(address) {
  87. var match = protocolre.exec(address);
  88. return {
  89. protocol: match[1] ? match[1].toLowerCase() : '',
  90. slashes: !!match[2],
  91. rest: match[3]
  92. };
  93. }
  94. /**
  95. * Resolve a relative URL pathname against a base URL pathname.
  96. *
  97. * @param {String} relative Pathname of the relative URL.
  98. * @param {String} base Pathname of the base URL.
  99. * @return {String} Resolved pathname.
  100. * @api private
  101. */
  102. function resolve(relative, base) {
  103. var path = (base || '/').split('/').slice(0, -1).concat(relative.split('/'))
  104. , i = path.length
  105. , last = path[i - 1]
  106. , unshift = false
  107. , up = 0;
  108. while (i--) {
  109. if (path[i] === '.') {
  110. path.splice(i, 1);
  111. } else if (path[i] === '..') {
  112. path.splice(i, 1);
  113. up++;
  114. } else if (up) {
  115. if (i === 0) unshift = true;
  116. path.splice(i, 1);
  117. up--;
  118. }
  119. }
  120. if (unshift) path.unshift('');
  121. if (last === '.' || last === '..') path.push('');
  122. return path.join('/');
  123. }
  124. /**
  125. * The actual URL instance. Instead of returning an object we've opted-in to
  126. * create an actual constructor as it's much more memory efficient and
  127. * faster and it pleases my OCD.
  128. *
  129. * @constructor
  130. * @param {String} address URL we want to parse.
  131. * @param {Object|String} location Location defaults for relative paths.
  132. * @param {Boolean|Function} parser Parser for the query string.
  133. * @api public
  134. */
  135. function URL(address, location, parser) {
  136. if (!(this instanceof URL)) {
  137. return new URL(address, location, parser);
  138. }
  139. var relative, extracted, parse, instruction, index, key
  140. , instructions = rules.slice()
  141. , type = typeof location
  142. , url = this
  143. , i = 0;
  144. //
  145. // The following if statements allows this module two have compatibility with
  146. // 2 different API:
  147. //
  148. // 1. Node.js's `url.parse` api which accepts a URL, boolean as arguments
  149. // where the boolean indicates that the query string should also be parsed.
  150. //
  151. // 2. The `URL` interface of the browser which accepts a URL, object as
  152. // arguments. The supplied object will be used as default values / fall-back
  153. // for relative paths.
  154. //
  155. if ('object' !== type && 'string' !== type) {
  156. parser = location;
  157. location = null;
  158. }
  159. if (parser && 'function' !== typeof parser) parser = qs.parse;
  160. location = lolcation(location);
  161. //
  162. // Extract protocol information before running the instructions.
  163. //
  164. extracted = extractProtocol(address || '');
  165. relative = !extracted.protocol && !extracted.slashes;
  166. url.slashes = extracted.slashes || relative && location.slashes;
  167. url.protocol = extracted.protocol || location.protocol || '';
  168. address = extracted.rest;
  169. //
  170. // When the authority component is absent the URL starts with a path
  171. // component.
  172. //
  173. if (!extracted.slashes) instructions[2] = [/(.*)/, 'pathname'];
  174. for (; i < instructions.length; i++) {
  175. instruction = instructions[i];
  176. parse = instruction[0];
  177. key = instruction[1];
  178. if (parse !== parse) {
  179. url[key] = address;
  180. } else if ('string' === typeof parse) {
  181. if (~(index = address.indexOf(parse))) {
  182. if ('number' === typeof instruction[2]) {
  183. url[key] = address.slice(0, index);
  184. address = address.slice(index + instruction[2]);
  185. } else {
  186. url[key] = address.slice(index);
  187. address = address.slice(0, index);
  188. }
  189. }
  190. } else if ((index = parse.exec(address))) {
  191. url[key] = index[1];
  192. address = address.slice(0, index.index);
  193. }
  194. url[key] = url[key] || (
  195. relative && instruction[3] ? location[key] || '' : ''
  196. );
  197. //
  198. // Hostname, host and protocol should be lowercased so they can be used to
  199. // create a proper `origin`.
  200. //
  201. if (instruction[4]) url[key] = url[key].toLowerCase();
  202. }
  203. //
  204. // Also parse the supplied query string in to an object. If we're supplied
  205. // with a custom parser as function use that instead of the default build-in
  206. // parser.
  207. //
  208. if (parser) url.query = parser(url.query);
  209. //
  210. // If the URL is relative, resolve the pathname against the base URL.
  211. //
  212. if (
  213. relative
  214. && location.slashes
  215. && url.pathname.charAt(0) !== '/'
  216. && (url.pathname !== '' || location.pathname !== '')
  217. ) {
  218. url.pathname = resolve(url.pathname, location.pathname);
  219. }
  220. //
  221. // We should not add port numbers if they are already the default port number
  222. // for a given protocol. As the host also contains the port number we're going
  223. // override it with the hostname which contains no port number.
  224. //
  225. if (!required(url.port, url.protocol)) {
  226. url.host = url.hostname;
  227. url.port = '';
  228. }
  229. //
  230. // Parse down the `auth` for the username and password.
  231. //
  232. url.username = url.password = '';
  233. if (url.auth) {
  234. instruction = url.auth.split(':');
  235. url.username = instruction[0] || '';
  236. url.password = instruction[1] || '';
  237. }
  238. url.origin = url.protocol && url.host && url.protocol !== 'file:'
  239. ? url.protocol +'//'+ url.host
  240. : 'null';
  241. //
  242. // The href is just the compiled result.
  243. //
  244. url.href = url.toString();
  245. }
  246. /**
  247. * This is convenience method for changing properties in the URL instance to
  248. * insure that they all propagate correctly.
  249. *
  250. * @param {String} part Property we need to adjust.
  251. * @param {Mixed} value The newly assigned value.
  252. * @param {Boolean|Function} fn When setting the query, it will be the function
  253. * used to parse the query.
  254. * When setting the protocol, double slash will be
  255. * removed from the final url if it is true.
  256. * @returns {URL}
  257. * @api public
  258. */
  259. function set(part, value, fn) {
  260. var url = this;
  261. switch (part) {
  262. case 'query':
  263. if ('string' === typeof value && value.length) {
  264. value = (fn || qs.parse)(value);
  265. }
  266. url[part] = value;
  267. break;
  268. case 'port':
  269. url[part] = value;
  270. if (!required(value, url.protocol)) {
  271. url.host = url.hostname;
  272. url[part] = '';
  273. } else if (value) {
  274. url.host = url.hostname +':'+ value;
  275. }
  276. break;
  277. case 'hostname':
  278. url[part] = value;
  279. if (url.port) value += ':'+ url.port;
  280. url.host = value;
  281. break;
  282. case 'host':
  283. url[part] = value;
  284. if (/:\d+$/.test(value)) {
  285. value = value.split(':');
  286. url.port = value.pop();
  287. url.hostname = value.join(':');
  288. } else {
  289. url.hostname = value;
  290. url.port = '';
  291. }
  292. break;
  293. case 'protocol':
  294. url.protocol = value.toLowerCase();
  295. url.slashes = !fn;
  296. break;
  297. case 'pathname':
  298. case 'hash':
  299. if (value) {
  300. var char = part === 'pathname' ? '/' : '#';
  301. url[part] = value.charAt(0) !== char ? char + value : value;
  302. } else {
  303. url[part] = value;
  304. }
  305. break;
  306. default:
  307. url[part] = value;
  308. }
  309. for (var i = 0; i < rules.length; i++) {
  310. var ins = rules[i];
  311. if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase();
  312. }
  313. url.origin = url.protocol && url.host && url.protocol !== 'file:'
  314. ? url.protocol +'//'+ url.host
  315. : 'null';
  316. url.href = url.toString();
  317. return url;
  318. }
  319. /**
  320. * Transform the properties back in to a valid and full URL string.
  321. *
  322. * @param {Function} stringify Optional query stringify function.
  323. * @returns {String}
  324. * @api public
  325. */
  326. function toString(stringify) {
  327. if (!stringify || 'function' !== typeof stringify) stringify = qs.stringify;
  328. var query
  329. , url = this
  330. , protocol = url.protocol;
  331. if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';
  332. var result = protocol + (url.slashes ? '//' : '');
  333. if (url.username) {
  334. result += url.username;
  335. if (url.password) result += ':'+ url.password;
  336. result += '@';
  337. }
  338. result += url.host + url.pathname;
  339. query = 'object' === typeof url.query ? stringify(url.query) : url.query;
  340. if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;
  341. if (url.hash) result += url.hash;
  342. return result;
  343. }
  344. URL.prototype = { set: set, toString: toString };
  345. //
  346. // Expose the URL parser and some additional properties that might be useful for
  347. // others or testing.
  348. //
  349. URL.extractProtocol = extractProtocol;
  350. URL.location = lolcation;
  351. URL.qs = qs;
  352. module.exports = URL;
  353. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  354. },{"querystringify":2,"requires-port":3}],2:[function(require,module,exports){
  355. 'use strict';
  356. var has = Object.prototype.hasOwnProperty;
  357. /**
  358. * Decode a URI encoded string.
  359. *
  360. * @param {String} input The URI encoded string.
  361. * @returns {String} The decoded string.
  362. * @api private
  363. */
  364. function decode(input) {
  365. return decodeURIComponent(input.replace(/\+/g, ' '));
  366. }
  367. /**
  368. * Simple query string parser.
  369. *
  370. * @param {String} query The query string that needs to be parsed.
  371. * @returns {Object}
  372. * @api public
  373. */
  374. function querystring(query) {
  375. var parser = /([^=?&]+)=?([^&]*)/g
  376. , result = {}
  377. , part;
  378. while (part = parser.exec(query)) {
  379. var key = decode(part[1])
  380. , value = decode(part[2]);
  381. //
  382. // Prevent overriding of existing properties. This ensures that build-in
  383. // methods like `toString` or __proto__ are not overriden by malicious
  384. // querystrings.
  385. //
  386. if (key in result) continue;
  387. result[key] = value;
  388. }
  389. return result;
  390. }
  391. /**
  392. * Transform a query string to an object.
  393. *
  394. * @param {Object} obj Object that should be transformed.
  395. * @param {String} prefix Optional prefix.
  396. * @returns {String}
  397. * @api public
  398. */
  399. function querystringify(obj, prefix) {
  400. prefix = prefix || '';
  401. var pairs = [];
  402. //
  403. // Optionally prefix with a '?' if needed
  404. //
  405. if ('string' !== typeof prefix) prefix = '?';
  406. for (var key in obj) {
  407. if (has.call(obj, key)) {
  408. pairs.push(encodeURIComponent(key) +'='+ encodeURIComponent(obj[key]));
  409. }
  410. }
  411. return pairs.length ? prefix + pairs.join('&') : '';
  412. }
  413. //
  414. // Expose the module.
  415. //
  416. exports.stringify = querystringify;
  417. exports.parse = querystring;
  418. },{}],3:[function(require,module,exports){
  419. 'use strict';
  420. /**
  421. * Check if we're required to add a port number.
  422. *
  423. * @see https://url.spec.whatwg.org/#default-port
  424. * @param {Number|String} port Port number we need to check
  425. * @param {String} protocol Protocol we need to check against.
  426. * @returns {Boolean} Is it a default port for the given protocol
  427. * @api private
  428. */
  429. module.exports = function required(port, protocol) {
  430. protocol = protocol.split(':')[0];
  431. port = +port;
  432. if (!port) return false;
  433. switch (protocol) {
  434. case 'http':
  435. case 'ws':
  436. return port !== 80;
  437. case 'https':
  438. case 'wss':
  439. return port !== 443;
  440. case 'ftp':
  441. return port !== 21;
  442. case 'gopher':
  443. return port !== 70;
  444. case 'file':
  445. return false;
  446. }
  447. return port !== 0;
  448. };
  449. },{}]},{},[1])(1)
  450. });