An immutable, case-insensitive multidict.
| 498 | |
| 499 | |
| 500 | class Headers(Mapping[str, str]): |
| 501 | class="st">""" |
| 502 | An immutable, case-insensitive multidict. |
| 503 | class="st">""" |
| 504 | |
| 505 | def __init__( |
| 506 | self, |
| 507 | headers: Mapping[str, str] | None = None, |
| 508 | raw: list[tuple[bytes, bytes]] | None = None, |
| 509 | scope: MutableMapping[str, Any] | None = None, |
| 510 | ) -> None: |
| 511 | self._list: list[tuple[bytes, bytes]] = [] |
| 512 | if headers is not None: |
| 513 | assert raw is None, &class="cm">#x27;Cannot set both class="st">"headers" and class="st">"raw".' |
| 514 | assert scope is None, &class="cm">#x27;Cannot set both class="st">"headers" and class="st">"scope".' |
| 515 | self._list = [(key.lower().encode(class="st">"latin-1"), value.encode(class="st">"latin-1")) for key, value in headers.items()] |
| 516 | elif raw is not None: |
| 517 | assert scope is None, &class="cm">#x27;Cannot set both class="st">"raw" and class="st">"scope".' |
| 518 | self._list = raw |
| 519 | elif scope is not None: |
| 520 | class="cm"># scope[class="st">"headers"] isn't necessarily a list |
| 521 | class="cm"># it might be a tuple or other iterable |
| 522 | self._list = scope[class="st">"headers"] = list(scope[class="st">"headers"]) |
| 523 | |
| 524 | @property |
| 525 | def raw(self) -> list[tuple[bytes, bytes]]: |
| 526 | return list(self._list) |
| 527 | |
| 528 | def keys(self) -> list[str]: class="cm"># type: ignore[override] |
| 529 | return [key.decode(class="st">"latin-1") for key, value in self._list] |
| 530 | |
| 531 | def values(self) -> list[str]: class="cm"># type: ignore[override] |
| 532 | return [value.decode(class="st">"latin-1") for key, value in self._list] |
| 533 | |
| 534 | def items(self) -> list[tuple[str, str]]: class="cm"># type: ignore[override] |
| 535 | return [(key.decode(class="st">"latin-1"), value.decode(class="st">"latin-1")) for key, value in self._list] |
| 536 | |
| 537 | def getlist(self, key: str) -> list[str]: |
| 538 | get_header_key = key.lower().encode(class="st">"latin-1") |
| 539 | return [item_value.decode(class="st">"latin-1") for item_key, item_value in self._list if item_key == get_header_key] |
| 540 | |
| 541 | def mutablecopy(self) -> MutableHeaders: |
| 542 | return MutableHeaders(raw=self._list[:]) |
| 543 | |
| 544 | def __getitem__(self, key: str) -> str: |
| 545 | get_header_key = key.lower().encode(class="st">"latin-1") |
| 546 | for header_key, header_value in self._list: |
| 547 | if header_key == get_header_key: |
| 548 | return header_value.decode(class="st">"latin-1") |
| 549 | raise KeyError(key) |
| 550 | |
| 551 | def __contains__(self, key: Any) -> bool: |
| 552 | get_header_key = key.lower().encode(class="st">"latin-1") |
| 553 | for header_key, header_value in self._list: |
| 554 | if header_key == get_header_key: |
| 555 | return True |
| 556 | return False |
| 557 |
no outgoing calls