From 972921a49e1c3bbb5a804b82926883d3d0b6b537 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sun, 4 Aug 2019 23:45:44 -0700 Subject: [PATCH] Disable short reads when chunked (fixes #28) 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 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sseclient.py b/sseclient.py index 6d13ad6..4967556 100644 --- a/sseclient.py +++ b/sseclient.py @@ -63,12 +63,12 @@ def _connect(self): def iter_content(self): def generate(): while True: - if hasattr(self.resp.raw, '_fp'): + if hasattr(self.resp.raw, '_fp') 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