Stream decoder now iterates more, fixes #155

This commit is contained in:
Lorenz Diener 2019-04-27 18:35:47 +02:00
pare d04d4d14fe
commit f809e0029c

Veure arxiu

@ -58,24 +58,26 @@ class StreamListener(object):
try: try:
for chunk in response.iter_content(chunk_size = 1): for chunk in response.iter_content(chunk_size = 1):
if chunk: if chunk:
if chunk == b'\n': for chunk_part in chunk:
try: chunk_part = bytearray([chunk_part])
line = line_buffer.decode('utf-8') if chunk_part == b'\n':
except UnicodeDecodeError as err: try:
exception = MastodonMalformedEventError("Malformed UTF-8") line = line_buffer.decode('utf-8')
self.on_abort(exception) except UnicodeDecodeError as err:
six.raise_from( exception = MastodonMalformedEventError("Malformed UTF-8")
exception, self.on_abort(exception)
err six.raise_from(
) exception,
if line == '': err
self._dispatch(event) )
event = {} if line == '':
self._dispatch(event)
event = {}
else:
event = self._parse_line(line, event)
line_buffer = bytearray()
else: else:
event = self._parse_line(line, event) line_buffer.extend(chunk_part)
line_buffer = bytearray()
else:
line_buffer.extend(chunk)
except ChunkedEncodingError as err: except ChunkedEncodingError as err:
exception = MastodonNetworkError("Server ceased communication.") exception = MastodonNetworkError("Server ceased communication.")
self.on_abort(exception) self.on_abort(exception)
@ -175,4 +177,4 @@ class CallbackStreamListener(StreamListener):
def on_notification(self, notification): def on_notification(self, notification):
if self.notification_handler != None: if self.notification_handler != None:
self.notification_handler(notification) self.notification_handler(notification)