Skip to content

Commit 77cf665

Browse files
committed
Deduplicate include_body and allow_http10.
The use case for these parameters is the same.
1 parent 5cd37cd commit 77cf665

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

src/websockets/asyncio/client.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,7 @@ def __init__(
754754
self.reader.read_line,
755755
self.reader.read_exact,
756756
self.reader.read_to_eof,
757-
include_body=False,
758-
allow_http10=True,
757+
proxy=True,
759758
)
760759

761760
loop = asyncio.get_running_loop()

src/websockets/http11.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ def parse(
213213
read_line: Callable[[int], Generator[None, None, bytes]],
214214
read_exact: Callable[[int], Generator[None, None, bytes]],
215215
read_to_eof: Callable[[int], Generator[None, None, bytes]],
216-
include_body: bool = True,
217-
allow_http10: bool = False,
216+
proxy: bool = False,
218217
) -> Generator[None, None, Response]:
219218
"""
220219
Parse a WebSocket handshake response.
@@ -250,7 +249,7 @@ def parse(
250249
protocol, raw_status_code, raw_reason = status_line.split(b" ", 2)
251250
except ValueError: # not enough values to unpack (expected 3, got 1-2)
252251
raise ValueError(f"invalid HTTP status line: {d(status_line)}") from None
253-
if allow_http10: # some proxies still use HTTP/1.0
252+
if proxy: # some proxies still use HTTP/1.0
254253
if protocol not in [b"HTTP/1.1", b"HTTP/1.0"]:
255254
raise ValueError(
256255
f"unsupported protocol; expected HTTP/1.1 or HTTP/1.0: "
@@ -277,12 +276,12 @@ def parse(
277276

278277
headers = yield from parse_headers(read_line)
279278

280-
if include_body:
279+
if proxy:
280+
body = b""
281+
else:
281282
body = yield from read_body(
282283
status_code, headers, read_line, read_exact, read_to_eof
283284
)
284-
else:
285-
body = b""
286285

287286
return cls(status_code, reason, headers, body)
288287

src/websockets/sync/client.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ def read_connect_response(sock: socket.socket, deadline: Deadline) -> Response:
498498
reader.read_line,
499499
reader.read_exact,
500500
reader.read_to_eof,
501-
include_body=False,
502-
allow_http10=True,
501+
proxy=True,
503502
)
504503
try:
505504
while True:

tests/test_http11.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -328,22 +328,22 @@ def test_parse_body_not_modified(self):
328328
response = self.assertGeneratorReturns(self.parse())
329329
self.assertEqual(response.body, b"")
330330

331-
def test_parse_without_body(self):
331+
def test_parse_proxy_response_does_not_read_body(self):
332332
self.reader.feed_data(b"HTTP/1.1 200 Connection Established\r\n\r\n")
333-
response = self.assertGeneratorReturns(self.parse(include_body=False))
333+
response = self.assertGeneratorReturns(self.parse(proxy=True))
334334
self.assertEqual(response.body, b"")
335335

336-
def test_parse_http10(self):
336+
def test_parse_proxy_http10(self):
337337
self.reader.feed_data(b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n")
338-
response = self.assertGeneratorReturns(self.parse(allow_http10=True))
338+
response = self.assertGeneratorReturns(self.parse(proxy=True))
339339
self.assertEqual(response.status_code, 200)
340340
self.assertEqual(response.reason_phrase, "OK")
341341
self.assertEqual(response.body, b"")
342342

343-
def test_parse_http10_unsupported_protocol(self):
343+
def test_parse_proxy_unsupported_protocol(self):
344344
self.reader.feed_data(b"HTTP/1.2 400 Bad Request\r\n\r\n")
345345
with self.assertRaises(ValueError) as raised:
346-
next(self.parse(allow_http10=True))
346+
next(self.parse(proxy=True))
347347
self.assertEqual(
348348
str(raised.exception),
349349
"unsupported protocol; expected HTTP/1.1 or HTTP/1.0: "

0 commit comments

Comments
 (0)