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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. return obj;
  309. }
  310. /**
  311. * Implements {@code href} and {@code toString} for the {@code Object} returned
  312. * by {@link #parseStandardURIString}.
  313. *
  314. * @param {Object} [thiz] - An {@code Object} returned by
  315. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  316. * function is invoked on such an instance.
  317. * @returns {string}
  318. */
  319. function _standardURIToString(thiz: ?Object) {
  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 {
  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: Object): ?string {
  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 = 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 = 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
  445. const { jwt } = o;
  446. if (jwt) {
  447. let { search } = url;
  448. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  449. search.startsWith('?') || (search = `?${search}`);
  450. search.length === 1 || (search += '&');
  451. search += `jwt=${jwt}`;
  452. url.search = search;
  453. }
  454. }
  455. // fragment/hash
  456. let { hash } = url;
  457. for (const urlPrefix of [ 'config', 'interfaceConfig', 'devices', 'userInfo', 'appData' ]) {
  458. const urlParamsArray
  459. = _objectToURLParamsArray(
  460. o[`${urlPrefix}Overwrite`]
  461. || o[urlPrefix]
  462. || o[`${urlPrefix}Override`]);
  463. if (urlParamsArray.length) {
  464. let urlParamsString
  465. = `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
  466. if (hash.length) {
  467. urlParamsString = `&${urlParamsString}`;
  468. } else {
  469. hash = '#';
  470. }
  471. hash += urlParamsString;
  472. }
  473. }
  474. url.hash = hash;
  475. return url.toString() || undefined;
  476. }
  477. /**
  478. * Adds hash params to URL.
  479. *
  480. * @param {URL} url - The URL.
  481. * @param {Object} hashParamsToAdd - A map with the parameters to be set.
  482. * @returns {URL} - The new URL.
  483. */
  484. export function addHashParamsToURL(url: URL, hashParamsToAdd: Object = {}) {
  485. const params = parseURLParams(url);
  486. const urlParamsArray = _objectToURLParamsArray({
  487. ...params,
  488. ...hashParamsToAdd
  489. });
  490. if (urlParamsArray.length) {
  491. url.hash = `#${urlParamsArray.join('&')}`;
  492. }
  493. return url;
  494. }
  495. /**
  496. * Returns the decoded URI.
  497. *
  498. * @param {string} uri - The URI to decode.
  499. * @returns {string}
  500. */
  501. export function getDecodedURI(uri: string) {
  502. return decodeURI(uri.replace(/^https?:\/\//i, ''));
  503. }