Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

uri.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* @flow */
  2. /**
  3. * The {@link RegExp} pattern of the authority of a URI.
  4. *
  5. * @private
  6. * @type {string}
  7. */
  8. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  9. /**
  10. * The {@link RegExp} pattern of the path of a URI.
  11. *
  12. * @private
  13. * @type {string}
  14. */
  15. const _URI_PATH_PATTERN = '([^?#]*)';
  16. /**
  17. * The {@link RegExp} pattern of the protocol of a URI.
  18. *
  19. * @private
  20. * @type {string}
  21. */
  22. const _URI_PROTOCOL_PATTERN = '([a-z][a-z0-9\\.\\+-]*:)';
  23. /**
  24. * Fixes the hier-part of a specific URI (string) so that the URI is well-known.
  25. * For example, certain Jitsi Meet deployments are not conventional but it is
  26. * possible to translate their URLs into conventional.
  27. *
  28. * @param {string} uri - The URI (string) to fix the hier-part of.
  29. * @private
  30. * @returns {string}
  31. */
  32. function _fixURIStringHierPart(uri) {
  33. // Rewrite the specified URL in order to handle special cases such as
  34. // hipchat.com and enso.me which do not follow the common pattern of most
  35. // Jitsi Meet deployments.
  36. // hipchat.com
  37. let regex
  38. = new RegExp(
  39. `^${_URI_PROTOCOL_PATTERN}//hipchat\\.com/video/call/`,
  40. 'gi');
  41. let match = regex.exec(uri);
  42. if (!match) {
  43. // enso.me
  44. regex
  45. = new RegExp(
  46. `^${_URI_PROTOCOL_PATTERN}//enso\\.me/(?:call|meeting)/`,
  47. 'gi');
  48. match = regex.exec(uri);
  49. }
  50. if (match) {
  51. /* eslint-disable no-param-reassign, prefer-template */
  52. uri
  53. = match[1] /* protocol */
  54. + '//enso.hipchat.me/'
  55. + uri.substring(regex.lastIndex); /* room (name) */
  56. /* eslint-enable no-param-reassign, prefer-template */
  57. }
  58. return uri;
  59. }
  60. /**
  61. * Fixes the scheme part of a specific URI (string) so that it contains a
  62. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  63. * app-specific URI scheme in addition to Universal Links. The app-specific
  64. * scheme may precede or replace the well-known scheme. In such a case, dealing
  65. * with the app-specific scheme only complicates the logic and it is simpler to
  66. * get rid of it (by translating the app-specific scheme into a well-known
  67. * scheme).
  68. *
  69. * @param {string} uri - The URI (string) to fix the scheme of.
  70. * @private
  71. * @returns {string}
  72. */
  73. function _fixURIStringScheme(uri: string) {
  74. const regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}+`, 'gi');
  75. const match = regex.exec(uri);
  76. if (match) {
  77. // As an implementation convenience, pick up the last scheme and make
  78. // sure that it is a well-known one.
  79. let protocol = match[match.length - 1].toLowerCase();
  80. if (protocol !== 'http:' && protocol !== 'https:') {
  81. protocol = 'https:';
  82. }
  83. /* eslint-disable no-param-reassign */
  84. uri = uri.substring(regex.lastIndex);
  85. if (uri.startsWith('//')) {
  86. // The specified URL was not a room name only, it contained an
  87. // authority.
  88. uri = protocol + uri;
  89. }
  90. /* eslint-enable no-param-reassign */
  91. }
  92. return uri;
  93. }
  94. /**
  95. * Gets the (Web application) context root defined by a specific location (URI).
  96. *
  97. * @param {Object} location - The location (URI) which defines the (Web
  98. * application) context root.
  99. * @public
  100. * @returns {string} - The (Web application) context root defined by the
  101. * specified {@code location} (URI).
  102. */
  103. export function getLocationContextRoot(location: Object) {
  104. const pathname = location.pathname;
  105. const contextRootEndIndex = pathname.lastIndexOf('/');
  106. return (
  107. contextRootEndIndex === -1
  108. ? '/'
  109. : pathname.substring(0, contextRootEndIndex + 1));
  110. }
  111. /**
  112. * Parses a specific URI string into an object with the well-known properties of
  113. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  114. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  115. *
  116. * @param {string} str - The URI string to parse.
  117. * @public
  118. * @returns {{
  119. * hash: string,
  120. * host: (string|undefined),
  121. * hostname: (string|undefined),
  122. * pathname: string,
  123. * port: (string|undefined),
  124. * protocol: (string|undefined),
  125. * search: string
  126. * }}
  127. */
  128. export function parseStandardURIString(str: string) {
  129. /* eslint-disable no-param-reassign */
  130. const obj = {};
  131. let regex;
  132. let match;
  133. // protocol
  134. regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}`, 'gi');
  135. match = regex.exec(str);
  136. if (match) {
  137. obj.protocol = match[1].toLowerCase();
  138. str = str.substring(regex.lastIndex);
  139. }
  140. // authority
  141. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  142. match = regex.exec(str);
  143. if (match) {
  144. let authority = match[1].substring(/* // */ 2);
  145. str = str.substring(regex.lastIndex);
  146. // userinfo
  147. const userinfoEndIndex = authority.indexOf('@');
  148. if (userinfoEndIndex !== -1) {
  149. authority = authority.substring(userinfoEndIndex + 1);
  150. }
  151. obj.host = authority;
  152. // port
  153. const portBeginIndex = authority.lastIndexOf(':');
  154. if (portBeginIndex !== -1) {
  155. obj.port = authority.substring(portBeginIndex + 1);
  156. authority = authority.substring(0, portBeginIndex);
  157. }
  158. // hostname
  159. obj.hostname = authority;
  160. }
  161. // pathname
  162. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  163. match = regex.exec(str);
  164. let pathname;
  165. if (match) {
  166. pathname = match[1];
  167. str = str.substring(regex.lastIndex);
  168. }
  169. if (pathname) {
  170. if (!pathname.startsWith('/')) {
  171. pathname = `/${pathname}`;
  172. }
  173. } else {
  174. pathname = '/';
  175. }
  176. obj.pathname = pathname;
  177. // query
  178. if (str.startsWith('?')) {
  179. let hashBeginIndex = str.indexOf('#', 1);
  180. if (hashBeginIndex === -1) {
  181. hashBeginIndex = str.length;
  182. }
  183. obj.search = str.substring(0, hashBeginIndex);
  184. str = str.substring(hashBeginIndex);
  185. } else {
  186. obj.search = ''; // Google Chrome
  187. }
  188. // fragment
  189. obj.hash = str.startsWith('#') ? str : '';
  190. /* eslint-enable no-param-reassign */
  191. return obj;
  192. }
  193. /**
  194. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  195. * (location).
  196. *
  197. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  198. * references a Jitsi Meet resource (location).
  199. * @public
  200. * @returns {{
  201. * room: (string|undefined)
  202. * }}
  203. */
  204. export function parseURIString(uri: ?string) {
  205. if (typeof uri !== 'string') {
  206. return undefined;
  207. }
  208. const obj
  209. = parseStandardURIString(
  210. _fixURIStringHierPart(_fixURIStringScheme(uri)));
  211. // Add the properties that are specific to a Jitsi Meet resource (location)
  212. // such as contextRoot, room:
  213. // contextRoot
  214. obj.contextRoot = getLocationContextRoot(obj);
  215. // The room (name) is the last component of pathname.
  216. const { pathname } = obj;
  217. obj.room = pathname.substring(pathname.lastIndexOf('/') + 1) || undefined;
  218. return obj;
  219. }