diff --git a/pylsp_jsonrpc/streams.py b/pylsp_jsonrpc/streams.py index 40048a9..4d22dfc 100644 --- a/pylsp_jsonrpc/streams.py +++ b/pylsp_jsonrpc/streams.py @@ -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): diff --git a/test/test_streams.py b/test/test_streams.py index 14fe1bb..db7e6ef 100644 --- a/test/test_streams.py +++ b/test/test_streams.py @@ -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'