Return 502, not 500 when a remote resource uses invalid Content-Type

pull/8719/head
Andrew Morgan 2020-11-04 18:30:05 +00:00
parent 1eb9de90c0
commit 1bf6e69d96
1 changed files with 9 additions and 2 deletions

View File

@ -1063,13 +1063,20 @@ def check_content_type_is_json(headers):
"""
c_type = headers.getRawHeaders(b"Content-Type")
if c_type is None:
raise RequestSendFailed(RuntimeError("No Content-Type header"), can_retry=False)
raise RequestSendFailed(
SynapseError(502, "No Content-Type header received from remote server"),
can_retry=False,
)
c_type = c_type[0].decode("ascii") # only the first header
val, options = cgi.parse_header(c_type)
if val != "application/json":
raise RequestSendFailed(
RuntimeError("Content-Type not application/json: was '%s'" % c_type),
SynapseError(
502,
"Remote server sent Content-Type header of '%s', not 'application/json'"
% c_type,
),
can_retry=False,
)