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

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