Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: allow 'Content-Length' = "0" #1617

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
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
7 changes: 6 additions & 1 deletion src/websockets/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ def parse(
raise NotImplementedError("transfer codings aren't supported")

if "Content-Length" in headers:
raise ValueError("unsupported request body")
content_length = headers["Content-Length"]
if content_length != "0":
raise ValueError(
f"unsupported request body as 'Content-Length' is "
f"non-zero: {content_length}"
)

return cls(path, headers)

Expand Down
20 changes: 19 additions & 1 deletion tests/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,27 @@ def test_parse_body(self):
next(self.parse())
self.assertEqual(
str(raised.exception),
"unsupported request body",
"unsupported request body as 'Content-Length' is non-zero: 3",
)

def test_parse_body_content_length_zero(self):
# Example from the protocol overview in RFC 6455
self.reader.feed_data(
b"GET /chat HTTP/1.1\r\n"
b"Host: server.example.com\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
b"Origin: http://example.com\r\n"
b"Sec-WebSocket-Protocol: chat, superchat\r\n"
b"Sec-WebSocket-Version: 13\r\n"
b"Content-Length: 0\r\n"
b"\r\n"
)
request = self.assertGeneratorReturns(self.parse())
self.assertEqual(request.path, "/chat")
self.assertEqual(request.headers["Content-Length"], "0")

def test_parse_body_with_transfer_encoding(self):
self.reader.feed_data(b"GET / HTTP/1.1\r\nTransfer-Encoding: compress\r\n\r\n")
with self.assertRaises(NotImplementedError) as raised:
Expand Down