Skip to content
Myr

Move normalize header functions from _utils.py to _models.py (#3382)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Mayank Sinha · committed by GitHub · parent 6622553 · Browse files · 2 files+30 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−49 removed

  • other:+30 added−49 removed

2 files+30 added−49 removed

modified:httpx/_models.py+30 added-22 removed
44 unmodified lines
45)46from ._urls import URL47from ._utils import (48 is_known_encoding,49 normalize_header_key,50 normalize_header_value,49 obfuscate_sensitive_headers,50 parse_content_type_charset,51 parse_header_links,52)5354__all__ = ["Cookies", "Headers", "Request", "Response"]555657def _normalize_header_key(key: str | bytes, encoding: str | None = None) -> bytes:58 """59 Coerce str/bytes into a strictly byte-wise HTTP header key.60 """61 return key if isinstance(key, bytes) else key.encode(encoding or "ascii")626364def _normalize_header_value(value: str | bytes, encoding: str | None = None) -> bytes:65 """66 Coerce str/bytes into a strictly byte-wise HTTP header value.67 """68 if isinstance(value, bytes):69 return value70 if not isinstance(value, str):71 raise TypeError(f"Header value must be str or bytes, not {type(value)}")72 return value.encode(encoding or "ascii")737475class Headers(typing.MutableMapping[str, str]):76 """77 HTTP headers, as a case-insensitive multi-dict.78 """
2 unmodified lines
81 self,82 headers: HeaderTypes | None = None,83 encoding: str | None = None,84 ) -> None:69 if headers is None:70 self._list = [] # type: typing.List[typing.Tuple[bytes, bytes, bytes]]71 elif isinstance(headers, Headers):85 self._list = [] # type: typing.List[typing.Tuple[bytes, bytes, bytes]]8687 if isinstance(headers, Headers):88 self._list = list(headers._list)89 elif isinstance(headers, Mapping):74 self._list = [75 (76 normalize_header_key(k, lower=False, encoding=encoding),77 normalize_header_key(k, lower=True, encoding=encoding),78 normalize_header_value(v, encoding),79 )80 for k, v in headers.items()81 ]82 else:83 self._list = [84 (85 normalize_header_key(k, lower=False, encoding=encoding),86 normalize_header_key(k, lower=True, encoding=encoding),87 normalize_header_value(v, encoding),88 )89 for k, v in headers90 ]90 for k, v in headers.items():91 bytes_key = _normalize_header_key(k, encoding)92 bytes_value = _normalize_header_value(v, encoding)93 self._list.append((bytes_key, bytes_key.lower(), bytes_value))94 elif headers is not None:95 for k, v in headers:96 bytes_key = _normalize_header_key(k, encoding)97 bytes_value = _normalize_header_value(v, encoding)98 self._list.append((bytes_key, bytes_key.lower(), bytes_value))99100 self._encoding = encoding101102 @property
1111 unmodified lines
modified:httpx/_utils.py-27 removed
22 unmodified lines
23 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])24)252627def normalize_header_key(28 value: str | bytes,29 lower: bool,30 encoding: str | None = None,31) -> bytes:32 """33 Coerce str/bytes into a strictly byte-wise HTTP header key.34 """35 if isinstance(value, bytes):36 bytes_value = value37 else:38 bytes_value = value.encode(encoding or "ascii")3940 return bytes_value.lower() if lower else bytes_value414243def normalize_header_value(value: str | bytes, encoding: str | None = None) -> bytes:44 """45 Coerce str/bytes into a strictly byte-wise HTTP header value.46 """47 if isinstance(value, bytes):48 return value49 if not isinstance(value, str):50 raise TypeError(f"Header value must be str or bytes, not {type(value)}")51 return value.encode(encoding or "ascii")525327def primitive_value_to_str(value: PrimitiveData) -> str:28 """29 Coerce a primitive data type into a string value.30
342 unmodified lines