Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #9 Decode later, to avoid decoding in the middle of utf8 sequence #10

Merged
merged 1 commit into from
Jun 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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