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 fileTake it
Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 1why, one step back1import codecsMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 2–4why, one step back2import typing34Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 5why, one step back5def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 6–10why, one step back6 """7 Coerce str/bytes into a strictly byte-wise HTTP header key.8 """9 if isinstance(value, bytes):10 return value.lower()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 11why, one step back11 return value.encode(encoding or "ascii").lower()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 12–13why, one step back1213Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 14why, one step back14def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 15–19why, one step back15 """16 Coerce str/bytes into a strictly byte-wise HTTP header value.17 """18 if isinstance(value, bytes):19 return valueRejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 20why, one step back20 return value.encode(encoding or "ascii")Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 21–23why, one step back212223def is_known_encoding(encoding: str) -> bool:Reorganise structureTom Christiebefore adoption0c6f618
lines 24–26why, one step back24 """25 Return `True` if `encoding` is a known codec.26 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 27–31why, one step back27 try:28 codecs.lookup(encoding)29 except LookupError:30 return False31 return True