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:
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)
self.notification_handler(notification)