uri.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. const Assert = require('@hapi/hoek/lib/assert');
  3. const EscapeRegex = require('@hapi/hoek/lib/escapeRegex');
  4. const internals = {};
  5. internals.generate = function () {
  6. const rfc3986 = {};
  7. const hexDigit = '\\dA-Fa-f'; // HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
  8. const hexDigitOnly = '[' + hexDigit + ']';
  9. const unreserved = '\\w-\\.~'; // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  10. const subDelims = '!\\$&\'\\(\\)\\*\\+,;='; // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  11. const pctEncoded = '%' + hexDigit; // pct-encoded = "%" HEXDIG HEXDIG
  12. const pchar = unreserved + pctEncoded + subDelims + ':@'; // pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  13. const pcharOnly = '[' + pchar + ']';
  14. const decOctect = '(?:0{0,2}\\d|0?[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])'; // dec-octet = DIGIT / %x31-39 DIGIT / "1" 2DIGIT / "2" %x30-34 DIGIT / "25" %x30-35 ; 0-9 / 10-99 / 100-199 / 200-249 / 250-255
  15. rfc3986.ipv4address = '(?:' + decOctect + '\\.){3}' + decOctect; // IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  16. /*
  17. h16 = 1*4HEXDIG ; 16 bits of address represented in hexadecimal
  18. ls32 = ( h16 ":" h16 ) / IPv4address ; least-significant 32 bits of address
  19. IPv6address = 6( h16 ":" ) ls32
  20. / "::" 5( h16 ":" ) ls32
  21. / [ h16 ] "::" 4( h16 ":" ) ls32
  22. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  23. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  24. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  25. / [ *4( h16 ":" ) h16 ] "::" ls32
  26. / [ *5( h16 ":" ) h16 ] "::" h16
  27. / [ *6( h16 ":" ) h16 ] "::"
  28. */
  29. const h16 = hexDigitOnly + '{1,4}';
  30. const ls32 = '(?:' + h16 + ':' + h16 + '|' + rfc3986.ipv4address + ')';
  31. const IPv6SixHex = '(?:' + h16 + ':){6}' + ls32;
  32. const IPv6FiveHex = '::(?:' + h16 + ':){5}' + ls32;
  33. const IPv6FourHex = '(?:' + h16 + ')?::(?:' + h16 + ':){4}' + ls32;
  34. const IPv6ThreeHex = '(?:(?:' + h16 + ':){0,1}' + h16 + ')?::(?:' + h16 + ':){3}' + ls32;
  35. const IPv6TwoHex = '(?:(?:' + h16 + ':){0,2}' + h16 + ')?::(?:' + h16 + ':){2}' + ls32;
  36. const IPv6OneHex = '(?:(?:' + h16 + ':){0,3}' + h16 + ')?::' + h16 + ':' + ls32;
  37. const IPv6NoneHex = '(?:(?:' + h16 + ':){0,4}' + h16 + ')?::' + ls32;
  38. const IPv6NoneHex2 = '(?:(?:' + h16 + ':){0,5}' + h16 + ')?::' + h16;
  39. const IPv6NoneHex3 = '(?:(?:' + h16 + ':){0,6}' + h16 + ')?::';
  40. rfc3986.ipv4Cidr = '(?:\\d|[1-2]\\d|3[0-2])'; // IPv4 cidr = DIGIT / %x31-32 DIGIT / "3" %x30-32 ; 0-9 / 10-29 / 30-32
  41. rfc3986.ipv6Cidr = '(?:0{0,2}\\d|0?[1-9]\\d|1[01]\\d|12[0-8])'; // IPv6 cidr = DIGIT / %x31-39 DIGIT / "1" %x0-1 DIGIT / "12" %x0-8; 0-9 / 10-99 / 100-119 / 120-128
  42. rfc3986.ipv6address = '(?:' + IPv6SixHex + '|' + IPv6FiveHex + '|' + IPv6FourHex + '|' + IPv6ThreeHex + '|' + IPv6TwoHex + '|' + IPv6OneHex + '|' + IPv6NoneHex + '|' + IPv6NoneHex2 + '|' + IPv6NoneHex3 + ')';
  43. rfc3986.ipvFuture = 'v' + hexDigitOnly + '+\\.[' + unreserved + subDelims + ':]+'; // IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  44. rfc3986.scheme = '[a-zA-Z][a-zA-Z\\d+-\\.]*'; // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  45. rfc3986.schemeRegex = new RegExp(rfc3986.scheme);
  46. const userinfo = '[' + unreserved + pctEncoded + subDelims + ':]*'; // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  47. const IPLiteral = '\\[(?:' + rfc3986.ipv6address + '|' + rfc3986.ipvFuture + ')\\]'; // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  48. const regName = '[' + unreserved + pctEncoded + subDelims + ']{1,255}'; // reg-name = *( unreserved / pct-encoded / sub-delims )
  49. const host = '(?:' + IPLiteral + '|' + rfc3986.ipv4address + '|' + regName + ')'; // host = IP-literal / IPv4address / reg-name
  50. const port = '\\d*'; // port = *DIGIT
  51. const authority = '(?:' + userinfo + '@)?' + host + '(?::' + port + ')?'; // authority = [ userinfo "@" ] host [ ":" port ]
  52. const authorityCapture = '(?:' + userinfo + '@)?(' + host + ')(?::' + port + ')?';
  53. /*
  54. segment = *pchar
  55. segment-nz = 1*pchar
  56. path = path-abempty ; begins with "/" '|' is empty
  57. / path-absolute ; begins with "/" but not "//"
  58. / path-noscheme ; begins with a non-colon segment
  59. / path-rootless ; begins with a segment
  60. / path-empty ; zero characters
  61. path-abempty = *( "/" segment )
  62. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  63. path-rootless = segment-nz *( "/" segment )
  64. */
  65. const segment = pcharOnly + '*';
  66. const segmentNz = pcharOnly + '+';
  67. const segmentNzNc = '[' + unreserved + pctEncoded + subDelims + '@' + ']+';
  68. const pathEmpty = '';
  69. const pathAbEmpty = '(?:\\/' + segment + ')*';
  70. const pathAbsolute = '\\/(?:' + segmentNz + pathAbEmpty + ')?';
  71. const pathRootless = segmentNz + pathAbEmpty;
  72. const pathNoScheme = segmentNzNc + pathAbEmpty;
  73. const pathAbNoAuthority = '(?:\\/\\/\\/' + segment + pathAbEmpty + ')'; // Used by file:///
  74. // hier-part = "//" authority path
  75. rfc3986.hierPart = '(?:' + '(?:\\/\\/' + authority + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathRootless + '|' + pathAbNoAuthority + ')';
  76. rfc3986.hierPartCapture = '(?:' + '(?:\\/\\/' + authorityCapture + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathRootless + ')';
  77. // relative-part = "//" authority path-abempty / path-absolute / path-noscheme / path-empty
  78. rfc3986.relativeRef = '(?:' + '(?:\\/\\/' + authority + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathNoScheme + '|' + pathEmpty + ')';
  79. rfc3986.relativeRefCapture = '(?:' + '(?:\\/\\/' + authorityCapture + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathNoScheme + '|' + pathEmpty + ')';
  80. // query = *( pchar / "/" / "?" )
  81. // query = *( pchar / "[" / "]" / "/" / "?" )
  82. rfc3986.query = '[' + pchar + '\\/\\?]*(?=#|$)'; //Finish matching either at the fragment part '|' end of the line.
  83. rfc3986.queryWithSquareBrackets = '[' + pchar + '\\[\\]\\/\\?]*(?=#|$)';
  84. // fragment = *( pchar / "/" / "?" )
  85. rfc3986.fragment = '[' + pchar + '\\/\\?]*';
  86. return rfc3986;
  87. };
  88. internals.rfc3986 = internals.generate();
  89. exports.ip = {
  90. v4Cidr: internals.rfc3986.ipv4Cidr,
  91. v6Cidr: internals.rfc3986.ipv6Cidr,
  92. ipv4: internals.rfc3986.ipv4address,
  93. ipv6: internals.rfc3986.ipv6address,
  94. ipvfuture: internals.rfc3986.ipvFuture
  95. };
  96. internals.createRegex = function (options) {
  97. const rfc = internals.rfc3986;
  98. // Construct expression
  99. const query = options.allowQuerySquareBrackets ? rfc.queryWithSquareBrackets : rfc.query;
  100. const suffix = '(?:\\?' + query + ')?' + '(?:#' + rfc.fragment + ')?';
  101. // relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  102. const relative = options.domain ? rfc.relativeRefCapture : rfc.relativeRef;
  103. if (options.relativeOnly) {
  104. return internals.wrap(relative + suffix);
  105. }
  106. // Custom schemes
  107. let customScheme = '';
  108. if (options.scheme) {
  109. Assert(options.scheme instanceof RegExp || typeof options.scheme === 'string' || Array.isArray(options.scheme), 'scheme must be a RegExp, String, or Array');
  110. const schemes = [].concat(options.scheme);
  111. Assert(schemes.length >= 1, 'scheme must have at least 1 scheme specified');
  112. // Flatten the array into a string to be used to match the schemes
  113. const selections = [];
  114. for (let i = 0; i < schemes.length; ++i) {
  115. const scheme = schemes[i];
  116. Assert(scheme instanceof RegExp || typeof scheme === 'string', 'scheme at position ' + i + ' must be a RegExp or String');
  117. if (scheme instanceof RegExp) {
  118. selections.push(scheme.source.toString());
  119. }
  120. else {
  121. Assert(rfc.schemeRegex.test(scheme), 'scheme at position ' + i + ' must be a valid scheme');
  122. selections.push(EscapeRegex(scheme));
  123. }
  124. }
  125. customScheme = selections.join('|');
  126. }
  127. // URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  128. const scheme = customScheme ? '(?:' + customScheme + ')' : rfc.scheme;
  129. const absolute = '(?:' + scheme + ':' + (options.domain ? rfc.hierPartCapture : rfc.hierPart) + ')';
  130. const prefix = options.allowRelative ? '(?:' + absolute + '|' + relative + ')' : absolute;
  131. return internals.wrap(prefix + suffix, customScheme);
  132. };
  133. internals.wrap = function (raw, scheme) {
  134. raw = `(?=.)(?!https?\:/(?:$|[^/]))(?!https?\:///)(?!https?\:[^/])${raw}`; // Require at least one character and explicitly forbid 'http:/' or HTTP with empty domain
  135. return {
  136. raw,
  137. regex: new RegExp(`^${raw}$`),
  138. scheme
  139. };
  140. };
  141. internals.uriRegex = internals.createRegex({});
  142. exports.regex = function (options = {}) {
  143. if (options.scheme ||
  144. options.allowRelative ||
  145. options.relativeOnly ||
  146. options.allowQuerySquareBrackets ||
  147. options.domain) {
  148. return internals.createRegex(options);
  149. }
  150. return internals.uriRegex;
  151. };