servlet.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ This module contains base REST classes for constructing REST servlets. """
  15. import logging
  16. from http import HTTPStatus
  17. from typing import (
  18. TYPE_CHECKING,
  19. Iterable,
  20. List,
  21. Mapping,
  22. Optional,
  23. Sequence,
  24. Tuple,
  25. overload,
  26. )
  27. from typing_extensions import Literal
  28. from twisted.web.server import Request
  29. from synapse.api.errors import Codes, SynapseError
  30. from synapse.http.server import HttpServer
  31. from synapse.types import JsonDict, RoomAlias, RoomID
  32. from synapse.util import json_decoder
  33. if TYPE_CHECKING:
  34. from synapse.server import HomeServer
  35. logger = logging.getLogger(__name__)
  36. @overload
  37. def parse_integer(request: Request, name: str, default: int) -> int:
  38. ...
  39. @overload
  40. def parse_integer(request: Request, name: str, *, required: Literal[True]) -> int:
  41. ...
  42. @overload
  43. def parse_integer(
  44. request: Request, name: str, default: Optional[int] = None, required: bool = False
  45. ) -> Optional[int]:
  46. ...
  47. def parse_integer(
  48. request: Request, name: str, default: Optional[int] = None, required: bool = False
  49. ) -> Optional[int]:
  50. """Parse an integer parameter from the request string
  51. Args:
  52. request: the twisted HTTP request.
  53. name: the name of the query parameter.
  54. default: value to use if the parameter is absent, defaults to None.
  55. required: whether to raise a 400 SynapseError if the parameter is absent,
  56. defaults to False.
  57. Returns:
  58. An int value or the default.
  59. Raises:
  60. SynapseError: if the parameter is absent and required, or if the
  61. parameter is present and not an integer.
  62. """
  63. args: Mapping[bytes, Sequence[bytes]] = request.args # type: ignore
  64. return parse_integer_from_args(args, name, default, required)
  65. @overload
  66. def parse_integer_from_args(
  67. args: Mapping[bytes, Sequence[bytes]],
  68. name: str,
  69. default: Optional[int] = None,
  70. ) -> Optional[int]:
  71. ...
  72. @overload
  73. def parse_integer_from_args(
  74. args: Mapping[bytes, Sequence[bytes]],
  75. name: str,
  76. *,
  77. required: Literal[True],
  78. ) -> int:
  79. ...
  80. @overload
  81. def parse_integer_from_args(
  82. args: Mapping[bytes, Sequence[bytes]],
  83. name: str,
  84. default: Optional[int] = None,
  85. required: bool = False,
  86. ) -> Optional[int]:
  87. ...
  88. def parse_integer_from_args(
  89. args: Mapping[bytes, Sequence[bytes]],
  90. name: str,
  91. default: Optional[int] = None,
  92. required: bool = False,
  93. ) -> Optional[int]:
  94. """Parse an integer parameter from the request string
  95. Args:
  96. args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
  97. name: the name of the query parameter.
  98. default: value to use if the parameter is absent, defaults to None.
  99. required: whether to raise a 400 SynapseError if the parameter is absent,
  100. defaults to False.
  101. Returns:
  102. An int value or the default.
  103. Raises:
  104. SynapseError: if the parameter is absent and required, or if the
  105. parameter is present and not an integer.
  106. """
  107. name_bytes = name.encode("ascii")
  108. if name_bytes in args:
  109. try:
  110. return int(args[name_bytes][0])
  111. except Exception:
  112. message = "Query parameter %r must be an integer" % (name,)
  113. raise SynapseError(
  114. HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
  115. )
  116. else:
  117. if required:
  118. message = "Missing integer query parameter %r" % (name,)
  119. raise SynapseError(
  120. HTTPStatus.BAD_REQUEST, message, errcode=Codes.MISSING_PARAM
  121. )
  122. else:
  123. return default
  124. @overload
  125. def parse_boolean(request: Request, name: str, default: bool) -> bool:
  126. ...
  127. @overload
  128. def parse_boolean(request: Request, name: str, *, required: Literal[True]) -> bool:
  129. ...
  130. @overload
  131. def parse_boolean(
  132. request: Request, name: str, default: Optional[bool] = None, required: bool = False
  133. ) -> Optional[bool]:
  134. ...
  135. def parse_boolean(
  136. request: Request, name: str, default: Optional[bool] = None, required: bool = False
  137. ) -> Optional[bool]:
  138. """Parse a boolean parameter from the request query string
  139. Args:
  140. request: the twisted HTTP request.
  141. name: the name of the query parameter.
  142. default: value to use if the parameter is absent, defaults to None.
  143. required: whether to raise a 400 SynapseError if the parameter is absent,
  144. defaults to False.
  145. Returns:
  146. A bool value or the default.
  147. Raises:
  148. SynapseError: if the parameter is absent and required, or if the
  149. parameter is present and not one of "true" or "false".
  150. """
  151. args: Mapping[bytes, Sequence[bytes]] = request.args # type: ignore
  152. return parse_boolean_from_args(args, name, default, required)
  153. @overload
  154. def parse_boolean_from_args(
  155. args: Mapping[bytes, Sequence[bytes]],
  156. name: str,
  157. default: bool,
  158. ) -> bool:
  159. ...
  160. @overload
  161. def parse_boolean_from_args(
  162. args: Mapping[bytes, Sequence[bytes]],
  163. name: str,
  164. *,
  165. required: Literal[True],
  166. ) -> bool:
  167. ...
  168. @overload
  169. def parse_boolean_from_args(
  170. args: Mapping[bytes, Sequence[bytes]],
  171. name: str,
  172. default: Optional[bool] = None,
  173. required: bool = False,
  174. ) -> Optional[bool]:
  175. ...
  176. def parse_boolean_from_args(
  177. args: Mapping[bytes, Sequence[bytes]],
  178. name: str,
  179. default: Optional[bool] = None,
  180. required: bool = False,
  181. ) -> Optional[bool]:
  182. """Parse a boolean parameter from the request query string
  183. Args:
  184. args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
  185. name: the name of the query parameter.
  186. default: value to use if the parameter is absent, defaults to None.
  187. required: whether to raise a 400 SynapseError if the parameter is absent,
  188. defaults to False.
  189. Returns:
  190. A bool value or the default.
  191. Raises:
  192. SynapseError: if the parameter is absent and required, or if the
  193. parameter is present and not one of "true" or "false".
  194. """
  195. name_bytes = name.encode("ascii")
  196. if name_bytes in args:
  197. try:
  198. return {b"true": True, b"false": False}[args[name_bytes][0]]
  199. except Exception:
  200. message = (
  201. "Boolean query parameter %r must be one of ['true', 'false']"
  202. ) % (name,)
  203. raise SynapseError(
  204. HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
  205. )
  206. else:
  207. if required:
  208. message = "Missing boolean query parameter %r" % (name,)
  209. raise SynapseError(
  210. HTTPStatus.BAD_REQUEST, message, errcode=Codes.MISSING_PARAM
  211. )
  212. else:
  213. return default
  214. @overload
  215. def parse_bytes_from_args(
  216. args: Mapping[bytes, Sequence[bytes]],
  217. name: str,
  218. default: Optional[bytes] = None,
  219. ) -> Optional[bytes]:
  220. ...
  221. @overload
  222. def parse_bytes_from_args(
  223. args: Mapping[bytes, Sequence[bytes]],
  224. name: str,
  225. default: Literal[None] = None,
  226. *,
  227. required: Literal[True],
  228. ) -> bytes:
  229. ...
  230. @overload
  231. def parse_bytes_from_args(
  232. args: Mapping[bytes, Sequence[bytes]],
  233. name: str,
  234. default: Optional[bytes] = None,
  235. required: bool = False,
  236. ) -> Optional[bytes]:
  237. ...
  238. def parse_bytes_from_args(
  239. args: Mapping[bytes, Sequence[bytes]],
  240. name: str,
  241. default: Optional[bytes] = None,
  242. required: bool = False,
  243. ) -> Optional[bytes]:
  244. """
  245. Parse a string parameter as bytes from the request query string.
  246. Args:
  247. args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
  248. name: the name of the query parameter.
  249. default: value to use if the parameter is absent,
  250. defaults to None. Must be bytes if encoding is None.
  251. required: whether to raise a 400 SynapseError if the
  252. parameter is absent, defaults to False.
  253. Returns:
  254. Bytes or the default value.
  255. Raises:
  256. SynapseError if the parameter is absent and required.
  257. """
  258. name_bytes = name.encode("ascii")
  259. if name_bytes in args:
  260. return args[name_bytes][0]
  261. elif required:
  262. message = "Missing string query parameter %s" % (name,)
  263. raise SynapseError(HTTPStatus.BAD_REQUEST, message, errcode=Codes.MISSING_PARAM)
  264. return default
  265. @overload
  266. def parse_string(
  267. request: Request,
  268. name: str,
  269. default: str,
  270. *,
  271. allowed_values: Optional[Iterable[str]] = None,
  272. encoding: str = "ascii",
  273. ) -> str:
  274. ...
  275. @overload
  276. def parse_string(
  277. request: Request,
  278. name: str,
  279. *,
  280. required: Literal[True],
  281. allowed_values: Optional[Iterable[str]] = None,
  282. encoding: str = "ascii",
  283. ) -> str:
  284. ...
  285. @overload
  286. def parse_string(
  287. request: Request,
  288. name: str,
  289. *,
  290. required: bool = False,
  291. allowed_values: Optional[Iterable[str]] = None,
  292. encoding: str = "ascii",
  293. ) -> Optional[str]:
  294. ...
  295. def parse_string(
  296. request: Request,
  297. name: str,
  298. default: Optional[str] = None,
  299. required: bool = False,
  300. allowed_values: Optional[Iterable[str]] = None,
  301. encoding: str = "ascii",
  302. ) -> Optional[str]:
  303. """
  304. Parse a string parameter from the request query string.
  305. If encoding is not None, the content of the query param will be
  306. decoded to Unicode using the encoding, otherwise it will be encoded
  307. Args:
  308. request: the twisted HTTP request.
  309. name: the name of the query parameter.
  310. default: value to use if the parameter is absent, defaults to None.
  311. required: whether to raise a 400 SynapseError if the
  312. parameter is absent, defaults to False.
  313. allowed_values: List of allowed values for the
  314. string, or None if any value is allowed, defaults to None. Must be
  315. the same type as name, if given.
  316. encoding: The encoding to decode the string content with.
  317. Returns:
  318. A string value or the default.
  319. Raises:
  320. SynapseError if the parameter is absent and required, or if the
  321. parameter is present, must be one of a list of allowed values and
  322. is not one of those allowed values.
  323. """
  324. args: Mapping[bytes, Sequence[bytes]] = request.args # type: ignore
  325. return parse_string_from_args(
  326. args,
  327. name,
  328. default,
  329. required=required,
  330. allowed_values=allowed_values,
  331. encoding=encoding,
  332. )
  333. def _parse_string_value(
  334. value: bytes,
  335. allowed_values: Optional[Iterable[str]],
  336. name: str,
  337. encoding: str,
  338. ) -> str:
  339. try:
  340. value_str = value.decode(encoding)
  341. except ValueError:
  342. raise SynapseError(
  343. HTTPStatus.BAD_REQUEST, "Query parameter %r must be %s" % (name, encoding)
  344. )
  345. if allowed_values is not None and value_str not in allowed_values:
  346. message = "Query parameter %r must be one of [%s]" % (
  347. name,
  348. ", ".join(repr(v) for v in allowed_values),
  349. )
  350. raise SynapseError(HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM)
  351. else:
  352. return value_str
  353. @overload
  354. def parse_strings_from_args(
  355. args: Mapping[bytes, Sequence[bytes]],
  356. name: str,
  357. *,
  358. allowed_values: Optional[Iterable[str]] = None,
  359. encoding: str = "ascii",
  360. ) -> Optional[List[str]]:
  361. ...
  362. @overload
  363. def parse_strings_from_args(
  364. args: Mapping[bytes, Sequence[bytes]],
  365. name: str,
  366. default: List[str],
  367. *,
  368. allowed_values: Optional[Iterable[str]] = None,
  369. encoding: str = "ascii",
  370. ) -> List[str]:
  371. ...
  372. @overload
  373. def parse_strings_from_args(
  374. args: Mapping[bytes, Sequence[bytes]],
  375. name: str,
  376. *,
  377. required: Literal[True],
  378. allowed_values: Optional[Iterable[str]] = None,
  379. encoding: str = "ascii",
  380. ) -> List[str]:
  381. ...
  382. @overload
  383. def parse_strings_from_args(
  384. args: Mapping[bytes, Sequence[bytes]],
  385. name: str,
  386. default: Optional[List[str]] = None,
  387. *,
  388. required: bool = False,
  389. allowed_values: Optional[Iterable[str]] = None,
  390. encoding: str = "ascii",
  391. ) -> Optional[List[str]]:
  392. ...
  393. def parse_strings_from_args(
  394. args: Mapping[bytes, Sequence[bytes]],
  395. name: str,
  396. default: Optional[List[str]] = None,
  397. required: bool = False,
  398. allowed_values: Optional[Iterable[str]] = None,
  399. encoding: str = "ascii",
  400. ) -> Optional[List[str]]:
  401. """
  402. Parse a string parameter from the request query string list.
  403. The content of the query param will be decoded to Unicode using the encoding.
  404. Args:
  405. args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
  406. name: the name of the query parameter.
  407. default: value to use if the parameter is absent, defaults to None.
  408. required: whether to raise a 400 SynapseError if the
  409. parameter is absent, defaults to False.
  410. allowed_values: List of allowed values for the
  411. string, or None if any value is allowed, defaults to None.
  412. encoding: The encoding to decode the string content with.
  413. Returns:
  414. A string value or the default.
  415. Raises:
  416. SynapseError if the parameter is absent and required, or if the
  417. parameter is present, must be one of a list of allowed values and
  418. is not one of those allowed values.
  419. """
  420. name_bytes = name.encode("ascii")
  421. if name_bytes in args:
  422. values = args[name_bytes]
  423. return [
  424. _parse_string_value(value, allowed_values, name=name, encoding=encoding)
  425. for value in values
  426. ]
  427. else:
  428. if required:
  429. message = "Missing string query parameter %r" % (name,)
  430. raise SynapseError(
  431. HTTPStatus.BAD_REQUEST, message, errcode=Codes.MISSING_PARAM
  432. )
  433. return default
  434. @overload
  435. def parse_string_from_args(
  436. args: Mapping[bytes, Sequence[bytes]],
  437. name: str,
  438. default: Optional[str] = None,
  439. *,
  440. allowed_values: Optional[Iterable[str]] = None,
  441. encoding: str = "ascii",
  442. ) -> Optional[str]:
  443. ...
  444. @overload
  445. def parse_string_from_args(
  446. args: Mapping[bytes, Sequence[bytes]],
  447. name: str,
  448. default: Optional[str] = None,
  449. *,
  450. required: Literal[True],
  451. allowed_values: Optional[Iterable[str]] = None,
  452. encoding: str = "ascii",
  453. ) -> str:
  454. ...
  455. @overload
  456. def parse_string_from_args(
  457. args: Mapping[bytes, Sequence[bytes]],
  458. name: str,
  459. default: Optional[str] = None,
  460. required: bool = False,
  461. allowed_values: Optional[Iterable[str]] = None,
  462. encoding: str = "ascii",
  463. ) -> Optional[str]:
  464. ...
  465. def parse_string_from_args(
  466. args: Mapping[bytes, Sequence[bytes]],
  467. name: str,
  468. default: Optional[str] = None,
  469. required: bool = False,
  470. allowed_values: Optional[Iterable[str]] = None,
  471. encoding: str = "ascii",
  472. ) -> Optional[str]:
  473. """
  474. Parse the string parameter from the request query string list
  475. and return the first result.
  476. The content of the query param will be decoded to Unicode using the encoding.
  477. Args:
  478. args: A mapping of request args as bytes to a list of bytes (e.g. request.args).
  479. name: the name of the query parameter.
  480. default: value to use if the parameter is absent, defaults to None.
  481. required: whether to raise a 400 SynapseError if the
  482. parameter is absent, defaults to False.
  483. allowed_values: List of allowed values for the
  484. string, or None if any value is allowed, defaults to None. Must be
  485. the same type as name, if given.
  486. encoding: The encoding to decode the string content with.
  487. Returns:
  488. A string value or the default.
  489. Raises:
  490. SynapseError if the parameter is absent and required, or if the
  491. parameter is present, must be one of a list of allowed values and
  492. is not one of those allowed values.
  493. """
  494. strings = parse_strings_from_args(
  495. args,
  496. name,
  497. default=[default] if default is not None else None,
  498. required=required,
  499. allowed_values=allowed_values,
  500. encoding=encoding,
  501. )
  502. if strings is None:
  503. return None
  504. return strings[0]
  505. @overload
  506. def parse_json_value_from_request(request: Request) -> JsonDict:
  507. ...
  508. @overload
  509. def parse_json_value_from_request(
  510. request: Request, allow_empty_body: Literal[False]
  511. ) -> JsonDict:
  512. ...
  513. @overload
  514. def parse_json_value_from_request(
  515. request: Request, allow_empty_body: bool = False
  516. ) -> Optional[JsonDict]:
  517. ...
  518. def parse_json_value_from_request(
  519. request: Request, allow_empty_body: bool = False
  520. ) -> Optional[JsonDict]:
  521. """Parse a JSON value from the body of a twisted HTTP request.
  522. Args:
  523. request: the twisted HTTP request.
  524. allow_empty_body: if True, an empty body will be accepted and turned into None
  525. Returns:
  526. The JSON value.
  527. Raises:
  528. SynapseError if the request body couldn't be decoded as JSON.
  529. """
  530. try:
  531. content_bytes = request.content.read() # type: ignore
  532. except Exception:
  533. raise SynapseError(HTTPStatus.BAD_REQUEST, "Error reading JSON content.")
  534. if not content_bytes and allow_empty_body:
  535. return None
  536. try:
  537. content = json_decoder.decode(content_bytes.decode("utf-8"))
  538. except Exception as e:
  539. logger.warning("Unable to parse JSON: %s (%s)", e, content_bytes)
  540. raise SynapseError(
  541. HTTPStatus.BAD_REQUEST, "Content not JSON.", errcode=Codes.NOT_JSON
  542. )
  543. return content
  544. def parse_json_object_from_request(
  545. request: Request, allow_empty_body: bool = False
  546. ) -> JsonDict:
  547. """Parse a JSON object from the body of a twisted HTTP request.
  548. Args:
  549. request: the twisted HTTP request.
  550. allow_empty_body: if True, an empty body will be accepted and turned into
  551. an empty dict.
  552. Raises:
  553. SynapseError if the request body couldn't be decoded as JSON or
  554. if it wasn't a JSON object.
  555. """
  556. content = parse_json_value_from_request(request, allow_empty_body=allow_empty_body)
  557. if allow_empty_body and content is None:
  558. return {}
  559. if not isinstance(content, dict):
  560. message = "Content must be a JSON object."
  561. raise SynapseError(HTTPStatus.BAD_REQUEST, message, errcode=Codes.BAD_JSON)
  562. return content
  563. def assert_params_in_dict(body: JsonDict, required: Iterable[str]) -> None:
  564. absent = []
  565. for k in required:
  566. if k not in body:
  567. absent.append(k)
  568. if len(absent) > 0:
  569. raise SynapseError(
  570. HTTPStatus.BAD_REQUEST, "Missing params: %r" % absent, Codes.MISSING_PARAM
  571. )
  572. class RestServlet:
  573. """A Synapse REST Servlet.
  574. An implementing class can either provide its own custom 'register' method,
  575. or use the automatic pattern handling provided by the base class.
  576. To use this latter, the implementing class instead provides a `PATTERN`
  577. class attribute containing a pre-compiled regular expression. The automatic
  578. register method will then use this method to register any of the following
  579. instance methods associated with the corresponding HTTP method:
  580. on_GET
  581. on_PUT
  582. on_POST
  583. on_DELETE
  584. Automatically handles turning CodeMessageExceptions thrown by these methods
  585. into the appropriate HTTP response.
  586. """
  587. def register(self, http_server: HttpServer) -> None:
  588. """Register this servlet with the given HTTP server."""
  589. patterns = getattr(self, "PATTERNS", None)
  590. if patterns:
  591. for method in ("GET", "PUT", "POST", "DELETE"):
  592. if hasattr(self, "on_%s" % (method,)):
  593. servlet_classname = self.__class__.__name__
  594. method_handler = getattr(self, "on_%s" % (method,))
  595. http_server.register_paths(
  596. method, patterns, method_handler, servlet_classname
  597. )
  598. else:
  599. raise NotImplementedError("RestServlet must register something.")
  600. class ResolveRoomIdMixin:
  601. def __init__(self, hs: "HomeServer"):
  602. self.room_member_handler = hs.get_room_member_handler()
  603. async def resolve_room_id(
  604. self, room_identifier: str, remote_room_hosts: Optional[List[str]] = None
  605. ) -> Tuple[str, Optional[List[str]]]:
  606. """
  607. Resolve a room identifier to a room ID, if necessary.
  608. This also performanes checks to ensure the room ID is of the proper form.
  609. Args:
  610. room_identifier: The room ID or alias.
  611. remote_room_hosts: The potential remote room hosts to use.
  612. Returns:
  613. The resolved room ID.
  614. Raises:
  615. SynapseError if the room ID is of the wrong form.
  616. """
  617. if RoomID.is_valid(room_identifier):
  618. resolved_room_id = room_identifier
  619. elif RoomAlias.is_valid(room_identifier):
  620. room_alias = RoomAlias.from_string(room_identifier)
  621. (
  622. room_id,
  623. remote_room_hosts,
  624. ) = await self.room_member_handler.lookup_room_alias(room_alias)
  625. resolved_room_id = room_id.to_string()
  626. else:
  627. raise SynapseError(
  628. HTTPStatus.BAD_REQUEST,
  629. "%s was not legal room ID or room alias" % (room_identifier,),
  630. )
  631. if not resolved_room_id:
  632. raise SynapseError(
  633. HTTPStatus.BAD_REQUEST,
  634. "Unknown room ID or room alias %s" % room_identifier,
  635. )
  636. return resolved_room_id, remote_room_hosts