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

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