* Add top-level API * Add tests for top-level API * Further work towards parallel support * StatusCode tweaks * Drop erronous commit
32 lines
784 B
Python
32 lines
784 B
Python
import codecs
|
|
import typing
|
|
|
|
|
|
def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:
|
|
"""
|
|
Coerce str/bytes into a strictly byte-wise HTTP header key.
|
|
"""
|
|
if isinstance(value, bytes):
|
|
return value.lower()
|
|
return value.encode(encoding or "ascii").lower()
|
|
|
|
|
|
def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:
|
|
"""
|
|
Coerce str/bytes into a strictly byte-wise HTTP header value.
|
|
"""
|
|
if isinstance(value, bytes):
|
|
return value
|
|
return value.encode(encoding or "ascii")
|
|
|
|
|
|
def is_known_encoding(encoding: str) -> bool:
|
|
"""
|
|
Return `True` if `encoding` is a known codec.
|
|
"""
|
|
try:
|
|
codecs.lookup(encoding)
|
|
except LookupError:
|
|
return False
|
|
return True
|