You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

4 years ago
  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. };