MCPcopy
hub / github.com/urllib3/urllib3 / HTTPHeaderDictItemView

Class HTTPHeaderDictItemView

src/urllib3/_collections.py:156–201  ·  view source on GitHub ↗

HTTPHeaderDict is unusual for a Mapping[str, str] in that it has two modes of address. If we directly try to get an item with a particular name, we will get a string back that is the concatenated version of all the values: >>> d['X-Header-Name'] 'Value1, Value2, Value3'

Source from the content-addressed store, hash-verified

154
155
156class HTTPHeaderDictItemView(set[tuple[str, str]]):
157 """
158 HTTPHeaderDict is unusual for a Mapping[str, str] in that it has two modes of
159 address.
160
161 If we directly try to get an item with a particular name, we will get a string
162 back that is the concatenated version of all the values:
163
164 >>> d['X-Header-Name']
165 'Value1, Value2, Value3'
166
167 However, if we iterate over an HTTPHeaderDict's items, we will optionally combine
168 these values based on whether combine=True was called when building up the dictionary
169
170 >>> d = HTTPHeaderDict({"A": "1", "B": "foo"})
171 >>> d.add("A", "2", combine=True)
172 >>> d.add("B", "bar")
173 >>> list(d.items())
174 [
175 ('A', '1, 2'),
176 ('B', 'foo'),
177 ('B', 'bar'),
178 ]
179
180 This class conforms to the interface required by the MutableMapping ABC while
181 also giving us the nonstandard iteration behavior we want; items with duplicate
182 keys, ordered by time of first insertion.
183 """
184
185 _headers: HTTPHeaderDict
186
187 def __init__(self, headers: HTTPHeaderDict) -> None:
188 self._headers = headers
189
190 def __len__(self) -> int:
191 return len(list(self._headers.iteritems()))
192
193 def __iter__(self) -> typing.Iterator[tuple[str, str]]:
194 return self._headers.iteritems()
195
196 def __contains__(self, item: object) -> bool:
197 if isinstance(item, tuple) and len(item) == 2:
198 passed_key, passed_val = item
199 if isinstance(passed_key, str) and isinstance(passed_val, str):
200 return self._headers._has_value_for_header(passed_key, passed_val)
201 return False
202
203
204class HTTPHeaderDict(typing.MutableMapping[str, str]):

Callers 1

itemsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected