MCPcopy
hub / github.com/encode/starlette / MutableHeaders

Class MutableHeaders

starlette/datastructures.py:577–661  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

575
576
577class MutableHeaders(Headers):
578 def __setitem__(self, key: str, value: str) -> None:
579 """
580 Set the header `key` to `value`, removing any duplicate entries.
581 Retains insertion order.
582 """
583 set_key = key.lower().encode("latin-1")
584 set_value = value.encode("latin-1")
585
586 found_indexes: list[int] = []
587 for idx, (item_key, item_value) in enumerate(self._list):
588 if item_key == set_key:
589 found_indexes.append(idx)
590
591 for idx in reversed(found_indexes[1:]):
592 del self._list[idx]
593
594 if found_indexes:
595 idx = found_indexes[0]
596 self._list[idx] = (set_key, set_value)
597 else:
598 self._list.append((set_key, set_value))
599
600 def __delitem__(self, key: str) -> None:
601 """
602 Remove the header `key`.
603 """
604 del_key = key.lower().encode("latin-1")
605
606 pop_indexes: list[int] = []
607 for idx, (item_key, item_value) in enumerate(self._list):
608 if item_key == del_key:
609 pop_indexes.append(idx)
610
611 for idx in reversed(pop_indexes):
612 del self._list[idx]
613
614 def __ior__(self, other: Mapping[str, str]) -> MutableHeaders:
615 if not isinstance(other, Mapping):
616 raise TypeError(f"Expected a mapping but got {other.__class__.__name__}")
617 self.update(other)
618 return self
619
620 def __or__(self, other: Mapping[str, str]) -> MutableHeaders:
621 if not isinstance(other, Mapping):
622 raise TypeError(f"Expected a mapping but got {other.__class__.__name__}")
623 new = self.mutablecopy()
624 new.update(other)
625 return new
626
627 @property
628 def raw(self) -> list[tuple[bytes, bytes]]:
629 return self._list
630
631 def setdefault(self, key: str, value: str) -> str:
632 """
633 If the header `key` does not exist, then set it to `value`.
634 Returns the header value.

Callers 14

headersMethod · 0.90
_handle_single_rangeMethod · 0.90
sendMethod · 0.90
send_wrapperMethod · 0.90
send_with_compressionMethod · 0.90
test_mutable_headersFunction · 0.90

Calls

no outgoing calls