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

Class Headers

starlette/datastructures.py:500–574  ·  view source on GitHub ↗

An immutable, case-insensitive multidict.

Source from the content-addressed store, hash-verified

498
499
500class Headers(Mapping[str, str]):
501 """
502 An immutable, case-insensitive multidict.
503 """
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, 'Cannot set both "headers" and "raw".'
514 assert scope is None, 'Cannot set both "headers" and "scope".'
515 self._list = [(key.lower().encode("latin-1"), value.encode("latin-1")) for key, value in headers.items()]
516 elif raw is not None:
517 assert scope is None, 'Cannot set both "raw" and "scope".'
518 self._list = raw
519 elif scope is not None:
520 # scope["headers"] isn't necessarily a list
521 # it might be a tuple or other iterable
522 self._list = scope["headers"] = list(scope["headers"])
523
524 @property
525 def raw(self) -> list[tuple[bytes, bytes]]:
526 return list(self._list)
527
528 def keys(self) -> list[str]: # type: ignore[override]
529 return [key.decode("latin-1") for key, value in self._list]
530
531 def values(self) -> list[str]: # type: ignore[override]
532 return [value.decode("latin-1") for key, value in self._list]
533
534 def items(self) -> list[tuple[str, str]]: # type: ignore[override]
535 return [(key.decode("latin-1"), value.decode("latin-1")) for key, value in self._list]
536
537 def getlist(self, key: str) -> list[str]:
538 get_header_key = key.lower().encode("latin-1")
539 return [item_value.decode("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("latin-1")
546 for header_key, header_value in self._list:
547 if header_key == get_header_key:
548 return header_value.decode("latin-1")
549 raise KeyError(key)
550
551 def __contains__(self, key: Any) -> bool:
552 get_header_key = key.lower().encode("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

Callers 15

file_responseMethod · 0.90
on_headers_finishedMethod · 0.90
__call__Method · 0.90
headersMethod · 0.90
matchesMethod · 0.90
__call__Method · 0.90
__call__Method · 0.90
send_with_compressionMethod · 0.90
__call__Method · 0.90
sendFunction · 0.90
test_headersFunction · 0.90

Calls

no outgoing calls