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
Provenance
- Commit
83a8518- Tree
60178e9- Author
Mayank Sinha <sinha.mayank900@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
6622553
2 files changed+30 added−49 removed
modified:httpx/_models.py+30 added-22 removed
44 unmodified lines45 )46 from ._urls import URL47 from ._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 )53 54 __all__ = ["Cookies", "Headers", "Request", "Response"]55 56 57+def _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")62+63+64+def _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")73+74+75 class Headers(typing.MutableMapping[str, str]):76 """77 HTTP headers, as a case-insensitive multi-dict.78 """2 unmodified lines81 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]]86+87+ 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))99 100 self._encoding = encoding101 102 @property1111 unmodified linesmodified:httpx/_utils.py-27 removed
22 unmodified lines23 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])24 )25 26 27-def 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")39-40- return bytes_value.lower() if lower else bytes_value41-42-43-def 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")52-53-27 def primitive_value_to_str(value: PrimitiveData) -> str:28 """29 Coerce a primitive data type into a string value.30 342 unmodified lines