From 8f9d068268182ac3533dddbd14d5f0b3dfa73b5e Mon Sep 17 00:00:00 2001 From: jmichiel Date: Fri, 19 Jan 2018 20:47:53 +0100 Subject: [PATCH 1/2] proper fix for #230: on secure, only pass the requested number of bytes to the parsers --- ws4py/websocket.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ws4py/websocket.py b/ws4py/websocket.py index 511921e..f1421bc 100644 --- a/ws4py/websocket.py +++ b/ws4py/websocket.py @@ -387,10 +387,11 @@ def once(self): logger.debug("WebSocket is already terminated") return False try: - b = self.sock.recv(self.reading_buffer_size) if self._is_secure: - b += self._get_from_pending() - if not b: + b = self._get_from_pending() + if not b and not self.buf: + b = self.sock.recv(self.reading_buffer_size) + if not b and not self.buf: return False self.buf += b except (socket.error, OSError, pyOpenSSLError) as e: @@ -403,9 +404,11 @@ def once(self): # process as much as we can # the process will stop either if there is no buffer left # or if the stream is closed - if not self.process(self.buf): + # only pass the requested number of bytes, leave the rest in the buffer + requested = self.reading_buffer_size + if not self.process(self.buf[:requested]): return False - self.buf = b"" + self.buf = self.buf[requested:] return True From e566ee36bb6dd880c77075a9ffd1416fd8d4b8cd Mon Sep 17 00:00:00 2001 From: jmichiel Date: Fri, 19 Jan 2018 20:56:34 +0100 Subject: [PATCH 2/2] fix non-secure --- ws4py/websocket.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ws4py/websocket.py b/ws4py/websocket.py index f1421bc..61f8c33 100644 --- a/ws4py/websocket.py +++ b/ws4py/websocket.py @@ -387,6 +387,7 @@ def once(self): logger.debug("WebSocket is already terminated") return False try: + b = b'' if self._is_secure: b = self._get_from_pending() if not b and not self.buf: