Refs -- Optimized ResponseHeaders._convert_to_charset() by reducing the type-checking duplication.
In the common case, where keys and values are be encoded into
ascii/latin-1, defer the checking for newlines until it's been
successfully coerced to a string.
@@ -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:
