Navigation Menu

Skip to content

Commit

Permalink
Fix websocket data parsing if content is larger than read BUFSIZE; Is…
Browse files Browse the repository at this point in the history
…sue #181
  • Loading branch information
spaceone committed Nov 2, 2016
1 parent b0df3dd commit 1a7a076
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions circuits/protocols/websocket.py
Expand Up @@ -49,6 +49,7 @@ def __init__(self, sock=None, data=bytearray(), *args, **kwargs):
self._pending_type = None
self._close_received = False
self._close_sent = False
self._buffer = bytearray()

messages = self._parse_messages(bytearray(data))
for message in messages:
Expand Down Expand Up @@ -90,6 +91,7 @@ def _parse_messages(self, data):
msgs = [] # one chunk of bytes may result in several messages
if self._close_received:
return msgs
data = self._buffer + data
while data:
# extract final flag, opcode and masking
final = bool(data[0] & 0x80 != 0)
Expand All @@ -111,7 +113,9 @@ def _parse_messages(self, data):
offset += 4
# if not enough bytes available yet, retry after next read
if len(data) - offset < payload_length:
self._buffer += data
break
self._buffer = bytearray()
# rest of _buffer is payload
msg = data[offset:offset + payload_length]
if masking: # unmask
Expand Down

0 comments on commit 1a7a076

Please sign in to comment.