Skip to content

Commit fa45092

Browse files
committed
Simplify previous commit.
(Mostly a matter of taste.) Refs #1617.
1 parent 6385bdd commit fa45092

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

Diff for: src/websockets/http11.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ def parse(
159159
raise NotImplementedError("transfer codings aren't supported")
160160

161161
if "Content-Length" in headers:
162-
content_length = headers["Content-Length"]
163-
if content_length != "0":
164-
raise ValueError(
165-
f"unsupported request body as 'Content-Length' is "
166-
f"non-zero: {content_length}"
167-
)
162+
# Some devices send a Content-Length header with a value of 0.
163+
# This raises ValueError if Content-Length isn't an integer too.
164+
if int(headers["Content-Length"]) != 0:
165+
raise ValueError("unsupported request body")
168166

169167
return cls(path, headers)
170168

Diff for: tests/test_http11.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,12 @@ def test_parse_body(self):
8383
next(self.parse())
8484
self.assertEqual(
8585
str(raised.exception),
86-
"unsupported request body as 'Content-Length' is non-zero: 3",
86+
"unsupported request body",
8787
)
8888

8989
def test_parse_body_content_length_zero(self):
90-
# Example from the protocol overview in RFC 6455
91-
self.reader.feed_data(
92-
b"GET /chat HTTP/1.1\r\n"
93-
b"Host: server.example.com\r\n"
94-
b"Upgrade: websocket\r\n"
95-
b"Connection: Upgrade\r\n"
96-
b"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
97-
b"Origin: http://example.com\r\n"
98-
b"Sec-WebSocket-Protocol: chat, superchat\r\n"
99-
b"Sec-WebSocket-Version: 13\r\n"
100-
b"Content-Length: 0\r\n"
101-
b"\r\n"
102-
)
90+
self.reader.feed_data(b"GET / HTTP/1.1\r\nContent-Length: 0\r\n\r\n")
10391
request = self.assertGeneratorReturns(self.parse())
104-
self.assertEqual(request.path, "/chat")
10592
self.assertEqual(request.headers["Content-Length"], "0")
10693

10794
def test_parse_body_with_transfer_encoding(self):

0 commit comments

Comments
 (0)