MCPcopy
hub / github.com/encode/httpx / Headers

Class Headers

httpx/_models.py:139–379  ·  view source on GitHub ↗

HTTP headers, as a case-insensitive multi-dict.

Source from the content-addressed store, hash-verified

137
138
139class Headers(typing.MutableMapping[str, str]):
140 """
141 HTTP headers, as a case-insensitive multi-dict.
142 """
143
144 def __init__(
145 self,
146 headers: HeaderTypes | None = None,
147 encoding: str | None = None,
148 ) -> None:
149 self._list = [] # type: typing.List[typing.Tuple[bytes, bytes, bytes]]
150
151 if isinstance(headers, Headers):
152 self._list = list(headers._list)
153 elif isinstance(headers, Mapping):
154 for k, v in headers.items():
155 bytes_key = _normalize_header_key(k, encoding)
156 bytes_value = _normalize_header_value(v, encoding)
157 self._list.append((bytes_key, bytes_key.lower(), bytes_value))
158 elif headers is not None:
159 for k, v in headers:
160 bytes_key = _normalize_header_key(k, encoding)
161 bytes_value = _normalize_header_value(v, encoding)
162 self._list.append((bytes_key, bytes_key.lower(), bytes_value))
163
164 self._encoding = encoding
165
166 @property
167 def encoding(self) -> str:
168 """
169 Header encoding is mandated as ascii, but we allow fallbacks to utf-8
170 or iso-8859-1.
171 """
172 if self._encoding is None:
173 for encoding in ["ascii", "utf-8"]:
174 for key, value in self.raw:
175 try:
176 key.decode(encoding)
177 value.decode(encoding)
178 except UnicodeDecodeError:
179 break
180 else:
181 # The else block runs if 'break' did not occur, meaning
182 # all values fitted the encoding.
183 self._encoding = encoding
184 break
185 else:
186 # The ISO-8859-1 encoding covers all 256 code points in a byte,
187 # so will never raise decode errors.
188 self._encoding = "iso-8859-1"
189 return self._encoding
190
191 @encoding.setter
192 def encoding(self, value: str) -> None:
193 self._encoding = value
194
195 @property
196 def raw(self) -> list[tuple[bytes, bytes]]:

Callers 11

updateMethod · 0.85
copyMethod · 0.85
__eq__Method · 0.85
__init__Method · 0.85
_prepareMethod · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
headersMethod · 0.85
_merge_headersMethod · 0.85
_redirect_headersMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected