Skip to content

Commit

Permalink
Merge pull request #10 from davedoesdev/issue-9-decode-after-split
Browse files Browse the repository at this point in the history
Issue #9 Decode later, to avoid decoding in the middle of utf8 sequence
  • Loading branch information
mpetazzoni authored Jun 11, 2017
2 parents f39f74a + f262d14 commit 208acfb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sseclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,24 @@ def _read(self):
event into multiple HTTP chunks in the response. It is thus necessary
to correctly stitch together consecutive response chunks and find the
SSE delimiter (empty new line) to yield full, correct event chunks."""
data = ''
data = b''
for chunk in self._event_source:
for line in chunk.splitlines(True):
if not line.strip():
yield data
data = ''
data += line.decode(self._char_enc)
data = b''
data += line
if data:
yield data

def events(self):
for chunk in self._read():
event = Event()
# Split before decoding so splitlines() only uses \r and \n
for line in chunk.splitlines():
# Decode the line.
line = line.decode(self._char_enc)

# Lines starting with a separator are comments and are to be
# ignored.
if not line.strip() or line.startswith(_FIELD_SEPARATOR):
Expand Down

0 comments on commit 208acfb

Please sign in to comment.