Skip to content

Content length handling #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions h11/_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def raw_items(self):

def normalize_and_validate(headers, _parsed=False):
new_headers = []
saw_content_length = False
seen_content_length = None
saw_transfer_encoding = False
for name, value in headers:
# For headers coming out of the parser, we can safely skip some steps,
Expand All @@ -132,11 +132,17 @@ def normalize_and_validate(headers, _parsed=False):
raw_name = name
name = name.lower()
if name == b"content-length":
if saw_content_length:
raise LocalProtocolError("multiple Content-Length headers")
lengths = set(length.strip() for length in value.split(b","))
if len(lengths) != 1:
raise LocalProtocolError("conflicting Content-Length headers")
value = lengths.pop()
validate(_content_length_re, value, "bad Content-Length")
saw_content_length = True
if name == b"transfer-encoding":
if seen_content_length is None:
seen_content_length = value
new_headers.append((raw_name, name, value))
elif seen_content_length != value:
raise LocalProtocolError("conflicting Content-Length headers")
elif name == b"transfer-encoding":
# "A server that receives a request message with a transfer coding
# it does not understand SHOULD respond with 501 (Not
# Implemented)."
Expand All @@ -154,7 +160,9 @@ def normalize_and_validate(headers, _parsed=False):
error_status_hint=501,
)
saw_transfer_encoding = True
new_headers.append((raw_name, name, value))
new_headers.append((raw_name, name, value))
else:
new_headers.append((raw_name, name, value))
return Headers(new_headers)


Expand Down
12 changes: 12 additions & 0 deletions h11/tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def test_normalize_and_validate():
normalize_and_validate([("Content-Length", "1x")])
with pytest.raises(LocalProtocolError):
normalize_and_validate([("Content-Length", "1"), ("Content-Length", "2")])
assert normalize_and_validate(
[("Content-Length", "0"), ("Content-Length", "0")]
) == [(b"content-length", b"0")]
assert normalize_and_validate([("Content-Length", "0 , 0")]) == [
(b"content-length", b"0")
]
with pytest.raises(LocalProtocolError):
normalize_and_validate(
[("Content-Length", "1"), ("Content-Length", "1"), ("Content-Length", "2")]
)
with pytest.raises(LocalProtocolError):
normalize_and_validate([("Content-Length", "1 , 1,2")])

# transfer-encoding
assert normalize_and_validate([("Transfer-Encoding", "chunked")]) == [
Expand Down