Header class which allows both convenient access to individual headers as well as direct access to the underlying raw data. Provides a full dictionary interface. Create headers with keyword arguments: >>> h = Headers(host="example.com", content_type="application/xml") Headers
| 47 | |
| 48 | # This cannot be easily typed with mypy yet, so we just specify MultiDict without concrete types. |
| 49 | class Headers(multidict.MultiDict): # type: ignore |
| 50 | """ |
| 51 | Header class which allows both convenient access to individual headers as well as |
| 52 | direct access to the underlying raw data. Provides a full dictionary interface. |
| 53 | |
| 54 | Create headers with keyword arguments: |
| 55 | >>> h = Headers(host="example.com", content_type="application/xml") |
| 56 | |
| 57 | Headers mostly behave like a normal dict: |
| 58 | >>> h["Host"] |
| 59 | "example.com" |
| 60 | |
| 61 | Headers are case insensitive: |
| 62 | >>> h["host"] |
| 63 | "example.com" |
| 64 | |
| 65 | Headers can also be created from a list of raw (header_name, header_value) byte tuples: |
| 66 | >>> h = Headers([ |
| 67 | (b"Host",b"example.com"), |
| 68 | (b"Accept",b"text/html"), |
| 69 | (b"accept",b"application/xml") |
| 70 | ]) |
| 71 | |
| 72 | Multiple headers are folded into a single header as per RFC 7230: |
| 73 | >>> h["Accept"] |
| 74 | "text/html, application/xml" |
| 75 | |
| 76 | Setting a header removes all existing headers with the same name: |
| 77 | >>> h["Accept"] = "application/text" |
| 78 | >>> h["Accept"] |
| 79 | "application/text" |
| 80 | |
| 81 | `bytes(h)` returns an HTTP/1 header block: |
| 82 | >>> print(bytes(h)) |
| 83 | Host: example.com |
| 84 | Accept: application/text |
| 85 | |
| 86 | For full control, the raw header fields can be accessed: |
| 87 | >>> h.fields |
| 88 | |
| 89 | Caveats: |
| 90 | - For use with the "Set-Cookie" and "Cookie" headers, either use `Response.cookies` or see `Headers.get_all`. |
| 91 | """ |
| 92 | |
| 93 | def __init__(self, fields: Iterable[tuple[bytes, bytes]] = (), **headers): |
| 94 | """ |
| 95 | *Args:* |
| 96 | - *fields:* (optional) list of ``(name, value)`` header byte tuples, |
| 97 | e.g. ``[(b"Host", b"example.com")]``. All names and values must be bytes. |
| 98 | - *\\*\\*headers:* Additional headers to set. Will overwrite existing values from `fields`. |
| 99 | For convenience, underscores in header names will be transformed to dashes - |
| 100 | this behaviour does not extend to other methods. |
| 101 | |
| 102 | If ``**headers`` contains multiple keys that have equal ``.lower()`` representations, |
| 103 | the behavior is undefined. |
| 104 | """ |
| 105 | super().__init__(fields) |
| 106 |
no outgoing calls
searching dependent graphs…