index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Parses an URI
  3. *
  4. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  5. * @api private
  6. */
  7. var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  8. var parts = [
  9. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  10. ];
  11. module.exports = function parseuri(str) {
  12. var src = str,
  13. b = str.indexOf('['),
  14. e = str.indexOf(']');
  15. if (b != -1 && e != -1) {
  16. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  17. }
  18. var m = re.exec(str || ''),
  19. uri = {},
  20. i = 14;
  21. while (i--) {
  22. uri[parts[i]] = m[i] || '';
  23. }
  24. if (b != -1 && e != -1) {
  25. uri.source = src;
  26. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  27. uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
  28. uri.ipv6uri = true;
  29. }
  30. return uri;
  31. };