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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // @flow
  2. /**
  3. * The app linking scheme.
  4. * TODO: This should be read from the manifest files later.
  5. */
  6. export const APP_LINK_SCHEME = 'org.jitsi.meet:';
  7. /**
  8. * The {@link RegExp} pattern of the authority of a URI.
  9. *
  10. * @private
  11. * @type {string}
  12. */
  13. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  14. /**
  15. * The {@link RegExp} pattern of the path of a URI.
  16. *
  17. * @private
  18. * @type {string}
  19. */
  20. const _URI_PATH_PATTERN = '([^?#]*)';
  21. /**
  22. * The {@link RegExp} pattern of the protocol of a URI.
  23. *
  24. * FIXME: The URL class exposed by JavaScript will not include the colon in
  25. * the protocol field. Also in other places (at the time of this writing:
  26. * the DeepLinkingMobilePage.js) the APP_LINK_SCHEME does not include
  27. * the double dots, so things are inconsistent.
  28. *
  29. * @type {string}
  30. */
  31. export const URI_PROTOCOL_PATTERN = '([a-z][a-z0-9\\.\\+-]*:)';
  32. /**
  33. * Fixes the hier-part of a specific URI (string) so that the URI is well-known.
  34. * For example, certain Jitsi Meet deployments are not conventional but it is
  35. * possible to translate their URLs into conventional.
  36. *
  37. * @param {string} uri - The URI (string) to fix the hier-part of.
  38. * @private
  39. * @returns {string}
  40. */
  41. function _fixURIStringHierPart(uri) {
  42. // Rewrite the specified URL in order to handle special cases such as
  43. // hipchat.com and enso.me which do not follow the common pattern of most
  44. // Jitsi Meet deployments.
  45. // hipchat.com
  46. let regex
  47. = new RegExp(
  48. `^${URI_PROTOCOL_PATTERN}//hipchat\\.com/video/call/`,
  49. 'gi');
  50. let match: Array<string> | null = regex.exec(uri);
  51. if (!match) {
  52. // enso.me
  53. regex
  54. = new RegExp(
  55. `^${URI_PROTOCOL_PATTERN}//enso\\.me/(?:call|meeting)/`,
  56. 'gi');
  57. match = regex.exec(uri);
  58. }
  59. if (match) {
  60. /* eslint-disable no-param-reassign, prefer-template */
  61. uri
  62. = match[1] /* protocol */
  63. + '//enso.hipchat.me/'
  64. + uri.substring(regex.lastIndex); /* room (name) */
  65. /* eslint-enable no-param-reassign, prefer-template */
  66. }
  67. return uri;
  68. }
  69. /**
  70. * Fixes the scheme part of a specific URI (string) so that it contains a
  71. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  72. * app-specific URI scheme in addition to Universal Links. The app-specific
  73. * scheme may precede or replace the well-known scheme. In such a case, dealing
  74. * with the app-specific scheme only complicates the logic and it is simpler to
  75. * get rid of it (by translating the app-specific scheme into a well-known
  76. * scheme).
  77. *
  78. * @param {string} uri - The URI (string) to fix the scheme of.
  79. * @private
  80. * @returns {string}
  81. */
  82. function _fixURIStringScheme(uri: string) {
  83. const regex = new RegExp(`^${URI_PROTOCOL_PATTERN}+`, 'gi');
  84. const match: Array<string> | null = regex.exec(uri);
  85. if (match) {
  86. // As an implementation convenience, pick up the last scheme and make
  87. // sure that it is a well-known one.
  88. let protocol = match[match.length - 1].toLowerCase();
  89. if (protocol !== 'http:' && protocol !== 'https:') {
  90. protocol = 'https:';
  91. }
  92. /* eslint-disable no-param-reassign */
  93. uri = uri.substring(regex.lastIndex);
  94. if (uri.startsWith('//')) {
  95. // The specified URL was not a room name only, it contained an
  96. // authority.
  97. uri = protocol + uri;
  98. }
  99. /* eslint-enable no-param-reassign */
  100. }
  101. return uri;
  102. }
  103. /**
  104. * Gets the (Web application) context root defined by a specific location (URI).
  105. *
  106. * @param {Object} location - The location (URI) which defines the (Web
  107. * application) context root.
  108. * @public
  109. * @returns {string} - The (Web application) context root defined by the
  110. * specified {@code location} (URI).
  111. */
  112. export function getLocationContextRoot({ pathname }: { pathname: string }) {
  113. const contextRootEndIndex = pathname.lastIndexOf('/');
  114. return (
  115. contextRootEndIndex === -1
  116. ? '/'
  117. : pathname.substring(0, contextRootEndIndex + 1));
  118. }
  119. /**
  120. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  121. * specific {@code Object}.
  122. *
  123. * @param {Object} obj - The {@code Object} to turn into URL parameter
  124. * {@code String}s.
  125. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  126. * constructed out of the specified {@code obj}.
  127. */
  128. function _objectToURLParamsArray(obj = {}) {
  129. const params = [];
  130. for (const key in obj) { // eslint-disable-line guard-for-in
  131. try {
  132. params.push(
  133. `${key}=${encodeURIComponent(JSON.stringify(obj[key]))}`);
  134. } catch (e) {
  135. console.warn(`Error encoding ${key}: ${e}`);
  136. }
  137. }
  138. return params;
  139. }
  140. /**
  141. * Parses a specific URI string into an object with the well-known properties of
  142. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  143. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  144. *
  145. * @param {string} str - The URI string to parse.
  146. * @public
  147. * @returns {{
  148. * hash: string,
  149. * host: (string|undefined),
  150. * hostname: (string|undefined),
  151. * pathname: string,
  152. * port: (string|undefined),
  153. * protocol: (string|undefined),
  154. * search: string
  155. * }}
  156. */
  157. export function parseStandardURIString(str: string) {
  158. /* eslint-disable no-param-reassign */
  159. const obj: Object = {
  160. toString: _standardURIToString
  161. };
  162. let regex;
  163. let match: Array<string> | null;
  164. // XXX A URI string as defined by RFC 3986 does not contain any whitespace.
  165. // Usually, a browser will have already encoded any whitespace. In order to
  166. // avoid potential later problems related to whitespace in URI, strip any
  167. // whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
  168. // whitespace so the stripping is deemed safe.
  169. str = str.replace(/\s/g, '');
  170. // protocol
  171. regex = new RegExp(`^${URI_PROTOCOL_PATTERN}`, 'gi');
  172. match = regex.exec(str);
  173. if (match) {
  174. obj.protocol = match[1].toLowerCase();
  175. str = str.substring(regex.lastIndex);
  176. }
  177. // authority
  178. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  179. match = regex.exec(str);
  180. if (match) {
  181. let authority: string = match[1].substring(/* // */ 2);
  182. str = str.substring(regex.lastIndex);
  183. // userinfo
  184. const userinfoEndIndex = authority.indexOf('@');
  185. if (userinfoEndIndex !== -1) {
  186. authority = authority.substring(userinfoEndIndex + 1);
  187. }
  188. obj.host = authority;
  189. // port
  190. const portBeginIndex = authority.lastIndexOf(':');
  191. if (portBeginIndex !== -1) {
  192. obj.port = authority.substring(portBeginIndex + 1);
  193. authority = authority.substring(0, portBeginIndex);
  194. }
  195. // hostname
  196. obj.hostname = authority;
  197. }
  198. // pathname
  199. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  200. match = regex.exec(str);
  201. let pathname: ?string;
  202. if (match) {
  203. pathname = match[1];
  204. str = str.substring(regex.lastIndex);
  205. }
  206. if (pathname) {
  207. pathname.startsWith('/') || (pathname = `/${pathname}`);
  208. } else {
  209. pathname = '/';
  210. }
  211. obj.pathname = pathname;
  212. // query
  213. if (str.startsWith('?')) {
  214. let hashBeginIndex = str.indexOf('#', 1);
  215. if (hashBeginIndex === -1) {
  216. hashBeginIndex = str.length;
  217. }
  218. obj.search = str.substring(0, hashBeginIndex);
  219. str = str.substring(hashBeginIndex);
  220. } else {
  221. obj.search = ''; // Google Chrome
  222. }
  223. // fragment
  224. obj.hash = str.startsWith('#') ? str : '';
  225. /* eslint-enable no-param-reassign */
  226. return obj;
  227. }
  228. /**
  229. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  230. * (location).
  231. *
  232. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  233. * references a Jitsi Meet resource (location).
  234. * @public
  235. * @returns {{
  236. * room: (string|undefined)
  237. * }}
  238. */
  239. export function parseURIString(uri: ?string) {
  240. if (typeof uri !== 'string') {
  241. return undefined;
  242. }
  243. const obj
  244. = parseStandardURIString(
  245. _fixURIStringHierPart(_fixURIStringScheme(uri)));
  246. // Add the properties that are specific to a Jitsi Meet resource (location)
  247. // such as contextRoot, room:
  248. // contextRoot
  249. obj.contextRoot = getLocationContextRoot(obj);
  250. // The room (name) is the last component of pathname.
  251. const { pathname } = obj;
  252. obj.room = pathname.substring(pathname.lastIndexOf('/') + 1) || undefined;
  253. return obj;
  254. }
  255. /**
  256. * Implements {@code href} and {@code toString} for the {@code Object} returned
  257. * by {@link #parseStandardURIString}.
  258. *
  259. * @param {Object} [thiz] - An {@code Object} returned by
  260. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  261. * function is invoked on such an instance.
  262. * @returns {string}
  263. */
  264. function _standardURIToString(thiz: ?Object) {
  265. // eslint-disable-next-line no-invalid-this
  266. const { hash, host, pathname, protocol, search } = thiz || this;
  267. let str = '';
  268. protocol && (str += protocol);
  269. // TODO userinfo
  270. host && (str += `//${host}`);
  271. str += pathname || '/';
  272. search && (str += search);
  273. hash && (str += hash);
  274. return str;
  275. }
  276. /**
  277. * Attempts to return a {@code String} representation of a specific
  278. * {@code Object} which is supposed to represent a URL. Obviously, if a
  279. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  280. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  281. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  282. * mobile/React Native and Web/React.
  283. *
  284. * @param {Object|string} obj - The URL to return a {@code String}
  285. * representation of.
  286. * @returns {string} - A {@code String} representation of the specified
  287. * {@code obj} which is supposed to represent a URL.
  288. */
  289. export function toURLString(obj: ?(Object | string)): ?string {
  290. let str;
  291. switch (typeof obj) {
  292. case 'object':
  293. if (obj) {
  294. if (obj instanceof URL) {
  295. str = obj.href;
  296. } else {
  297. str = urlObjectToString(obj);
  298. }
  299. }
  300. break;
  301. case 'string':
  302. str = String(obj);
  303. break;
  304. }
  305. return str;
  306. }
  307. /**
  308. * Attempts to return a {@code String} representation of a specific
  309. * {@code Object} similar to the one accepted by the constructor
  310. * of Web's ExternalAPI.
  311. *
  312. * @param {Object} o - The URL to return a {@code String} representation of.
  313. * @returns {string} - A {@code String} representation of the specified
  314. * {@code Object}.
  315. */
  316. export function urlObjectToString(o: Object): ?string {
  317. const url = parseStandardURIString(_fixURIStringScheme(o.url || ''));
  318. // protocol
  319. if (!url.protocol) {
  320. let protocol: ?string = o.protocol || o.scheme;
  321. if (protocol) {
  322. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  323. // do not make a fuss if the final ':' is not there.
  324. protocol.endsWith(':') || (protocol += ':');
  325. url.protocol = protocol;
  326. }
  327. }
  328. // authority & pathname
  329. let { pathname } = url;
  330. if (!url.host) {
  331. // Web's ExternalAPI domain
  332. //
  333. // It may be host/hostname and pathname with the latter denoting the
  334. // tenant.
  335. const domain: ?string = o.domain || o.host || o.hostname;
  336. if (domain) {
  337. const { host, hostname, pathname: contextRoot, port }
  338. = parseStandardURIString(
  339. // XXX The value of domain in supposed to be host/hostname
  340. // and, optionally, pathname. Make sure it is not taken for
  341. // a pathname only.
  342. _fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
  343. // authority
  344. if (host) {
  345. url.host = host;
  346. url.hostname = hostname;
  347. url.port = port;
  348. }
  349. // pathname
  350. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  351. }
  352. }
  353. // pathname
  354. // Web's ExternalAPI roomName
  355. const room = o.roomName || o.room;
  356. if (room
  357. && (url.pathname.endsWith('/')
  358. || !url.pathname.endsWith(`/${room}`))) {
  359. pathname.endsWith('/') || (pathname += '/');
  360. pathname += room;
  361. }
  362. url.pathname = pathname;
  363. // query/search
  364. // Web's ExternalAPI jwt
  365. const { jwt } = o;
  366. if (jwt) {
  367. let { search } = url;
  368. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  369. search.startsWith('?') || (search = `?${search}`);
  370. search.length === 1 || (search += '&');
  371. search += `jwt=${jwt}`;
  372. url.search = search;
  373. }
  374. }
  375. // fragment/hash
  376. let { hash } = url;
  377. for (const configName of [ 'config', 'interfaceConfig' ]) {
  378. const urlParamsArray
  379. = _objectToURLParamsArray(
  380. o[`${configName}Overwrite`]
  381. || o[configName]
  382. || o[`${configName}Override`]);
  383. if (urlParamsArray.length) {
  384. let urlParamsString
  385. = `${configName}.${urlParamsArray.join(`&${configName}.`)}`;
  386. if (hash.length) {
  387. urlParamsString = `&${urlParamsString}`;
  388. } else {
  389. hash = '#';
  390. }
  391. hash += urlParamsString;
  392. }
  393. }
  394. url.hash = hash;
  395. return url.toString() || undefined;
  396. }