Skip to content

Commit c007570

Browse files
committed
Add option not to read body in Response.parse.
This allows keeping the connection open after reading the response to a CONNECT request.
1 parent 47307b8 commit c007570

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/websockets/http11.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def parse(
210210
read_line: Callable[[int], Generator[None, None, bytes]],
211211
read_exact: Callable[[int], Generator[None, None, bytes]],
212212
read_to_eof: Callable[[int], Generator[None, None, bytes]],
213+
include_body: bool = True,
213214
) -> Generator[None, None, Response]:
214215
"""
215216
Parse a WebSocket handshake response.
@@ -265,9 +266,12 @@ def parse(
265266

266267
headers = yield from parse_headers(read_line)
267268

268-
body = yield from read_body(
269-
status_code, headers, read_line, read_exact, read_to_eof
270-
)
269+
if include_body:
270+
body = yield from read_body(
271+
status_code, headers, read_line, read_exact, read_to_eof
272+
)
273+
else:
274+
body = b""
271275

272276
return cls(status_code, reason, headers, body)
273277

tests/test_http11.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ def setUp(self):
130130
super().setUp()
131131
self.reader = StreamReader()
132132

133-
def parse(self):
133+
def parse(self, **kwargs):
134134
return Response.parse(
135135
self.reader.read_line,
136136
self.reader.read_exact,
137137
self.reader.read_to_eof,
138+
**kwargs,
138139
)
139140

140141
def test_parse(self):
@@ -322,6 +323,11 @@ def test_parse_body_not_modified(self):
322323
response = self.assertGeneratorReturns(self.parse())
323324
self.assertEqual(response.body, b"")
324325

326+
def test_parse_without_body(self):
327+
self.reader.feed_data(b"HTTP/1.1 200 Connection Established\r\n\r\n")
328+
response = self.assertGeneratorReturns(self.parse(include_body=False))
329+
self.assertEqual(response.body, b"")
330+
325331
def test_serialize(self):
326332
# Example from the protocol overview in RFC 6455
327333
response = Response(

0 commit comments

Comments
 (0)