MCPcopy
hub / github.com/urllib3/urllib3 / add

Method add

src/urllib3/_collections.py:306–339  ·  view source on GitHub ↗

Adds a (name, value) pair, doesn't overwrite the value if it already exists. If this is called with combine=True, instead of adding a new header value as a distinct item during iteration, this will instead append the value to any existing header value with a comma. I

(self, key: str, val: str, *, combine: bool = False)

Source from the content-addressed store, hash-verified

304 pass
305
306 def add(self, key: str, val: str, *, combine: bool = False) -> None:
307 """Adds a (name, value) pair, doesn't overwrite the value if it already
308 exists.
309
310 If this is called with combine=True, instead of adding a new header value
311 as a distinct item during iteration, this will instead append the value to
312 any existing header value with a comma. If no existing header value exists
313 for the key, then the value will simply be added, ignoring the combine parameter.
314
315 >>> headers = HTTPHeaderDict(foo='bar')
316 >>> headers.add('Foo', 'baz')
317 >>> headers['foo']
318 'bar, baz'
319 >>> list(headers.items())
320 [('foo', 'bar'), ('foo', 'baz')]
321 >>> headers.add('foo', 'quz', combine=True)
322 >>> list(headers.items())
323 [('foo', 'bar, baz, quz')]
324 """
325 # avoid a bytes/str comparison by decoding before httplib
326 if isinstance(key, bytes):
327 key = key.decode("latin-1")
328 key_lower = key.lower()
329 new_vals = [key, val]
330 # Keep the common case aka no item present as fast as possible
331 vals = self._container.setdefault(key_lower, new_vals)
332 if new_vals is not vals:
333 # if there are values here, then there is at least the initial
334 # key/value pair
335 assert len(vals) >= 2
336 if combine:
337 vals[-1] = vals[-1] + ", " + val
338 else:
339 vals.append(val)
340
341 def extend(self, *args: ValidHTTPHeaderSource, **kwargs: str) -> None:
342 """Generic import function for any type of header-like object.

Calls 1

setdefaultMethod · 0.80