diff --git a/django/http/response.py b/django/http/response.py index fb379f98a7..fae91b3f05 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -43,22 +43,32 @@ class ResponseHeaders(CaseInsensitiveMapping): `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and `value` can't be represented in the given charset, apply MIME-encoding. """ - if not isinstance(value, (bytes, str)): - value = str(value) - if (isinstance(value, bytes) and (b"\n" in value or b"\r" in value)) or ( - isinstance(value, str) and ("\n" in value or "\r" in value) - ): - raise BadHeaderError( - "Header values can't contain newlines (got %r)" % value - ) try: if isinstance(value, str): # Ensure string is valid in given charset value.encode(charset) - else: + elif isinstance(value, bytes): # Convert bytestring using given charset value = value.decode(charset) + else: + value = str(value) + # Ensure string is valid in given charset. + value.encode(charset) + if "\n" in value or "\r" in value: + raise BadHeaderError( + f"Header values can't contain newlines (got {value!r})" + ) except UnicodeError as e: + # Encoding to a string of the specified charset failed, but we + # don't know what type that value was, or if it contains newlines, + # which we may need to check for before sending it to be + # encoded for multiple character sets. + if (isinstance(value, bytes) and (b"\n" in value or b"\r" in value)) or ( + isinstance(value, str) and ("\n" in value or "\r" in value) + ): + raise BadHeaderError( + f"Header values can't contain newlines (got {value!r})" + ) from e if mime_encode: value = Header(value, "utf-8", maxlinelen=sys.maxsize).encode() else: