Skip to content
Open
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
13 changes: 12 additions & 1 deletion pylsp_jsonrpc/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ def _read_message(self):
return None

# Grab the body
return self._rfile.read(content_length)
body = self._rfile.read(content_length)
if content_length is None:
return body
chunks = [body]
remaining = content_length - len(body)
while remaining > 0:
chunk = self._rfile.read(remaining)
if not chunk:
return None
chunks.append(chunk)
remaining -= len(chunk)
return b''.join(chunks)

@staticmethod
def _content_length(line):
Expand Down
29 changes: 29 additions & 0 deletions test/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ def test_reader_bad_message(rfile, reader):
consumer.assert_not_called()


@pytest.mark.parametrize('chunk_size', [1, 3, 7, 4096])
def test_reader_short_body_reads(chunk_size):
class ShortReadStream(BytesIO):
def read(self, size=-1):
return super().read(min(size, chunk_size))

stream = ShortReadStream()
writer = JsonRpcStreamWriter(stream, ensure_ascii=False)
messages = [
{'id': 1, 'method': 'hello', 'params': {'text': '\u03bb\U0001f30d'}},
{'id': 2, 'method': 'bye', 'params': {}},
]
for message in messages:
writer.write(message)
stream.seek(0)
received = []
JsonRpcStreamReader(stream).listen(received.append)
assert received == messages


@pytest.mark.parametrize('body', [b'', b'{', b'{}'])
def test_reader_short_body_at_eof(body):
# Even a valid JSON prefix must not be dispatched as a complete frame.
stream = BytesIO(b'Content-Length: 10\r\n\r\n' + body)
received = []
JsonRpcStreamReader(stream).listen(received.append)
assert received == []


def test_reader_bad_json(rfile, reader):
rfile.write(
b'Content-Length: 8\r\n'
Expand Down