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

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