Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

31 lines · 784 bytes · Python

Last changed by

Status code tweaks (#77)Tom Christiebefore adoptionc12c271

History of this file

Take it

1import codecs2import typing345def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:6    """7    Coerce str/bytes into a strictly byte-wise HTTP header key.8    """9    if isinstance(value, bytes):10        return value.lower()11    return value.encode(encoding or "ascii").lower()121314def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:15    """16    Coerce str/bytes into a strictly byte-wise HTTP header value.17    """18    if isinstance(value, bytes):19        return value20    return value.encode(encoding or "ascii")212223def is_known_encoding(encoding: str) -> bool:24    """25    Return `True` if `encoding` is a known codec.26    """27    try:28        codecs.lookup(encoding)29    except LookupError:30        return False31    return True