From f809e0029c91d1549124586f08bd7fd636aa0dc6 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sat, 27 Apr 2019 18:35:47 +0200 Subject: [PATCH] Stream decoder now iterates more, fixes #155 --- mastodon/streaming.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/mastodon/streaming.py b/mastodon/streaming.py index 65ec30a..1e1eefd 100644 --- a/mastodon/streaming.py +++ b/mastodon/streaming.py @@ -58,24 +58,26 @@ class StreamListener(object): try: for chunk in response.iter_content(chunk_size = 1): if chunk: - if chunk == b'\n': - try: - line = line_buffer.decode('utf-8') - except UnicodeDecodeError as err: - exception = MastodonMalformedEventError("Malformed UTF-8") - self.on_abort(exception) - six.raise_from( - exception, - err - ) - if line == '': - self._dispatch(event) - event = {} + for chunk_part in chunk: + chunk_part = bytearray([chunk_part]) + if chunk_part == b'\n': + try: + line = line_buffer.decode('utf-8') + except UnicodeDecodeError as err: + exception = MastodonMalformedEventError("Malformed UTF-8") + self.on_abort(exception) + six.raise_from( + exception, + err + ) + if line == '': + self._dispatch(event) + event = {} + else: + event = self._parse_line(line, event) + line_buffer = bytearray() else: - event = self._parse_line(line, event) - line_buffer = bytearray() - else: - line_buffer.extend(chunk) + line_buffer.extend(chunk_part) except ChunkedEncodingError as err: exception = MastodonNetworkError("Server ceased communication.") self.on_abort(exception) @@ -175,4 +177,4 @@ class CallbackStreamListener(StreamListener): def on_notification(self, notification): if self.notification_handler != None: - self.notification_handler(notification) \ No newline at end of file + self.notification_handler(notification)