Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 room name to a backend-safe format. Properly lowercased and url encoded.
  89. *
  90. * @param {string?} room - The room name to convert.
  91. * @returns {string?}
  92. */
  93. export function getBackendSafeRoomName(room: ?string): ?string {
  94. if (!room) {
  95. return room;
  96. }
  97. /* eslint-disable no-param-reassign */
  98. try {
  99. // We do not know if we get an already encoded string at this point
  100. // as different platforms do it differently, but we need a decoded one
  101. // for sure. However since decoding a non-encoded string is a noop, we're safe
  102. // doing it here.
  103. room = decodeURIComponent(room);
  104. } catch (e) {
  105. // This can happen though if we get an unencoded string and it contains
  106. // some characters that look like an encoded entity, but it's not.
  107. // But in this case we're fine goin on...
  108. }
  109. // Normalize the character set.
  110. room = normalizeNFKC(room);
  111. // Only decoded and normalized strings can be lowercased properly.
  112. room = room.toLowerCase();
  113. // But we still need to (re)encode it.
  114. room = encodeURIComponent(room);
  115. /* eslint-enable no-param-reassign */
  116. // Unfortunately we still need to lowercase it, because encoding a string will
  117. // add some uppercase characters, but some backend services
  118. // expect it to be full lowercase. However lowercasing an encoded string
  119. // doesn't change the string value.
  120. return room.toLowerCase();
  121. }
  122. /**
  123. * Gets the (Web application) context root defined by a specific location (URI).
  124. *
  125. * @param {Object} location - The location (URI) which defines the (Web
  126. * application) context root.
  127. * @public
  128. * @returns {string} - The (Web application) context root defined by the
  129. * specified {@code location} (URI).
  130. */
  131. export function getLocationContextRoot({ pathname }: { pathname: string }) {
  132. const contextRootEndIndex = pathname.lastIndexOf('/');
  133. return (
  134. contextRootEndIndex === -1
  135. ? '/'
  136. : pathname.substring(0, contextRootEndIndex + 1));
  137. }
  138. /**
  139. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  140. * specific {@code Object}.
  141. *
  142. * @param {Object} obj - The {@code Object} to turn into URL parameter
  143. * {@code String}s.
  144. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  145. * constructed out of the specified {@code obj}.
  146. */
  147. function _objectToURLParamsArray(obj = {}) {
  148. const params = [];
  149. for (const key in obj) { // eslint-disable-line guard-for-in
  150. try {
  151. params.push(
  152. `${key}=${encodeURIComponent(JSON.stringify(obj[key]))}`);
  153. } catch (e) {
  154. console.warn(`Error encoding ${key}: ${e}`);
  155. }
  156. }
  157. return params;
  158. }
  159. /**
  160. * Parses a specific URI string into an object with the well-known properties of
  161. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  162. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  163. *
  164. * @param {string} str - The URI string to parse.
  165. * @public
  166. * @returns {{
  167. * hash: string,
  168. * host: (string|undefined),
  169. * hostname: (string|undefined),
  170. * pathname: string,
  171. * port: (string|undefined),
  172. * protocol: (string|undefined),
  173. * search: string
  174. * }}
  175. */
  176. export function parseStandardURIString(str: string) {
  177. /* eslint-disable no-param-reassign */
  178. const obj: Object = {
  179. toString: _standardURIToString
  180. };
  181. let regex;
  182. let match: Array<string> | null;
  183. // XXX A URI string as defined by RFC 3986 does not contain any whitespace.
  184. // Usually, a browser will have already encoded any whitespace. In order to
  185. // avoid potential later problems related to whitespace in URI, strip any
  186. // whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
  187. // whitespace so the stripping is deemed safe.
  188. str = str.replace(/\s/g, '');
  189. // protocol
  190. regex = new RegExp(URI_PROTOCOL_PATTERN, 'gi');
  191. match = regex.exec(str);
  192. if (match) {
  193. obj.protocol = match[1].toLowerCase();
  194. str = str.substring(regex.lastIndex);
  195. }
  196. // authority
  197. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  198. match = regex.exec(str);
  199. if (match) {
  200. let authority: string = match[1].substring(/* // */ 2);
  201. str = str.substring(regex.lastIndex);
  202. // userinfo
  203. const userinfoEndIndex = authority.indexOf('@');
  204. if (userinfoEndIndex !== -1) {
  205. authority = authority.substring(userinfoEndIndex + 1);
  206. }
  207. obj.host = authority;
  208. // port
  209. const portBeginIndex = authority.lastIndexOf(':');
  210. if (portBeginIndex !== -1) {
  211. obj.port = authority.substring(portBeginIndex + 1);
  212. authority = authority.substring(0, portBeginIndex);
  213. }
  214. // hostname
  215. obj.hostname = authority;
  216. }
  217. // pathname
  218. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  219. match = regex.exec(str);
  220. let pathname: ?string;
  221. if (match) {
  222. pathname = match[1];
  223. str = str.substring(regex.lastIndex);
  224. }
  225. if (pathname) {
  226. pathname.startsWith('/') || (pathname = `/${pathname}`);
  227. } else {
  228. pathname = '/';
  229. }
  230. obj.pathname = pathname;
  231. // query
  232. if (str.startsWith('?')) {
  233. let hashBeginIndex = str.indexOf('#', 1);
  234. if (hashBeginIndex === -1) {
  235. hashBeginIndex = str.length;
  236. }
  237. obj.search = str.substring(0, hashBeginIndex);
  238. str = str.substring(hashBeginIndex);
  239. } else {
  240. obj.search = ''; // Google Chrome
  241. }
  242. // fragment
  243. obj.hash = str.startsWith('#') ? str : '';
  244. /* eslint-enable no-param-reassign */
  245. return obj;
  246. }
  247. /**
  248. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  249. * (location).
  250. *
  251. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  252. * references a Jitsi Meet resource (location).
  253. * @public
  254. * @returns {{
  255. * contextRoot: string,
  256. * hash: string,
  257. * host: string,
  258. * hostname: string,
  259. * pathname: string,
  260. * port: string,
  261. * protocol: string,
  262. * room: (string|undefined),
  263. * search: string
  264. * }}
  265. */
  266. export function parseURIString(uri: ?string) {
  267. if (typeof uri !== 'string') {
  268. return undefined;
  269. }
  270. const obj = parseStandardURIString(_fixURIStringScheme(uri));
  271. // Add the properties that are specific to a Jitsi Meet resource (location)
  272. // such as contextRoot, room:
  273. // contextRoot
  274. obj.contextRoot = getLocationContextRoot(obj);
  275. // The room (name) is the last component/segment of pathname.
  276. const { pathname } = obj;
  277. // XXX While the components/segments of pathname are URI encoded, Jitsi Meet
  278. // on the client and/or server sides still don't support certain characters.
  279. const contextRootEndIndex = pathname.lastIndexOf('/');
  280. let room = pathname.substring(contextRootEndIndex + 1) || undefined;
  281. if (room) {
  282. const fixedRoom = _fixRoom(room);
  283. if (fixedRoom !== room) {
  284. room = fixedRoom;
  285. // XXX Drive fixedRoom into pathname (because room is derived from
  286. // pathname).
  287. obj.pathname
  288. = pathname.substring(0, contextRootEndIndex + 1) + (room || '');
  289. }
  290. }
  291. obj.room = room;
  292. return obj;
  293. }
  294. /**
  295. * Implements {@code href} and {@code toString} for the {@code Object} returned
  296. * by {@link #parseStandardURIString}.
  297. *
  298. * @param {Object} [thiz] - An {@code Object} returned by
  299. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  300. * function is invoked on such an instance.
  301. * @returns {string}
  302. */
  303. function _standardURIToString(thiz: ?Object) {
  304. // eslint-disable-next-line no-invalid-this
  305. const { hash, host, pathname, protocol, search } = thiz || this;
  306. let str = '';
  307. protocol && (str += protocol);
  308. // TODO userinfo
  309. host && (str += `//${host}`);
  310. str += pathname || '/';
  311. search && (str += search);
  312. hash && (str += hash);
  313. return str;
  314. }
  315. /**
  316. * Sometimes we receive strings that we don't know if already percent-encoded, or not, due to the
  317. * various sources we get URLs or room names. This function encapsulates the decoding in a safe way.
  318. *
  319. * @param {string} text - The text to decode.
  320. * @returns {string}
  321. */
  322. export function safeDecodeURIComponent(text: string) {
  323. try {
  324. return decodeURIComponent(text);
  325. } catch (e) {
  326. // The text wasn't encoded.
  327. }
  328. return text;
  329. }
  330. /**
  331. * Attempts to return a {@code String} representation of a specific
  332. * {@code Object} which is supposed to represent a URL. Obviously, if a
  333. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  334. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  335. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  336. * mobile/React Native and Web/React.
  337. *
  338. * @param {Object|string} obj - The URL to return a {@code String}
  339. * representation of.
  340. * @returns {string} - A {@code String} representation of the specified
  341. * {@code obj} which is supposed to represent a URL.
  342. */
  343. export function toURLString(obj: ?(Object | string)): ?string {
  344. let str;
  345. switch (typeof obj) {
  346. case 'object':
  347. if (obj) {
  348. if (obj instanceof URL) {
  349. str = obj.href;
  350. } else {
  351. str = urlObjectToString(obj);
  352. }
  353. }
  354. break;
  355. case 'string':
  356. str = String(obj);
  357. break;
  358. }
  359. return str;
  360. }
  361. /**
  362. * Attempts to return a {@code String} representation of a specific
  363. * {@code Object} similar to the one accepted by the constructor
  364. * of Web's ExternalAPI.
  365. *
  366. * @param {Object} o - The URL to return a {@code String} representation of.
  367. * @returns {string} - A {@code String} representation of the specified
  368. * {@code Object}.
  369. */
  370. export function urlObjectToString(o: Object): ?string {
  371. // First normalize the given url. It come as o.url or split into o.serverURL
  372. // and o.room.
  373. let tmp;
  374. if (o.serverURL && o.room) {
  375. tmp = new URL(o.room, o.serverURL).toString();
  376. } else if (o.room) {
  377. tmp = o.room;
  378. } else {
  379. tmp = o.url || '';
  380. }
  381. const url = parseStandardURIString(_fixURIStringScheme(tmp));
  382. // protocol
  383. if (!url.protocol) {
  384. let protocol: ?string = o.protocol || o.scheme;
  385. if (protocol) {
  386. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  387. // do not make a fuss if the final ':' is not there.
  388. protocol.endsWith(':') || (protocol += ':');
  389. url.protocol = protocol;
  390. }
  391. }
  392. // authority & pathname
  393. let { pathname } = url;
  394. if (!url.host) {
  395. // Web's ExternalAPI domain
  396. //
  397. // It may be host/hostname and pathname with the latter denoting the
  398. // tenant.
  399. const domain: ?string = o.domain || o.host || o.hostname;
  400. if (domain) {
  401. const { host, hostname, pathname: contextRoot, port }
  402. = parseStandardURIString(
  403. // XXX The value of domain in supposed to be host/hostname
  404. // and, optionally, pathname. Make sure it is not taken for
  405. // a pathname only.
  406. _fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
  407. // authority
  408. if (host) {
  409. url.host = host;
  410. url.hostname = hostname;
  411. url.port = port;
  412. }
  413. // pathname
  414. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  415. }
  416. }
  417. // pathname
  418. // Web's ExternalAPI roomName
  419. const room = o.roomName || o.room;
  420. if (room
  421. && (url.pathname.endsWith('/')
  422. || !url.pathname.endsWith(`/${room}`))) {
  423. pathname.endsWith('/') || (pathname += '/');
  424. pathname += room;
  425. }
  426. url.pathname = pathname;
  427. // query/search
  428. // Web's ExternalAPI jwt
  429. const { jwt } = o;
  430. if (jwt) {
  431. let { search } = url;
  432. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  433. search.startsWith('?') || (search = `?${search}`);
  434. search.length === 1 || (search += '&');
  435. search += `jwt=${jwt}`;
  436. url.search = search;
  437. }
  438. }
  439. // fragment/hash
  440. let { hash } = url;
  441. for (const urlPrefix of [ 'config', 'interfaceConfig', 'devices', 'userInfo' ]) {
  442. const urlParamsArray
  443. = _objectToURLParamsArray(
  444. o[`${urlPrefix}Overwrite`]
  445. || o[urlPrefix]
  446. || o[`${urlPrefix}Override`]);
  447. if (urlParamsArray.length) {
  448. let urlParamsString
  449. = `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
  450. if (hash.length) {
  451. urlParamsString = `&${urlParamsString}`;
  452. } else {
  453. hash = '#';
  454. }
  455. hash += urlParamsString;
  456. }
  457. }
  458. url.hash = hash;
  459. return url.toString() || undefined;
  460. }
  461. /**
  462. * Adds hash params to URL.
  463. *
  464. * @param {URL} url - The URL.
  465. * @param {Object} hashParamsToAdd - A map with the parameters to be set.
  466. * @returns {URL} - The new URL.
  467. */
  468. export function addHashParamsToURL(url: URL, hashParamsToAdd: Object = {}) {
  469. const params = parseURLParams(url);
  470. const urlParamsArray = _objectToURLParamsArray({
  471. ...params,
  472. ...hashParamsToAdd
  473. });
  474. if (urlParamsArray.length) {
  475. url.hash = `#${urlParamsArray.join('&')}`;
  476. }
  477. return url;
  478. }