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.ts 18KB

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