智慧水务管理系统 - 精河县供水工程综合管理平台

uri-utils.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { isDigitChar, isAsciiLetterChar, isAlphaNumericOrMarkChar, isUrlSuffixAllowedSpecialChar, isUrlSuffixNotAllowedAsFinalChar, } from '../char-utils';
  2. import { tldRegex } from './known-tlds';
  3. /**
  4. * Regular expression to match an http:// or https:// scheme.
  5. */
  6. export var httpSchemeRe = /https?:\/\//i;
  7. /**
  8. * Regular expression to match an http:// or https:// scheme as the prefix of
  9. * a string.
  10. */
  11. export var httpSchemePrefixRe = new RegExp('^' + httpSchemeRe.source, 'i');
  12. /**
  13. * A regular expression used to determine the schemes we should not autolink
  14. */
  15. export var invalidSchemeRe = /^(javascript|vbscript):/i;
  16. // A regular expression used to determine if the URL is a scheme match (such as
  17. // 'http://google.com', and as opposed to a "TLD match"). This regular
  18. // expression is used to parse out the host along with if the URL has an
  19. // authority component (i.e. '//')
  20. //
  21. // Capturing groups:
  22. // 1. '//' if the URL has an authority component, empty string otherwise
  23. // 2. The host (if one exists). Ex: 'google.com'
  24. //
  25. // See https://www.rfc-editor.org/rfc/rfc3986#appendix-A for terminology
  26. export var schemeUrlRe = /^[A-Za-z][-.+A-Za-z0-9]*:(\/\/)?([^:/]*)/;
  27. // A regular expression used to determine if the URL is a TLD match (such as
  28. // 'google.com', and as opposed to a "scheme match"). This regular
  29. // expression is used to help parse out the TLD (top-level domain) of the host.
  30. //
  31. // See https://www.rfc-editor.org/rfc/rfc3986#appendix-A for terminology
  32. export var tldUrlHostRe = /^(?:\/\/)?([^/#?:]+)/; // optionally prefixed with protocol-relative '//' chars
  33. /**
  34. * Determines if the given character code represents a character that may start
  35. * a scheme (ex: the 'h' in 'http')
  36. */
  37. export var isSchemeStartChar = isAsciiLetterChar; // Equivalent to checking the RegExp `/[A-Za-z]/`, but aliased for clarity and maintainability
  38. /**
  39. * Determines if the given character is a valid character in a scheme (such as
  40. * 'http' or 'ssh+git'), but only after the start char (which is handled by
  41. * {@link isSchemeStartChar}.
  42. */
  43. export function isSchemeChar(charCode) {
  44. return (isAsciiLetterChar(charCode) ||
  45. isDigitChar(charCode) ||
  46. charCode === 43 /* Char.Plus */ || // '+'
  47. charCode === 45 /* Char.Dash */ || // '-'
  48. charCode === 46 /* Char.Dot */ // '.'
  49. );
  50. }
  51. /**
  52. * Determines if the character can begin a domain label, which must be an
  53. * alphanumeric character and not an underscore or dash.
  54. *
  55. * A domain label is a segment of a hostname such as subdomain.google.com.
  56. */
  57. export var isDomainLabelStartChar = isAlphaNumericOrMarkChar; // alias function for clarity
  58. /**
  59. * Determines if the character is part of a domain label (but not a domain label
  60. * start character).
  61. *
  62. * A domain label is a segment of a hostname such as subdomain.google.com.
  63. */
  64. export function isDomainLabelChar(charCode) {
  65. return charCode === 95 /* Char.Underscore */ || isDomainLabelStartChar(charCode);
  66. }
  67. /**
  68. * Determines if the character is a path character ("pchar") as defined by
  69. * https://tools.ietf.org/html/rfc3986#appendix-A
  70. *
  71. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  72. *
  73. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  74. * pct-encoded = "%" HEXDIG HEXDIG
  75. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  76. * / "*" / "+" / "," / ";" / "="
  77. *
  78. * Note that this implementation doesn't follow the spec exactly, but rather
  79. * follows URL path characters found out in the wild (spec might be out of date?)
  80. */
  81. export function isPathChar(charCode) {
  82. return (isAlphaNumericOrMarkChar(charCode) ||
  83. isUrlSuffixAllowedSpecialChar(charCode) ||
  84. isUrlSuffixNotAllowedAsFinalChar(charCode) // characters in addition to those allowed by isUrlSuffixAllowedSpecialChar()
  85. );
  86. }
  87. /**
  88. * Determines if the character given may begin the "URL Suffix" section of a
  89. * URI (i.e. the path, query, or hash section). These are the '/', '?' and '#'
  90. * characters.
  91. *
  92. * See https://tools.ietf.org/html/rfc3986#appendix-A
  93. */
  94. export function isUrlSuffixStartChar(charCode) {
  95. return (charCode === 47 /* Char.Slash */ || // '/'
  96. charCode === 63 /* Char.Question */ || // '?'
  97. charCode === 35 /* Char.NumberSign */ // '#'
  98. );
  99. }
  100. /**
  101. * Determines if the top-level domain (TLD) read in the host is a known TLD.
  102. *
  103. * Example: 'com' would be a known TLD (for a host of 'google.com'), but
  104. * 'local' would not (for a domain name of 'my-computer.local').
  105. */
  106. export function isKnownTld(tld) {
  107. return tldRegex.test(tld.toLowerCase()); // make sure the tld is lowercase for the regex
  108. }
  109. /**
  110. * Determines if the given `url` is a valid scheme-prefixed URL.
  111. */
  112. export function isValidSchemeUrl(url) {
  113. // If the scheme is 'javascript:' or 'vbscript:', these link
  114. // types can be dangerous. Don't link them.
  115. if (invalidSchemeRe.test(url)) {
  116. return false;
  117. }
  118. var schemeMatch = url.match(schemeUrlRe);
  119. if (!schemeMatch) {
  120. return false;
  121. }
  122. var isAuthorityMatch = !!schemeMatch[1];
  123. var host = schemeMatch[2];
  124. if (isAuthorityMatch) {
  125. // Any match that has an authority ('//' chars) after the scheme is
  126. // valid, such as 'http://anything'
  127. return true;
  128. }
  129. // If there's no authority ('//' chars), check that we have a hostname
  130. // that looks valid.
  131. //
  132. // The host must contain at least one '.' char and have a domain label
  133. // with at least one letter to be considered valid.
  134. //
  135. // Accept:
  136. // - git:domain.com (scheme followed by a host
  137. // Do not accept:
  138. // - git:something ('something' doesn't look like a host)
  139. // - version:1.0 ('1.0' doesn't look like a host)
  140. if (host.indexOf('.') === -1 || !/[A-Za-z]/.test(host)) {
  141. // `letterRe` RegExp checks for a letter anywhere in the host string
  142. return false;
  143. }
  144. return true;
  145. }
  146. /**
  147. * Determines if the given `url` is a match with a valid TLD.
  148. */
  149. export function isValidTldMatch(url) {
  150. // TLD URL such as 'google.com', we need to confirm that we have a valid
  151. // top-level domain
  152. var tldUrlHostMatch = url.match(tldUrlHostRe);
  153. if (!tldUrlHostMatch) {
  154. // At this point, if the URL didn't match our TLD re, it must be invalid
  155. // (highly unlikely to happen, but just in case)
  156. return false;
  157. }
  158. var host = tldUrlHostMatch[0];
  159. var hostLabels = host.split('.');
  160. if (hostLabels.length < 2) {
  161. // 0 or 1 host label, there's no TLD. Ex: 'localhost'
  162. return false;
  163. }
  164. var tld = hostLabels[hostLabels.length - 1];
  165. if (!isKnownTld(tld)) {
  166. return false;
  167. }
  168. // TODO: Implement these conditions for TLD matcher:
  169. // (
  170. // this.longestDomainLabelLength <= 63 &&
  171. // this.domainNameLength <= 255
  172. // );
  173. return true;
  174. }
  175. // Regular expression to confirm a valid IPv4 address (ex: '192.168.0.1')
  176. // TODO: encode this into the state machine so that we don't need to run this
  177. // regexp separately to confirm the match
  178. var ipV4Re = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
  179. // Regular expression used to split the IPv4 address itself from any port/path/query/hash
  180. var ipV4PartRe = /[:/?#]/;
  181. /**
  182. * Determines if the given URL is a valid IPv4-prefixed URL.
  183. */
  184. export function isValidIpV4Address(url) {
  185. // Grab just the IP address
  186. var ipV4Part = url.split(ipV4PartRe, 1)[0]; // only 1 result needed
  187. return ipV4Re.test(ipV4Part);
  188. }
  189. //# sourceMappingURL=uri-utils.js.map