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

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