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.

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