uwebsockets icon indicating copy to clipboard operation
uwebsockets copied to clipboard

Partial recv can cause failure

Open osresearch opened this issue 4 years ago • 1 comments

sock.recv(length) is not guaranteed to return all the bytes in a single call, so it is necessary to wrap it in recv_frame(). Otherwise a slow network can cause unrecoverable protocol errors.

diff --git a/uwebsockets/protocol.py b/uwebsockets/protocol.py
index 64f4d06..8ed34b8 100644
--- a/uwebsockets/protocol.py
+++ b/uwebsockets/protocol.py
@@ -113,7 +115,9 @@ class Websocket:
             mask_bits = self.sock.read(4)
 
         try:
-            data = self.sock.read(length)
+            data = b''
+            while len(data) < length:
+                data += self.sock.read(length - len(data))
         except MemoryError:
             # We can't receive this many bytes, close the socket
             if __debug__: LOGGER.debug("Frame of length %s too big. Closing",

osresearch avatar Sep 12 '20 07:09 osresearch

Hi, yes! Can you please submit a pull request? Thanks.

danni avatar Sep 14 '20 03:09 danni