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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // @flow
  2. import { parseURLParams } from './parseURLParams';
  3. import { normalizeNFKC } from './strings';
  4. /**
  5. * The app linking scheme.
  6. * TODO: This should be read from the manifest files later.
  7. */
  8. export const APP_LINK_SCHEME = 'org.jitsi.meet:';
  9. /**
  10. * A list of characters to be excluded/removed from the room component/segment
  11. * of a conference/meeting URI/URL. The list is based on RFC 3986 and the jxmpp
  12. * library utilized by jicofo.
  13. */
  14. const _ROOM_EXCLUDE_PATTERN = '[\\:\\?#\\[\\]@!$&\'()*+,;=></"]';
  15. /**
  16. * The {@link RegExp} pattern of the authority of a URI.
  17. *
  18. * @private
  19. * @type {string}
  20. */
  21. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  22. /**
  23. * The {@link RegExp} pattern of the path of a URI.
  24. *
  25. * @private
  26. * @type {string}
  27. */
  28. const _URI_PATH_PATTERN = '([^?#]*)';
  29. /**
  30. * The {@link RegExp} pattern of the protocol of a URI.
  31. *
  32. * FIXME: The URL class exposed by JavaScript will not include the colon in
  33. * the protocol field. Also in other places (at the time of this writing:
  34. * the DeepLinkingMobilePage.js) the APP_LINK_SCHEME does not include
  35. * the double dots, so things are inconsistent.
  36. *
  37. * @type {string}
  38. */
  39. export const URI_PROTOCOL_PATTERN = '^([a-z][a-z0-9\\.\\+-]*:)';
  40. /**
  41. * Excludes/removes certain characters from a specific room (name) which are
  42. * incompatible with Jitsi Meet on the client and/or server sides.
  43. *
  44. * @param {?string} room - The room (name) to fix.
  45. * @private
  46. * @returns {?string}
  47. */
  48. function _fixRoom(room: ?string) {
  49. return room
  50. ? room.replace(new RegExp(_ROOM_EXCLUDE_PATTERN, 'g'), '')
  51. : room;
  52. }
  53. /**
  54. * Fixes the scheme part of a specific URI (string) so that it contains a
  55. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  56. * app-specific URI scheme in addition to Universal Links. The app-specific
  57. * scheme may precede or replace the well-known scheme. In such a case, dealing
  58. * with the app-specific scheme only complicates the logic and it is simpler to
  59. * get rid of it (by translating the app-specific scheme into a well-known
  60. * scheme).
  61. *
  62. * @param {string} uri - The URI (string) to fix the scheme of.
  63. * @private
  64. * @returns {string}
  65. */
  66. function _fixURIStringScheme(uri: string) {
  67. const regex = new RegExp(`${URI_PROTOCOL_PATTERN}+`, 'gi');
  68. const match: Array<string> | null = regex.exec(uri);
  69. if (match) {
  70. // As an implementation convenience, pick up the last scheme and make
  71. // sure that it is a well-known one.
  72. let protocol = match[match.length - 1].toLowerCase();
  73. if (protocol !== 'http:' && protocol !== 'https:') {
  74. protocol = 'https:';
  75. }
  76. /* eslint-disable no-param-reassign */
  77. uri = uri.substring(regex.lastIndex);
  78. if (uri.startsWith('//')) {
  79. // The specified URL was not a room name only, it contained an
  80. // authority.
  81. uri = protocol + uri;
  82. }
  83. /* eslint-enable no-param-reassign */
  84. }
  85. return uri;
  86. }
  87. /**
  88. * Converts a path to a backend-safe format, by splitting the path '/' processing each part.
  89. * Properly lowercased and url encoded.
  90. *
  91. * @param {string?} path - The path to convert.
  92. * @returns {string?}
  93. */
  94. export function getBackendSafePath(path: ?string): ?string {
  95. if (!path) {
  96. return path;
  97. }
  98. return path
  99. .split('/')
  100. .map(getBackendSafeRoomName)
  101. .join('/');
  102. }
  103. /**
  104. * Converts a room name to a backend-safe format. Properly lowercased and url encoded.
  105. *
  106. * @param {string?} room - The room name to convert.
  107. * @returns {string?}
  108. */
  109. export function getBackendSafeRoomName(room: ?string): ?string {
  110. if (!room) {
  111. return room;
  112. }
  113. /* eslint-disable no-param-reassign */
  114. try {
  115. // We do not know if we get an already encoded string at this point
  116. // as different platforms do it differently, but we need a decoded one
  117. // for sure. However since decoding a non-encoded string is a noop, we're safe
  118. // doing it here.
  119. room = decodeURIComponent(room);
  120. } catch (e) {
  121. // This can happen though if we get an unencoded string and it contains
  122. // some characters that look like an encoded entity, but it's not.
  123. // But in this case we're fine goin on...
  124. }
  125. // Normalize the character set.
  126. room = normalizeNFKC(room);
  127. // Only decoded and normalized strings can be lowercased properly.
  128. room = room.toLowerCase();
  129. // But we still need to (re)encode it.
  130. room = encodeURIComponent(room);
  131. /* eslint-enable no-param-reassign */
  132. // Unfortunately we still need to lowercase it, because encoding a string will
  133. // add some uppercase characters, but some backend services
  134. // expect it to be full lowercase. However lowercasing an encoded string
  135. // doesn't change the string value.
  136. return room.toLowerCase();
  137. }
  138. /**
  139. * Gets the (Web application) context root defined by a specific location (URI).
  140. *
  141. * @param {Object} location - The location (URI) which defines the (Web
  142. * application) context root.
  143. * @public
  144. * @returns {string} - The (Web application) context root defined by the
  145. * specified {@code location} (URI).
  146. */
  147. export function getLocationContextRoot({ pathname }: { pathname: string }) {
  148. const contextRootEndIndex = pathname.lastIndexOf('/');
  149. return (
  150. contextRootEndIndex === -1
  151. ? '/'
  152. : pathname.substring(0, contextRootEndIndex + 1));
  153. }
  154. /**
  155. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  156. * specific {@code Object}.
  157. *
  158. * @param {Object} obj - The {@code Object} to turn into URL parameter
  159. * {@code String}s.
  160. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  161. * constructed out of the specified {@code obj}.
  162. */
  163. function _objectToURLParamsArray(obj = {}) {
  164. const params = [];
  165. for (const key in obj) { // eslint-disable-line guard-for-in
  166. try {
  167. params.push(
  168. `${key}=${encodeURIComponent(JSON.stringify(obj[key]))}`);
  169. } catch (e) {
  170. console.warn(`Error encoding ${key}: ${e}`);
  171. }
  172. }
  173. return params;
  174. }
  175. /**
  176. * Parses a specific URI string into an object with the well-known properties of
  177. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  178. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  179. *
  180. * @param {string} str - The URI string to parse.
  181. * @public
  182. * @returns {{
  183. * hash: string,
  184. * host: (string|undefined),
  185. * hostname: (string|undefined),
  186. * pathname: string,
  187. * port: (string|undefined),
  188. * protocol: (string|undefined),
  189. * search: string
  190. * }}
  191. */
  192. export function parseStandardURIString(str: string) {
  193. /* eslint-disable no-param-reassign */
  194. const obj: Object = {
  195. toString: _standardURIToString
  196. };
  197. let regex;
  198. let match: Array<string> | null;
  199. // XXX A URI string as defined by RFC 3986 does not contain any whitespace.
  200. // Usually, a browser will have already encoded any whitespace. In order to
  201. // avoid potential later problems related to whitespace in URI, strip any
  202. // whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
  203. // whitespace so the stripping is deemed safe.
  204. str = str.replace(/\s/g, '');
  205. // protocol
  206. regex = new RegExp(URI_PROTOCOL_PATTERN, 'gi');
  207. match = regex.exec(str);
  208. if (match) {
  209. obj.protocol = match[1].toLowerCase();
  210. str = str.substring(regex.lastIndex);
  211. }
  212. // authority
  213. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  214. match = regex.exec(str);
  215. if (match) {
  216. let authority: string = match[1].substring(/* // */ 2);
  217. str = str.substring(regex.lastIndex);
  218. // userinfo
  219. const userinfoEndIndex = authority.indexOf('@');
  220. if (userinfoEndIndex !== -1) {
  221. authority = authority.substring(userinfoEndIndex + 1);
  222. }
  223. obj.host = authority;
  224. // port
  225. const portBeginIndex = authority.lastIndexOf(':');
  226. if (portBeginIndex !== -1) {
  227. obj.port = authority.substring(portBeginIndex + 1);
  228. authority = authority.substring(0, portBeginIndex);
  229. }
  230. // hostname
  231. obj.hostname = authority;
  232. }
  233. // pathname
  234. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  235. match = regex.exec(str);
  236. let pathname: ?string;
  237. if (match) {
  238. pathname = match[1];
  239. str = str.substring(regex.lastIndex);
  240. }
  241. if (pathname) {
  242. pathname.startsWith('/') || (pathname = `/${pathname}`);
  243. } else {
  244. pathname = '/';
  245. }
  246. obj.pathname = pathname;
  247. // query
  248. if (str.startsWith('?')) {
  249. let hashBeginIndex = str.indexOf('#', 1);
  250. if (hashBeginIndex === -1) {
  251. hashBeginIndex = str.length;
  252. }
  253. obj.search = str.substring(0, hashBeginIndex);
  254. str = str.substring(hashBeginIndex);
  255. } else {
  256. obj.search = ''; // Google Chrome
  257. }
  258. // fragment
  259. obj.hash = str.startsWith('#') ? str : '';
  260. /* eslint-enable no-param-reassign */
  261. return obj;
  262. }
  263. /**
  264. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  265. * (location).
  266. *
  267. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  268. * references a Jitsi Meet resource (location).
  269. * @public
  270. * @returns {{
  271. * contextRoot: string,
  272. * hash: string,
  273. * host: string,
  274. * hostname: string,
  275. * pathname: string,
  276. * port: string,
  277. * protocol: string,
  278. * room: (string|undefined),
  279. * search: string
  280. * }}
  281. */
  282. export function parseURIString(uri: ?string) {
  283. if (typeof uri !== 'string') {
  284. return undefined;
  285. }
  286. const obj = parseStandardURIString(_fixURIStringScheme(uri));
  287. // Add the properties that are specific to a Jitsi Meet resource (location)
  288. // such as contextRoot, room:
  289. // contextRoot
  290. obj.contextRoot = getLocationContextRoot(obj);
  291. // The room (name) is the last component/segment of pathname.
  292. const { pathname } = obj;
  293. // XXX While the components/segments of pathname are URI encoded, Jitsi Meet
  294. // on the client and/or server sides still don't support certain characters.
  295. const contextRootEndIndex = pathname.lastIndexOf('/');
  296. let room = pathname.substring(contextRootEndIndex + 1) || undefined;
  297. if (room) {
  298. const fixedRoom = _fixRoom(room);
  299. if (fixedRoom !== room) {
  300. room = fixedRoom;
  301. // XXX Drive fixedRoom into pathname (because room is derived from
  302. // pathname).
  303. obj.pathname
  304. = pathname.substring(0, contextRootEndIndex + 1) + (room || '');
  305. }
  306. }
  307. obj.room = room;
  308. if (contextRootEndIndex > 1) {
  309. // The part of the pathname from the beginning to the room name is the tenant.
  310. obj.tenant = pathname.substring(1, contextRootEndIndex);
  311. }
  312. return obj;
  313. }
  314. /**
  315. * Implements {@code href} and {@code toString} for the {@code Object} returned
  316. * by {@link #parseStandardURIString}.
  317. *
  318. * @param {Object} [thiz] - An {@code Object} returned by
  319. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  320. * function is invoked on such an instance.
  321. * @returns {string}
  322. */
  323. function _standardURIToString(thiz: ?Object) {
  324. // eslint-disable-next-line no-invalid-this
  325. const { hash, host, pathname, protocol, search } = thiz || this;
  326. let str = '';
  327. protocol && (str += protocol);
  328. // TODO userinfo
  329. host && (str += `//${host}`);
  330. str += pathname || '/';
  331. search && (str += search);
  332. hash && (str += hash);
  333. return str;
  334. }
  335. /**
  336. * Sometimes we receive strings that we don't know if already percent-encoded, or not, due to the
  337. * various sources we get URLs or room names. This function encapsulates the decoding in a safe way.
  338. *
  339. * @param {string} text - The text to decode.
  340. * @returns {string}
  341. */
  342. export function safeDecodeURIComponent(text: string) {
  343. try {
  344. return decodeURIComponent(text);
  345. } catch (e) {
  346. // The text wasn't encoded.
  347. }
  348. return text;
  349. }
  350. /**
  351. * Attempts to return a {@code String} representation of a specific
  352. * {@code Object} which is supposed to represent a URL. Obviously, if a
  353. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  354. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  355. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  356. * mobile/React Native and Web/React.
  357. *
  358. * @param {Object|string} obj - The URL to return a {@code String}
  359. * representation of.
  360. * @returns {string} - A {@code String} representation of the specified
  361. * {@code obj} which is supposed to represent a URL.
  362. */
  363. export function toURLString(obj: ?(Object | string)): ?string {
  364. let str;
  365. switch (typeof obj) {
  366. case 'object':
  367. if (obj) {
  368. if (obj instanceof URL) {
  369. str = obj.href;
  370. } else {
  371. str = urlObjectToString(obj);
  372. }
  373. }
  374. break;
  375. case 'string':
  376. str = String(obj);
  377. break;
  378. }
  379. return str;
  380. }
  381. /**
  382. * Attempts to return a {@code String} representation of a specific
  383. * {@code Object} similar to the one accepted by the constructor
  384. * of Web's ExternalAPI.
  385. *
  386. * @param {Object} o - The URL to return a {@code String} representation of.
  387. * @returns {string} - A {@code String} representation of the specified
  388. * {@code Object}.
  389. */
  390. export function urlObjectToString(o: Object): ?string {
  391. // First normalize the given url. It come as o.url or split into o.serverURL
  392. // and o.room.
  393. let tmp;
  394. if (o.serverURL && o.room) {
  395. tmp = new URL(o.room, o.serverURL).toString();
  396. } else if (o.room) {
  397. tmp = o.room;
  398. } else {
  399. tmp = o.url || '';
  400. }
  401. const url = parseStandardURIString(_fixURIStringScheme(tmp));
  402. // protocol
  403. if (!url.protocol) {
  404. let protocol: ?string = o.protocol || o.scheme;
  405. if (protocol) {
  406. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  407. // do not make a fuss if the final ':' is not there.
  408. protocol.endsWith(':') || (protocol += ':');
  409. url.protocol = protocol;
  410. }
  411. }
  412. // authority & pathname
  413. let { pathname } = url;
  414. if (!url.host) {
  415. // Web's ExternalAPI domain
  416. //
  417. // It may be host/hostname and pathname with the latter denoting the
  418. // tenant.
  419. const domain: ?string = o.domain || o.host || o.hostname;
  420. if (domain) {
  421. const { host, hostname, pathname: contextRoot, port }
  422. = parseStandardURIString(
  423. // XXX The value of domain in supposed to be host/hostname
  424. // and, optionally, pathname. Make sure it is not taken for
  425. // a pathname only.
  426. _fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
  427. // authority
  428. if (host) {
  429. url.host = host;
  430. url.hostname = hostname;
  431. url.port = port;
  432. }
  433. // pathname
  434. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  435. }
  436. }
  437. // pathname
  438. // Web's ExternalAPI roomName
  439. const room = o.roomName || o.room;
  440. if (room
  441. && (url.pathname.endsWith('/')
  442. || !url.pathname.endsWith(`/${room}`))) {
  443. pathname.endsWith('/') || (pathname += '/');
  444. pathname += room;
  445. }
  446. url.pathname = pathname;
  447. // query/search
  448. // Web's ExternalAPI jwt
  449. const { jwt } = o;
  450. if (jwt) {
  451. let { search } = url;
  452. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  453. search.startsWith('?') || (search = `?${search}`);
  454. search.length === 1 || (search += '&');
  455. search += `jwt=${jwt}`;
  456. url.search = search;
  457. }
  458. }
  459. // fragment/hash
  460. let { hash } = url;
  461. for (const urlPrefix of [ 'config', 'interfaceConfig', 'devices', 'userInfo', 'appData' ]) {
  462. const urlParamsArray
  463. = _objectToURLParamsArray(
  464. o[`${urlPrefix}Overwrite`]
  465. || o[urlPrefix]
  466. || o[`${urlPrefix}Override`]);
  467. if (urlParamsArray.length) {
  468. let urlParamsString
  469. = `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
  470. if (hash.length) {
  471. urlParamsString = `&${urlParamsString}`;
  472. } else {
  473. hash = '#';
  474. }
  475. hash += urlParamsString;
  476. }
  477. }
  478. url.hash = hash;
  479. return url.toString() || undefined;
  480. }
  481. /**
  482. * Adds hash params to URL.
  483. *
  484. * @param {URL} url - The URL.
  485. * @param {Object} hashParamsToAdd - A map with the parameters to be set.
  486. * @returns {URL} - The new URL.
  487. */
  488. export function addHashParamsToURL(url: URL, hashParamsToAdd: Object = {}) {
  489. const params = parseURLParams(url);
  490. const urlParamsArray = _objectToURLParamsArray({
  491. ...params,
  492. ...hashParamsToAdd
  493. });
  494. if (urlParamsArray.length) {
  495. url.hash = `#${urlParamsArray.join('&')}`;
  496. }
  497. return url;
  498. }
  499. /**
  500. * Returns the decoded URI.
  501. *
  502. * @param {string} uri - The URI to decode.
  503. * @returns {string}
  504. */
  505. export function getDecodedURI(uri: string) {
  506. return decodeURI(uri.replace(/^https?:\/\//i, ''));
  507. }