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

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