您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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