Skip to content

Commit

Permalink
Disable short reads when chunked (fixes #28) (#35)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mutantmonkey authored May 31, 2023
1 parent 87e4b49 commit 2ddfa4a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions sseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ddfa4a

Please sign in to comment.