Skip to content

Commit 1e791e2

Browse files
tomchristiepgjones
authored andcommitted
Update docs, docstrings
1 parent e1340f4 commit 1e791e2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

docs/source/api.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ have a newline inside a header value, and ``Content-Length: hello`` is
128128
an error because `Content-Length` should always be an integer. We may
129129
add additional checks in the future.
130130

131+
While we make sure to expose header names as lowercased bytes, we also
132+
preserve the original header casing that is used. Compliant HTTP
133+
agents should always treat headers in a case insensitive manner, but
134+
this may not always be the case. When sending bytes over the wire we
135+
use send headers preserving whatever original header casing is used.
136+
137+
It is possible to access the headers in their raw original casing,
138+
which may be useful for some user output or debugging purposes.
139+
140+
.. ipython:: python
141+
142+
original_headers = [("Host", "example.com")]
143+
req = h11.Request(method="GET", target="/", headers=original_headers)
144+
req.headers.raw_items()
145+
131146
.. _http_version-format:
132147

133148
It's not just headers we normalize to being byte-strings: the same

h11/_headers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def __eq__(self, other):
103103
def __len__(self):
104104
return len(self._full_items)
105105

106+
def __repr__(self):
107+
return "<Headers(%s)>" % repr(list(self))
108+
109+
def __getitem__(self, idx):
110+
_, name, value = self._full_items[idx]
111+
return (name, value)
112+
106113
def raw_items(self):
107114
return [(raw_name, value) for raw_name, _, value in self._full_items]
108115

h11/tests/test_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def test_header_casing():
173173
http_version="1.1",
174174
)
175175
assert len(r.headers) == 2
176+
assert r.headers[0] == (b"host", b"example.org")
176177
assert r.headers == [(b"host", b"example.org"), (b"connection", b"keep-alive")]
177178
assert r.headers.raw_items() == [
178179
(b"Host", b"example.org"),

0 commit comments

Comments
 (0)