utils.py
This file
Path
httpcore/utils.py
39 lines · 980 bytes · Python
Last changed by
Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
History of this fileTake it
Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 1why, one step back1import codecsFinessing interfaceTom Christiebefore adoptiond7619a9
line 2why, one step back2import httpMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 3–5why, one step back3import typing45Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 6why, one step back6def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 7–11why, one step back7 """8 Coerce str/bytes into a strictly byte-wise HTTP header key.9 """10 if isinstance(value, bytes):11 return value.lower()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 12why, one step back12 return value.encode(encoding or "ascii").lower()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 13–14why, one step back1314Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 15why, one step back15def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 16–20why, one step back16 """17 Coerce str/bytes into a strictly byte-wise HTTP header value.18 """19 if isinstance(value, bytes):20 return valueRejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 21why, one step back21 return value.encode(encoding or "ascii")Finessing interfaceTom Christiebefore adoptiond7619a9
lines 22–31why, one step back222324def 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 ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 32–39why, one step back323334def is_known_encoding(encoding: str) -> bool:35 try:36 codecs.lookup(encoding)37 except LookupError:38 return False39 return True