Skip to content
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

Normalize Content-Length into a single value if duplicated exactly #94

Closed
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
11 changes: 7 additions & 4 deletions h11/_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

def normalize_and_validate(headers, _parsed=False):
new_headers = []
saw_content_length = False
saw_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 @@ -77,10 +77,13 @@ def normalize_and_validate(headers, _parsed=False):
validate(_field_value_re, value, "Illegal header value {!r}", value)
name = name.lower()
if name == b"content-length":
if saw_content_length:
raise LocalProtocolError("multiple Content-Length headers")
validate(_content_length_re, value, "bad Content-Length")
saw_content_length = True
if saw_content_length:
Copy link
Member

@sethmlarson sethmlarson Jan 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd also like to handle Content-Length: x, x, x, ... -> Content-Length: x @njsmith?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it also be if saw_content_length is not None, say if one header is Content-Length: 0?

if saw_content_length != value:
raise LocalProtocolError("conflicting Content-Length headers")
# We have a duplicate Content-Length, so collapse it
new_headers.remove((name, saw_content_length))
saw_content_length = value
if name == b"transfer-encoding":
# "A server that receives a request message with a transfer coding
# it does not understand SHOULD respond with 501 (Not
Expand Down
7 changes: 7 additions & 0 deletions h11/tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ 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", "1"), ("Content-Length", "1")]
) == [(b"content-length", b"1")]
with pytest.raises(LocalProtocolError):
normalize_and_validate(
[("Content-Length", "1"), ("Content-Length", "1"), ("Content-Length", "2")]
)

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