Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

42 lines · 1.0 KiB · Python

Last changed by

Reorganise structureTom Christiebefore adoption0c6f618

History of this file

Take it

1import codecs2import http3import typing456def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:7    """8    Coerce str/bytes into a strictly byte-wise HTTP header key.9    """10    if isinstance(value, bytes):11        return value.lower()12    return value.encode(encoding or "ascii").lower()131415def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:16    """17    Coerce str/bytes into a strictly byte-wise HTTP header value.18    """19    if isinstance(value, bytes):20        return value21    return value.encode(encoding or "ascii")222324def get_reason_phrase(status_code: int) -> str:25    """26    Return an HTTP reason phrase such as "OK" for 200, or "Not Found" for 404.27    """28    try:29        return http.HTTPStatus(status_code).phrase30    except ValueError as exc:31        return ""323334def is_known_encoding(encoding: str) -> bool:35    """36    Return `True` if `encoding` is a known codec.37    """38    try:39        codecs.lookup(encoding)40    except LookupError:41        return False42    return True