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

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