Skip to content
Myr

utils.py

This file

Path

httpcore/utils.py

39 lines · 980 bytes · Python

Last changed by

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

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, eg. "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    try:36        codecs.lookup(encoding)37    except LookupError:38        return False39    return True