From 941ebe49ffc32c6d67b487094a6f8e1c290e93bc Mon Sep 17 00:00:00 2001 From: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> Date: Thu, 9 Dec 2021 12:58:25 +0100 Subject: Use HTTPStatus constants in place of literals in `synapse.http` (#11543) --- synapse/http/client.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'synapse/http/client.py') diff --git a/synapse/http/client.py b/synapse/http/client.py index b5a2d333a6..fbbeceabeb 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -14,6 +14,7 @@ # limitations under the License. import logging import urllib.parse +from http import HTTPStatus from io import BytesIO from typing import ( TYPE_CHECKING, @@ -280,7 +281,9 @@ class BlacklistingAgentWrapper(Agent): ip_address, self._ip_whitelist, self._ip_blacklist ): logger.info("Blocking access to %s due to blacklist" % (ip_address,)) - e = SynapseError(403, "IP address blocked by IP blacklist entry") + e = SynapseError( + HTTPStatus.FORBIDDEN, "IP address blocked by IP blacklist entry" + ) return defer.fail(Failure(e)) return self._agent.request( @@ -719,7 +722,9 @@ class SimpleHttpClient: if response.code > 299: logger.warning("Got %d when downloading %s" % (response.code, url)) - raise SynapseError(502, "Got error %d" % (response.code,), Codes.UNKNOWN) + raise SynapseError( + HTTPStatus.BAD_GATEWAY, "Got error %d" % (response.code,), Codes.UNKNOWN + ) # TODO: if our Content-Type is HTML or something, just read the first # N bytes into RAM rather than saving it all to disk only to read it @@ -731,12 +736,14 @@ class SimpleHttpClient: ) except BodyExceededMaxSize: raise SynapseError( - 502, + HTTPStatus.BAD_GATEWAY, "Requested file is too large > %r bytes" % (max_size,), Codes.TOO_LARGE, ) except Exception as e: - raise SynapseError(502, ("Failed to download remote body: %s" % e)) from e + raise SynapseError( + HTTPStatus.BAD_GATEWAY, ("Failed to download remote body: %s" % e) + ) from e return ( length, -- cgit 1.5.1 From 0201c6371cdfa0e8245c59686c131e40384bbac2 Mon Sep 17 00:00:00 2001 From: Fr3shTea <31766876+Fr3shTea@users.noreply.github.com> Date: Wed, 5 Jan 2022 11:59:29 +0000 Subject: Fix SimpleHttpClient not sending Accept header in `get_json` (#11677) Co-authored-by: reivilibre --- changelog.d/11677.bugfix | 1 + synapse/http/client.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changelog.d/11677.bugfix (limited to 'synapse/http/client.py') diff --git a/changelog.d/11677.bugfix b/changelog.d/11677.bugfix new file mode 100644 index 0000000000..5691064a30 --- /dev/null +++ b/changelog.d/11677.bugfix @@ -0,0 +1 @@ +Fix wrong variable reference in `SimpleHttpClient.get_json` that results in the absence of the `Accept` header in the request. diff --git a/synapse/http/client.py b/synapse/http/client.py index fbbeceabeb..ca33b45cb2 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -588,7 +588,7 @@ class SimpleHttpClient: if headers: actual_headers.update(headers) # type: ignore - body = await self.get_raw(uri, args, headers=headers) + body = await self.get_raw(uri, args, headers=actual_headers) return json_decoder.decode(body.decode("utf-8")) async def put_json( -- cgit 1.5.1