matrixfederationclient.py 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import cgi
  17. import logging
  18. import random
  19. import sys
  20. from io import BytesIO
  21. from six import raise_from, string_types
  22. from six.moves import urllib
  23. import attr
  24. import treq
  25. from canonicaljson import encode_canonical_json
  26. from prometheus_client import Counter
  27. from signedjson.sign import sign_json
  28. from zope.interface import implementer
  29. from twisted.internet import defer, protocol
  30. from twisted.internet.error import DNSLookupError
  31. from twisted.internet.interfaces import IReactorPluggableNameResolver
  32. from twisted.internet.task import _EPSILON, Cooperator
  33. from twisted.web._newclient import ResponseDone
  34. from twisted.web.http_headers import Headers
  35. import synapse.metrics
  36. import synapse.util.retryutils
  37. from synapse.api.errors import (
  38. Codes,
  39. FederationDeniedError,
  40. HttpResponseException,
  41. RequestSendFailed,
  42. SynapseError,
  43. )
  44. from synapse.http import QuieterFileBodyProducer
  45. from synapse.http.client import BlacklistingAgentWrapper, IPBlacklistingResolver
  46. from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
  47. from synapse.logging.context import make_deferred_yieldable
  48. from synapse.logging.opentracing import (
  49. inject_active_span_byte_dict,
  50. set_tag,
  51. start_active_span,
  52. tags,
  53. )
  54. from synapse.util.async_helpers import timeout_deferred
  55. from synapse.util.metrics import Measure
  56. logger = logging.getLogger(__name__)
  57. outgoing_requests_counter = Counter(
  58. "synapse_http_matrixfederationclient_requests", "", ["method"]
  59. )
  60. incoming_responses_counter = Counter(
  61. "synapse_http_matrixfederationclient_responses", "", ["method", "code"]
  62. )
  63. MAX_LONG_RETRIES = 10
  64. MAX_SHORT_RETRIES = 3
  65. MAXINT = sys.maxsize
  66. _next_id = 1
  67. @attr.s
  68. class MatrixFederationRequest(object):
  69. method = attr.ib()
  70. """HTTP method
  71. :type: str
  72. """
  73. path = attr.ib()
  74. """HTTP path
  75. :type: str
  76. """
  77. destination = attr.ib()
  78. """The remote server to send the HTTP request to.
  79. :type: str"""
  80. json = attr.ib(default=None)
  81. """JSON to send in the body.
  82. :type: dict|None
  83. """
  84. json_callback = attr.ib(default=None)
  85. """A callback to generate the JSON.
  86. :type: func|None
  87. """
  88. query = attr.ib(default=None)
  89. """Query arguments.
  90. :type: dict|None
  91. """
  92. txn_id = attr.ib(default=None)
  93. """Unique ID for this request (for logging)
  94. :type: str|None
  95. """
  96. def __attrs_post_init__(self):
  97. global _next_id
  98. self.txn_id = "%s-O-%s" % (self.method, _next_id)
  99. _next_id = (_next_id + 1) % (MAXINT - 1)
  100. def get_json(self):
  101. if self.json_callback:
  102. return self.json_callback()
  103. return self.json
  104. @defer.inlineCallbacks
  105. def _handle_json_response(reactor, timeout_sec, request, response):
  106. """
  107. Reads the JSON body of a response, with a timeout
  108. Args:
  109. reactor (IReactor): twisted reactor, for the timeout
  110. timeout_sec (float): number of seconds to wait for response to complete
  111. request (MatrixFederationRequest): the request that triggered the response
  112. response (IResponse): response to the request
  113. Returns:
  114. dict: parsed JSON response
  115. """
  116. try:
  117. check_content_type_is_json(response.headers)
  118. d = treq.json_content(response)
  119. d = timeout_deferred(d, timeout=timeout_sec, reactor=reactor)
  120. body = yield make_deferred_yieldable(d)
  121. except Exception as e:
  122. logger.warning(
  123. "{%s} [%s] Error reading response: %s",
  124. request.txn_id,
  125. request.destination,
  126. e,
  127. )
  128. raise
  129. logger.info(
  130. "{%s} [%s] Completed: %d %s",
  131. request.txn_id,
  132. request.destination,
  133. response.code,
  134. response.phrase.decode("ascii", errors="replace"),
  135. )
  136. return body
  137. class MatrixFederationHttpClient(object):
  138. """HTTP client used to talk to other homeservers over the federation
  139. protocol. Send client certificates and signs requests.
  140. Attributes:
  141. agent (twisted.web.client.Agent): The twisted Agent used to send the
  142. requests.
  143. """
  144. def __init__(self, hs, tls_client_options_factory):
  145. self.hs = hs
  146. self.signing_key = hs.config.signing_key[0]
  147. self.server_name = hs.hostname
  148. real_reactor = hs.get_reactor()
  149. # We need to use a DNS resolver which filters out blacklisted IP
  150. # addresses, to prevent DNS rebinding.
  151. nameResolver = IPBlacklistingResolver(
  152. real_reactor, None, hs.config.federation_ip_range_blacklist
  153. )
  154. @implementer(IReactorPluggableNameResolver)
  155. class Reactor(object):
  156. def __getattr__(_self, attr):
  157. if attr == "nameResolver":
  158. return nameResolver
  159. else:
  160. return getattr(real_reactor, attr)
  161. self.reactor = Reactor()
  162. self.agent = MatrixFederationAgent(self.reactor, tls_client_options_factory)
  163. # Use a BlacklistingAgentWrapper to prevent circumventing the IP
  164. # blacklist via IP literals in server names
  165. self.agent = BlacklistingAgentWrapper(
  166. self.agent,
  167. self.reactor,
  168. ip_blacklist=hs.config.federation_ip_range_blacklist,
  169. )
  170. self.clock = hs.get_clock()
  171. self._store = hs.get_datastore()
  172. self.version_string_bytes = hs.version_string.encode("ascii")
  173. self.default_timeout = 60
  174. def schedule(x):
  175. self.reactor.callLater(_EPSILON, x)
  176. self._cooperator = Cooperator(scheduler=schedule)
  177. @defer.inlineCallbacks
  178. def _send_request_with_optional_trailing_slash(
  179. self, request, try_trailing_slash_on_400=False, **send_request_args
  180. ):
  181. """Wrapper for _send_request which can optionally retry the request
  182. upon receiving a combination of a 400 HTTP response code and a
  183. 'M_UNRECOGNIZED' errcode. This is a workaround for Synapse <= v0.99.3
  184. due to #3622.
  185. Args:
  186. request (MatrixFederationRequest): details of request to be sent
  187. try_trailing_slash_on_400 (bool): Whether on receiving a 400
  188. 'M_UNRECOGNIZED' from the server to retry the request with a
  189. trailing slash appended to the request path.
  190. send_request_args (Dict): A dictionary of arguments to pass to
  191. `_send_request()`.
  192. Raises:
  193. HttpResponseException: If we get an HTTP response code >= 300
  194. (except 429).
  195. Returns:
  196. Deferred[Dict]: Parsed JSON response body.
  197. """
  198. try:
  199. response = yield self._send_request(request, **send_request_args)
  200. except HttpResponseException as e:
  201. # Received an HTTP error > 300. Check if it meets the requirements
  202. # to retry with a trailing slash
  203. if not try_trailing_slash_on_400:
  204. raise
  205. if e.code != 400 or e.to_synapse_error().errcode != "M_UNRECOGNIZED":
  206. raise
  207. # Retry with a trailing slash if we received a 400 with
  208. # 'M_UNRECOGNIZED' which some endpoints can return when omitting a
  209. # trailing slash on Synapse <= v0.99.3.
  210. logger.info("Retrying request with trailing slash")
  211. request.path += "/"
  212. response = yield self._send_request(request, **send_request_args)
  213. return response
  214. @defer.inlineCallbacks
  215. def _send_request(
  216. self,
  217. request,
  218. retry_on_dns_fail=True,
  219. timeout=None,
  220. long_retries=False,
  221. ignore_backoff=False,
  222. backoff_on_404=False,
  223. ):
  224. """
  225. Sends a request to the given server.
  226. Args:
  227. request (MatrixFederationRequest): details of request to be sent
  228. timeout (int|None): number of milliseconds to wait for the response headers
  229. (including connecting to the server), *for each attempt*.
  230. 60s by default.
  231. long_retries (bool): whether to use the long retry algorithm.
  232. The regular retry algorithm makes 4 attempts, with intervals
  233. [0.5s, 1s, 2s].
  234. The long retry algorithm makes 11 attempts, with intervals
  235. [4s, 16s, 60s, 60s, ...]
  236. Both algorithms add -20%/+40% jitter to the retry intervals.
  237. Note that the above intervals are *in addition* to the time spent
  238. waiting for the request to complete (up to `timeout` ms).
  239. NB: the long retry algorithm takes over 20 minutes to complete, with
  240. a default timeout of 60s!
  241. ignore_backoff (bool): true to ignore the historical backoff data
  242. and try the request anyway.
  243. backoff_on_404 (bool): Back off if we get a 404
  244. Returns:
  245. Deferred[twisted.web.client.Response]: resolves with the HTTP
  246. response object on success.
  247. Raises:
  248. HttpResponseException: If we get an HTTP response code >= 300
  249. (except 429).
  250. NotRetryingDestination: If we are not yet ready to retry this
  251. server.
  252. FederationDeniedError: If this destination is not on our
  253. federation whitelist
  254. RequestSendFailed: If there were problems connecting to the
  255. remote, due to e.g. DNS failures, connection timeouts etc.
  256. """
  257. if timeout:
  258. _sec_timeout = timeout / 1000
  259. else:
  260. _sec_timeout = self.default_timeout
  261. if (
  262. self.hs.config.federation_domain_whitelist is not None
  263. and request.destination not in self.hs.config.federation_domain_whitelist
  264. ):
  265. raise FederationDeniedError(request.destination)
  266. limiter = yield synapse.util.retryutils.get_retry_limiter(
  267. request.destination,
  268. self.clock,
  269. self._store,
  270. backoff_on_404=backoff_on_404,
  271. ignore_backoff=ignore_backoff,
  272. )
  273. method_bytes = request.method.encode("ascii")
  274. destination_bytes = request.destination.encode("ascii")
  275. path_bytes = request.path.encode("ascii")
  276. if request.query:
  277. query_bytes = encode_query_args(request.query)
  278. else:
  279. query_bytes = b""
  280. scope = start_active_span(
  281. "outgoing-federation-request",
  282. tags={
  283. tags.SPAN_KIND: tags.SPAN_KIND_RPC_CLIENT,
  284. tags.PEER_ADDRESS: request.destination,
  285. tags.HTTP_METHOD: request.method,
  286. tags.HTTP_URL: request.path,
  287. },
  288. finish_on_close=True,
  289. )
  290. # Inject the span into the headers
  291. headers_dict = {}
  292. inject_active_span_byte_dict(headers_dict, request.destination)
  293. headers_dict[b"User-Agent"] = [self.version_string_bytes]
  294. with limiter, scope:
  295. # XXX: Would be much nicer to retry only at the transaction-layer
  296. # (once we have reliable transactions in place)
  297. if long_retries:
  298. retries_left = MAX_LONG_RETRIES
  299. else:
  300. retries_left = MAX_SHORT_RETRIES
  301. url_bytes = urllib.parse.urlunparse(
  302. (b"matrix", destination_bytes, path_bytes, None, query_bytes, b"")
  303. )
  304. url_str = url_bytes.decode("ascii")
  305. url_to_sign_bytes = urllib.parse.urlunparse(
  306. (b"", b"", path_bytes, None, query_bytes, b"")
  307. )
  308. while True:
  309. try:
  310. json = request.get_json()
  311. if json:
  312. headers_dict[b"Content-Type"] = [b"application/json"]
  313. auth_headers = self.build_auth_headers(
  314. destination_bytes, method_bytes, url_to_sign_bytes, json
  315. )
  316. data = encode_canonical_json(json)
  317. producer = QuieterFileBodyProducer(
  318. BytesIO(data), cooperator=self._cooperator
  319. )
  320. else:
  321. producer = None
  322. auth_headers = self.build_auth_headers(
  323. destination_bytes, method_bytes, url_to_sign_bytes
  324. )
  325. headers_dict[b"Authorization"] = auth_headers
  326. logger.info(
  327. "{%s} [%s] Sending request: %s %s; timeout %fs",
  328. request.txn_id,
  329. request.destination,
  330. request.method,
  331. url_str,
  332. _sec_timeout,
  333. )
  334. outgoing_requests_counter.labels(request.method).inc()
  335. try:
  336. with Measure(self.clock, "outbound_request"):
  337. # we don't want all the fancy cookie and redirect handling
  338. # that treq.request gives: just use the raw Agent.
  339. request_deferred = self.agent.request(
  340. method_bytes,
  341. url_bytes,
  342. headers=Headers(headers_dict),
  343. bodyProducer=producer,
  344. )
  345. request_deferred = timeout_deferred(
  346. request_deferred,
  347. timeout=_sec_timeout,
  348. reactor=self.reactor,
  349. )
  350. response = yield request_deferred
  351. except DNSLookupError as e:
  352. raise_from(RequestSendFailed(e, can_retry=retry_on_dns_fail), e)
  353. except Exception as e:
  354. logger.info("Failed to send request: %s", e)
  355. raise_from(RequestSendFailed(e, can_retry=True), e)
  356. incoming_responses_counter.labels(
  357. request.method, response.code
  358. ).inc()
  359. set_tag(tags.HTTP_STATUS_CODE, response.code)
  360. if 200 <= response.code < 300:
  361. logger.debug(
  362. "{%s} [%s] Got response headers: %d %s",
  363. request.txn_id,
  364. request.destination,
  365. response.code,
  366. response.phrase.decode("ascii", errors="replace"),
  367. )
  368. pass
  369. else:
  370. logger.info(
  371. "{%s} [%s] Got response headers: %d %s",
  372. request.txn_id,
  373. request.destination,
  374. response.code,
  375. response.phrase.decode("ascii", errors="replace"),
  376. )
  377. # :'(
  378. # Update transactions table?
  379. d = treq.content(response)
  380. d = timeout_deferred(
  381. d, timeout=_sec_timeout, reactor=self.reactor
  382. )
  383. try:
  384. body = yield make_deferred_yieldable(d)
  385. except Exception as e:
  386. # Eh, we're already going to raise an exception so lets
  387. # ignore if this fails.
  388. logger.warning(
  389. "{%s} [%s] Failed to get error response: %s %s: %s",
  390. request.txn_id,
  391. request.destination,
  392. request.method,
  393. url_str,
  394. _flatten_response_never_received(e),
  395. )
  396. body = None
  397. e = HttpResponseException(response.code, response.phrase, body)
  398. # Retry if the error is a 429 (Too Many Requests),
  399. # otherwise just raise a standard HttpResponseException
  400. if response.code == 429:
  401. raise_from(RequestSendFailed(e, can_retry=True), e)
  402. else:
  403. raise e
  404. break
  405. except RequestSendFailed as e:
  406. logger.warning(
  407. "{%s} [%s] Request failed: %s %s: %s",
  408. request.txn_id,
  409. request.destination,
  410. request.method,
  411. url_str,
  412. _flatten_response_never_received(e.inner_exception),
  413. )
  414. if not e.can_retry:
  415. raise
  416. if retries_left and not timeout:
  417. if long_retries:
  418. delay = 4 ** (MAX_LONG_RETRIES + 1 - retries_left)
  419. delay = min(delay, 60)
  420. delay *= random.uniform(0.8, 1.4)
  421. else:
  422. delay = 0.5 * 2 ** (MAX_SHORT_RETRIES - retries_left)
  423. delay = min(delay, 2)
  424. delay *= random.uniform(0.8, 1.4)
  425. logger.debug(
  426. "{%s} [%s] Waiting %ss before re-sending...",
  427. request.txn_id,
  428. request.destination,
  429. delay,
  430. )
  431. yield self.clock.sleep(delay)
  432. retries_left -= 1
  433. else:
  434. raise
  435. except Exception as e:
  436. logger.warning(
  437. "{%s} [%s] Request failed: %s %s: %s",
  438. request.txn_id,
  439. request.destination,
  440. request.method,
  441. url_str,
  442. _flatten_response_never_received(e),
  443. )
  444. raise
  445. return response
  446. def build_auth_headers(
  447. self, destination, method, url_bytes, content=None, destination_is=None
  448. ):
  449. """
  450. Builds the Authorization headers for a federation request
  451. Args:
  452. destination (bytes|None): The desination homeserver of the request.
  453. May be None if the destination is an identity server, in which case
  454. destination_is must be non-None.
  455. method (bytes): The HTTP method of the request
  456. url_bytes (bytes): The URI path of the request
  457. content (object): The body of the request
  458. destination_is (bytes): As 'destination', but if the destination is an
  459. identity server
  460. Returns:
  461. list[bytes]: a list of headers to be added as "Authorization:" headers
  462. """
  463. request = {"method": method, "uri": url_bytes, "origin": self.server_name}
  464. if destination is not None:
  465. request["destination"] = destination
  466. if destination_is is not None:
  467. request["destination_is"] = destination_is
  468. if content is not None:
  469. request["content"] = content
  470. request = sign_json(request, self.server_name, self.signing_key)
  471. auth_headers = []
  472. for key, sig in request["signatures"][self.server_name].items():
  473. auth_headers.append(
  474. (
  475. 'X-Matrix origin=%s,key="%s",sig="%s"'
  476. % (self.server_name, key, sig)
  477. ).encode("ascii")
  478. )
  479. return auth_headers
  480. @defer.inlineCallbacks
  481. def put_json(
  482. self,
  483. destination,
  484. path,
  485. args={},
  486. data={},
  487. json_data_callback=None,
  488. long_retries=False,
  489. timeout=None,
  490. ignore_backoff=False,
  491. backoff_on_404=False,
  492. try_trailing_slash_on_400=False,
  493. ):
  494. """ Sends the specifed json data using PUT
  495. Args:
  496. destination (str): The remote server to send the HTTP request
  497. to.
  498. path (str): The HTTP path.
  499. args (dict): query params
  500. data (dict): A dict containing the data that will be used as
  501. the request body. This will be encoded as JSON.
  502. json_data_callback (callable): A callable returning the dict to
  503. use as the request body.
  504. long_retries (bool): whether to use the long retry algorithm. See
  505. docs on _send_request for details.
  506. timeout (int|None): number of milliseconds to wait for the response headers
  507. (including connecting to the server), *for each attempt*.
  508. self._default_timeout (60s) by default.
  509. ignore_backoff (bool): true to ignore the historical backoff data
  510. and try the request anyway.
  511. backoff_on_404 (bool): True if we should count a 404 response as
  512. a failure of the server (and should therefore back off future
  513. requests).
  514. try_trailing_slash_on_400 (bool): True if on a 400 M_UNRECOGNIZED
  515. response we should try appending a trailing slash to the end
  516. of the request. Workaround for #3622 in Synapse <= v0.99.3. This
  517. will be attempted before backing off if backing off has been
  518. enabled.
  519. Returns:
  520. Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
  521. result will be the decoded JSON body.
  522. Raises:
  523. HttpResponseException: If we get an HTTP response code >= 300
  524. (except 429).
  525. NotRetryingDestination: If we are not yet ready to retry this
  526. server.
  527. FederationDeniedError: If this destination is not on our
  528. federation whitelist
  529. RequestSendFailed: If there were problems connecting to the
  530. remote, due to e.g. DNS failures, connection timeouts etc.
  531. """
  532. request = MatrixFederationRequest(
  533. method="PUT",
  534. destination=destination,
  535. path=path,
  536. query=args,
  537. json_callback=json_data_callback,
  538. json=data,
  539. )
  540. response = yield self._send_request_with_optional_trailing_slash(
  541. request,
  542. try_trailing_slash_on_400,
  543. backoff_on_404=backoff_on_404,
  544. ignore_backoff=ignore_backoff,
  545. long_retries=long_retries,
  546. timeout=timeout,
  547. )
  548. body = yield _handle_json_response(
  549. self.reactor, self.default_timeout, request, response
  550. )
  551. return body
  552. @defer.inlineCallbacks
  553. def post_json(
  554. self,
  555. destination,
  556. path,
  557. data={},
  558. long_retries=False,
  559. timeout=None,
  560. ignore_backoff=False,
  561. args={},
  562. ):
  563. """ Sends the specifed json data using POST
  564. Args:
  565. destination (str): The remote server to send the HTTP request
  566. to.
  567. path (str): The HTTP path.
  568. data (dict): A dict containing the data that will be used as
  569. the request body. This will be encoded as JSON.
  570. long_retries (bool): whether to use the long retry algorithm. See
  571. docs on _send_request for details.
  572. timeout (int|None): number of milliseconds to wait for the response headers
  573. (including connecting to the server), *for each attempt*.
  574. self._default_timeout (60s) by default.
  575. ignore_backoff (bool): true to ignore the historical backoff data and
  576. try the request anyway.
  577. args (dict): query params
  578. Returns:
  579. Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
  580. result will be the decoded JSON body.
  581. Raises:
  582. HttpResponseException: If we get an HTTP response code >= 300
  583. (except 429).
  584. NotRetryingDestination: If we are not yet ready to retry this
  585. server.
  586. FederationDeniedError: If this destination is not on our
  587. federation whitelist
  588. RequestSendFailed: If there were problems connecting to the
  589. remote, due to e.g. DNS failures, connection timeouts etc.
  590. """
  591. request = MatrixFederationRequest(
  592. method="POST", destination=destination, path=path, query=args, json=data
  593. )
  594. response = yield self._send_request(
  595. request,
  596. long_retries=long_retries,
  597. timeout=timeout,
  598. ignore_backoff=ignore_backoff,
  599. )
  600. if timeout:
  601. _sec_timeout = timeout / 1000
  602. else:
  603. _sec_timeout = self.default_timeout
  604. body = yield _handle_json_response(
  605. self.reactor, _sec_timeout, request, response
  606. )
  607. return body
  608. @defer.inlineCallbacks
  609. def get_json(
  610. self,
  611. destination,
  612. path,
  613. args=None,
  614. retry_on_dns_fail=True,
  615. timeout=None,
  616. ignore_backoff=False,
  617. try_trailing_slash_on_400=False,
  618. ):
  619. """ GETs some json from the given host homeserver and path
  620. Args:
  621. destination (str): The remote server to send the HTTP request
  622. to.
  623. path (str): The HTTP path.
  624. args (dict|None): A dictionary used to create query strings, defaults to
  625. None.
  626. timeout (int|None): number of milliseconds to wait for the response headers
  627. (including connecting to the server), *for each attempt*.
  628. self._default_timeout (60s) by default.
  629. ignore_backoff (bool): true to ignore the historical backoff data
  630. and try the request anyway.
  631. try_trailing_slash_on_400 (bool): True if on a 400 M_UNRECOGNIZED
  632. response we should try appending a trailing slash to the end of
  633. the request. Workaround for #3622 in Synapse <= v0.99.3.
  634. Returns:
  635. Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
  636. result will be the decoded JSON body.
  637. Raises:
  638. HttpResponseException: If we get an HTTP response code >= 300
  639. (except 429).
  640. NotRetryingDestination: If we are not yet ready to retry this
  641. server.
  642. FederationDeniedError: If this destination is not on our
  643. federation whitelist
  644. RequestSendFailed: If there were problems connecting to the
  645. remote, due to e.g. DNS failures, connection timeouts etc.
  646. """
  647. request = MatrixFederationRequest(
  648. method="GET", destination=destination, path=path, query=args
  649. )
  650. response = yield self._send_request_with_optional_trailing_slash(
  651. request,
  652. try_trailing_slash_on_400,
  653. backoff_on_404=False,
  654. ignore_backoff=ignore_backoff,
  655. retry_on_dns_fail=retry_on_dns_fail,
  656. timeout=timeout,
  657. )
  658. body = yield _handle_json_response(
  659. self.reactor, self.default_timeout, request, response
  660. )
  661. return body
  662. @defer.inlineCallbacks
  663. def delete_json(
  664. self,
  665. destination,
  666. path,
  667. long_retries=False,
  668. timeout=None,
  669. ignore_backoff=False,
  670. args={},
  671. ):
  672. """Send a DELETE request to the remote expecting some json response
  673. Args:
  674. destination (str): The remote server to send the HTTP request
  675. to.
  676. path (str): The HTTP path.
  677. long_retries (bool): whether to use the long retry algorithm. See
  678. docs on _send_request for details.
  679. timeout (int|None): number of milliseconds to wait for the response headers
  680. (including connecting to the server), *for each attempt*.
  681. self._default_timeout (60s) by default.
  682. ignore_backoff (bool): true to ignore the historical backoff data and
  683. try the request anyway.
  684. args (dict): query params
  685. Returns:
  686. Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The
  687. result will be the decoded JSON body.
  688. Raises:
  689. HttpResponseException: If we get an HTTP response code >= 300
  690. (except 429).
  691. NotRetryingDestination: If we are not yet ready to retry this
  692. server.
  693. FederationDeniedError: If this destination is not on our
  694. federation whitelist
  695. RequestSendFailed: If there were problems connecting to the
  696. remote, due to e.g. DNS failures, connection timeouts etc.
  697. """
  698. request = MatrixFederationRequest(
  699. method="DELETE", destination=destination, path=path, query=args
  700. )
  701. response = yield self._send_request(
  702. request,
  703. long_retries=long_retries,
  704. timeout=timeout,
  705. ignore_backoff=ignore_backoff,
  706. )
  707. body = yield _handle_json_response(
  708. self.reactor, self.default_timeout, request, response
  709. )
  710. return body
  711. @defer.inlineCallbacks
  712. def get_file(
  713. self,
  714. destination,
  715. path,
  716. output_stream,
  717. args={},
  718. retry_on_dns_fail=True,
  719. max_size=None,
  720. ignore_backoff=False,
  721. ):
  722. """GETs a file from a given homeserver
  723. Args:
  724. destination (str): The remote server to send the HTTP request to.
  725. path (str): The HTTP path to GET.
  726. output_stream (file): File to write the response body to.
  727. args (dict): Optional dictionary used to create the query string.
  728. ignore_backoff (bool): true to ignore the historical backoff data
  729. and try the request anyway.
  730. Returns:
  731. Deferred[tuple[int, dict]]: Resolves with an (int,dict) tuple of
  732. the file length and a dict of the response headers.
  733. Raises:
  734. HttpResponseException: If we get an HTTP response code >= 300
  735. (except 429).
  736. NotRetryingDestination: If we are not yet ready to retry this
  737. server.
  738. FederationDeniedError: If this destination is not on our
  739. federation whitelist
  740. RequestSendFailed: If there were problems connecting to the
  741. remote, due to e.g. DNS failures, connection timeouts etc.
  742. """
  743. request = MatrixFederationRequest(
  744. method="GET", destination=destination, path=path, query=args
  745. )
  746. response = yield self._send_request(
  747. request, retry_on_dns_fail=retry_on_dns_fail, ignore_backoff=ignore_backoff
  748. )
  749. headers = dict(response.headers.getAllRawHeaders())
  750. try:
  751. d = _readBodyToFile(response, output_stream, max_size)
  752. d.addTimeout(self.default_timeout, self.reactor)
  753. length = yield make_deferred_yieldable(d)
  754. except Exception as e:
  755. logger.warning(
  756. "{%s} [%s] Error reading response: %s",
  757. request.txn_id,
  758. request.destination,
  759. e,
  760. )
  761. raise
  762. logger.info(
  763. "{%s} [%s] Completed: %d %s [%d bytes]",
  764. request.txn_id,
  765. request.destination,
  766. response.code,
  767. response.phrase.decode("ascii", errors="replace"),
  768. length,
  769. )
  770. return (length, headers)
  771. class _ReadBodyToFileProtocol(protocol.Protocol):
  772. def __init__(self, stream, deferred, max_size):
  773. self.stream = stream
  774. self.deferred = deferred
  775. self.length = 0
  776. self.max_size = max_size
  777. def dataReceived(self, data):
  778. self.stream.write(data)
  779. self.length += len(data)
  780. if self.max_size is not None and self.length >= self.max_size:
  781. self.deferred.errback(
  782. SynapseError(
  783. 502,
  784. "Requested file is too large > %r bytes" % (self.max_size,),
  785. Codes.TOO_LARGE,
  786. )
  787. )
  788. self.deferred = defer.Deferred()
  789. self.transport.loseConnection()
  790. def connectionLost(self, reason):
  791. if reason.check(ResponseDone):
  792. self.deferred.callback(self.length)
  793. else:
  794. self.deferred.errback(reason)
  795. def _readBodyToFile(response, stream, max_size):
  796. d = defer.Deferred()
  797. response.deliverBody(_ReadBodyToFileProtocol(stream, d, max_size))
  798. return d
  799. def _flatten_response_never_received(e):
  800. if hasattr(e, "reasons"):
  801. reasons = ", ".join(
  802. _flatten_response_never_received(f.value) for f in e.reasons
  803. )
  804. return "%s:[%s]" % (type(e).__name__, reasons)
  805. else:
  806. return repr(e)
  807. def check_content_type_is_json(headers):
  808. """
  809. Check that a set of HTTP headers have a Content-Type header, and that it
  810. is application/json.
  811. Args:
  812. headers (twisted.web.http_headers.Headers): headers to check
  813. Raises:
  814. RequestSendFailed: if the Content-Type header is missing or isn't JSON
  815. """
  816. c_type = headers.getRawHeaders(b"Content-Type")
  817. if c_type is None:
  818. raise RequestSendFailed(RuntimeError("No Content-Type header"), can_retry=False)
  819. c_type = c_type[0].decode("ascii") # only the first header
  820. val, options = cgi.parse_header(c_type)
  821. if val != "application/json":
  822. raise RequestSendFailed(
  823. RuntimeError("Content-Type not application/json: was '%s'" % c_type),
  824. can_retry=False,
  825. )
  826. def encode_query_args(args):
  827. if args is None:
  828. return b""
  829. encoded_args = {}
  830. for k, vs in args.items():
  831. if isinstance(vs, string_types):
  832. vs = [vs]
  833. encoded_args[k] = [v.encode("UTF-8") for v in vs]
  834. query_bytes = urllib.parse.urlencode(encoded_args, True)
  835. return query_bytes.encode("utf8")