From 2ddfa4a341304d2a7504364169f773d8e9ffcc9f Mon Sep 17 00:00:00 2001 From: mutantmonkey <67266+mutantmonkey@users.noreply.github.com> Date: Wed, 31 May 2023 09:23:08 -0700 Subject: [PATCH] Disable short reads when chunked (fixes #28) (#35) When chunked transfer encoding is used, the size of the chunk is prepended to each chunk. When calling read1 on the raw file-pointer, this data is not stripped off and breaks the JSON formatting. --- sseclient.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sseclient.py b/sseclient.py index 1cfcd17..6039f85 100644 --- a/sseclient.py +++ b/sseclient.py @@ -67,12 +67,13 @@ def generate(): while True: if hasattr(self.resp.raw, '_fp') and \ hasattr(self.resp.raw._fp, 'fp') and \ - hasattr(self.resp.raw._fp.fp, 'read1'): + hasattr(self.resp.raw._fp.fp, 'read1') and \ + not self.resp.raw.chunked: chunk = self.resp.raw._fp.fp.read1(self.chunk_size) else: - # _fp is not available, this means that we cannot use short - # reads and this will block until the full chunk size is - # actually read + # _fp is not available or we are using chunked encoding + # this means that we cannot use short reads and this will + # block until the full chunk size is actually read chunk = self.resp.raw.read(self.chunk_size) if not chunk: break