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.

uri.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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: Array<string> | null = 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: Array<string> | null = 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({ pathname }: { pathname: string }) {
  104. const contextRootEndIndex = pathname.lastIndexOf('/');
  105. return (
  106. contextRootEndIndex === -1
  107. ? '/'
  108. : pathname.substring(0, contextRootEndIndex + 1));
  109. }
  110. /**
  111. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  112. * specific {@code Object}.
  113. *
  114. * @param {Object} obj - The {@code Object} to turn into URL parameter
  115. * {@code String}s.
  116. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  117. * constructed out of the specified {@code obj}.
  118. */
  119. function _objectToURLParamsArray(obj = {}) {
  120. const params = [];
  121. for (const key in obj) { // eslint-disable-line guard-for-in
  122. try {
  123. params.push(
  124. `${key}=${encodeURIComponent(JSON.stringify(obj[key]))}`);
  125. } catch (e) {
  126. console.warn(`Error encoding ${key}: ${e}`);
  127. }
  128. }
  129. return params;
  130. }
  131. /**
  132. * Parses a specific URI string into an object with the well-known properties of
  133. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  134. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  135. *
  136. * @param {string} str - The URI string to parse.
  137. * @public
  138. * @returns {{
  139. * hash: string,
  140. * host: (string|undefined),
  141. * hostname: (string|undefined),
  142. * pathname: string,
  143. * port: (string|undefined),
  144. * protocol: (string|undefined),
  145. * search: string
  146. * }}
  147. */
  148. export function parseStandardURIString(str: string) {
  149. /* eslint-disable no-param-reassign */
  150. const obj: Object = {
  151. toString: _standardURIToString
  152. };
  153. let regex;
  154. let match: Array<string> | null;
  155. // XXX A URI string as defined by RFC 3986 does not contain any whitespace.
  156. // Usually, a browser will have already encoded any whitespace. In order to
  157. // avoid potential later problems related to whitespace in URI, strip any
  158. // whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
  159. // whitespace so the stripping is deemed safe.
  160. str = str.replace(/\s/g, '');
  161. // protocol
  162. regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}`, 'gi');
  163. match = regex.exec(str);
  164. if (match) {
  165. obj.protocol = match[1].toLowerCase();
  166. str = str.substring(regex.lastIndex);
  167. }
  168. // authority
  169. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  170. match = regex.exec(str);
  171. if (match) {
  172. let authority: string = match[1].substring(/* // */ 2);
  173. str = str.substring(regex.lastIndex);
  174. // userinfo
  175. const userinfoEndIndex = authority.indexOf('@');
  176. if (userinfoEndIndex !== -1) {
  177. authority = authority.substring(userinfoEndIndex + 1);
  178. }
  179. obj.host = authority;
  180. // port
  181. const portBeginIndex = authority.lastIndexOf(':');
  182. if (portBeginIndex !== -1) {
  183. obj.port = authority.substring(portBeginIndex + 1);
  184. authority = authority.substring(0, portBeginIndex);
  185. }
  186. // hostname
  187. obj.hostname = authority;
  188. }
  189. // pathname
  190. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  191. match = regex.exec(str);
  192. let pathname: ?string;
  193. if (match) {
  194. pathname = match[1];
  195. str = str.substring(regex.lastIndex);
  196. }
  197. if (pathname) {
  198. pathname.startsWith('/') || (pathname = `/${pathname}`);
  199. } else {
  200. pathname = '/';
  201. }
  202. obj.pathname = pathname;
  203. // query
  204. if (str.startsWith('?')) {
  205. let hashBeginIndex = str.indexOf('#', 1);
  206. if (hashBeginIndex === -1) {
  207. hashBeginIndex = str.length;
  208. }
  209. obj.search = str.substring(0, hashBeginIndex);
  210. str = str.substring(hashBeginIndex);
  211. } else {
  212. obj.search = ''; // Google Chrome
  213. }
  214. // fragment
  215. obj.hash = str.startsWith('#') ? str : '';
  216. /* eslint-enable no-param-reassign */
  217. return obj;
  218. }
  219. /**
  220. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  221. * (location).
  222. *
  223. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  224. * references a Jitsi Meet resource (location).
  225. * @public
  226. * @returns {{
  227. * room: (string|undefined)
  228. * }}
  229. */
  230. export function parseURIString(uri: ?string) {
  231. if (typeof uri !== 'string') {
  232. return undefined;
  233. }
  234. const obj
  235. = parseStandardURIString(
  236. _fixURIStringHierPart(_fixURIStringScheme(uri)));
  237. // Add the properties that are specific to a Jitsi Meet resource (location)
  238. // such as contextRoot, room:
  239. // contextRoot
  240. obj.contextRoot = getLocationContextRoot(obj);
  241. // The room (name) is the last component of pathname.
  242. const { pathname } = obj;
  243. obj.room = pathname.substring(pathname.lastIndexOf('/') + 1) || undefined;
  244. return obj;
  245. }
  246. /**
  247. * Implements {@code href} and {@code toString} for the {@code Object} returned
  248. * by {@link #parseStandardURIString}.
  249. *
  250. * @param {Object} [thiz] - An {@code Object} returned by
  251. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  252. * function is invoked on such an instance.
  253. * @returns {string}
  254. */
  255. function _standardURIToString(thiz: ?Object) {
  256. // eslint-disable-next-line no-invalid-this
  257. const { hash, host, pathname, protocol, search } = thiz || this;
  258. let str = '';
  259. protocol && (str += protocol);
  260. // TODO userinfo
  261. host && (str += `//${host}`);
  262. str += pathname || '/';
  263. search && (str += search);
  264. hash && (str += hash);
  265. return str;
  266. }
  267. /**
  268. * Attempts to return a {@code String} representation of a specific
  269. * {@code Object} which is supposed to represent a URL. Obviously, if a
  270. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  271. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  272. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  273. * mobile/React Native and Web/React.
  274. *
  275. * @param {Object|string} obj - The URL to return a {@code String}
  276. * representation of.
  277. * @returns {string} - A {@code String} representation of the specified
  278. * {@code obj} which is supposed to represent a URL.
  279. */
  280. export function toURLString(obj: ?(Object | string)): ?string {
  281. let str;
  282. switch (typeof obj) {
  283. case 'object':
  284. if (obj) {
  285. if (obj instanceof URL) {
  286. str = obj.href;
  287. } else {
  288. str = urlObjectToString(obj);
  289. }
  290. }
  291. break;
  292. case 'string':
  293. str = String(obj);
  294. break;
  295. }
  296. return str;
  297. }
  298. /**
  299. * Attempts to return a {@code String} representation of a specific
  300. * {@code Object} similar to the one accepted by the constructor
  301. * of Web's ExternalAPI.
  302. *
  303. * @param {Object} o - The URL to return a {@code String} representation of.
  304. * @returns {string} - A {@code String} representation of the specified
  305. * {@code Object}.
  306. */
  307. export function urlObjectToString(o: Object): ?string {
  308. const url = parseStandardURIString(_fixURIStringScheme(o.url || ''));
  309. // protocol
  310. if (!url.protocol) {
  311. let protocol: ?string = o.protocol || o.scheme;
  312. if (protocol) {
  313. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  314. // do not make a fuss if the final ':' is not there.
  315. protocol.endsWith(':') || (protocol += ':');
  316. url.protocol = protocol;
  317. }
  318. }
  319. // authority & pathname
  320. let { pathname } = url;
  321. if (!url.host) {
  322. // Web's ExternalAPI domain
  323. //
  324. // It may be host/hostname and pathname with the latter denoting the
  325. // tenant.
  326. const domain: ?string = o.domain || o.host || o.hostname;
  327. if (domain) {
  328. const { host, hostname, pathname: contextRoot, port }
  329. = parseStandardURIString(
  330. // XXX The value of domain in supposed to be host/hostname
  331. // and, optionally, pathname. Make sure it is not taken for
  332. // a pathname only.
  333. _fixURIStringScheme(`org.jitsi.meet://${domain}`));
  334. // authority
  335. if (host) {
  336. url.host = host;
  337. url.hostname = hostname;
  338. url.port = port;
  339. }
  340. // pathname
  341. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  342. }
  343. }
  344. // pathname
  345. // Web's ExternalAPI roomName
  346. const room = o.roomName || o.room;
  347. if (room
  348. && (url.pathname.endsWith('/')
  349. || !url.pathname.endsWith(`/${room}`))) {
  350. pathname.endsWith('/') || (pathname += '/');
  351. pathname += room;
  352. }
  353. url.pathname = pathname;
  354. // query/search
  355. // Web's ExternalAPI jwt
  356. const { jwt } = o;
  357. if (jwt) {
  358. let { search } = url;
  359. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  360. search.startsWith('?') || (search = `?${search}`);
  361. search.length === 1 || (search += '&');
  362. search += `jwt=${jwt}`;
  363. url.search = search;
  364. }
  365. }
  366. // fragment/hash
  367. let { hash } = url;
  368. for (const configName of [ 'config', 'interfaceConfig' ]) {
  369. const urlParamsArray
  370. = _objectToURLParamsArray(
  371. o[`${configName}Overwrite`]
  372. || o[configName]
  373. || o[`${configName}Override`]);
  374. if (urlParamsArray.length) {
  375. let urlParamsString
  376. = `${configName}.${urlParamsArray.join(`&${configName}.`)}`;
  377. if (hash.length) {
  378. urlParamsString = `&${urlParamsString}`;
  379. } else {
  380. hash = '#';
  381. }
  382. hash += urlParamsString;
  383. }
  384. }
  385. url.hash = hash;
  386. return url.toString() || undefined;
  387. }