Skip to content

Commit 0d69a4c

Browse files
committed
Fix: First header was skipped and headers were case-sensitive
1 parent 4a4cdfd commit 0d69a4c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

adafruit_httpserver/request.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,20 @@ class HTTPRequest:
4040
"""HTTP version, e.g. "HTTP/1.1"."""
4141

4242
headers: Dict[str, str]
43-
"""Headers from the request."""
43+
"""
44+
Headers from the request as `dict`.
45+
46+
Values should be accessed using **lower case header names**.
47+
48+
Example::
49+
50+
request.headers
51+
# {'connection': 'keep-alive', 'content-length': '64' ...}
52+
request.headers["content-length"]
53+
# '64'
54+
request.headers["Content-Length"]
55+
# KeyError: 'Content-Length'
56+
"""
4457

4558
raw_request: bytes
4659
"""Raw bytes passed to the constructor."""
@@ -111,4 +124,8 @@ def _parse_headers(header_bytes: bytes) -> Dict[str, str]:
111124
"""Parse HTTP headers from raw request."""
112125
header_lines = header_bytes.decode("utf8").splitlines()[1:]
113126

114-
return dict([header.split(": ", 1) for header in header_lines[1:]])
127+
return {
128+
name.lower(): value
129+
for header_line in header_lines
130+
for name, value in [header_line.split(": ", 1)]
131+
}

0 commit comments

Comments
 (0)