Refs #33546 -- 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. Co-authored-by: Nick Pope <nick@nickpope.me.uk>
This commit is contained in:
parent
e0b197c63c
commit
51f896fe25
|
@ -43,22 +43,32 @@ class ResponseHeaders(CaseInsensitiveMapping):
|
||||||
`charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and
|
`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.
|
`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:
|
try:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
# Ensure string is valid in given charset
|
# Ensure string is valid in given charset
|
||||||
value.encode(charset)
|
value.encode(charset)
|
||||||
else:
|
elif isinstance(value, bytes):
|
||||||
# Convert bytestring using given charset
|
# Convert bytestring using given charset
|
||||||
value = value.decode(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:
|
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:
|
if mime_encode:
|
||||||
value = Header(value, "utf-8", maxlinelen=sys.maxsize).encode()
|
value = Header(value, "utf-8", maxlinelen=sys.maxsize).encode()
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue