Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial recv can cause failure #16

Open
osresearch opened this issue Sep 12, 2020 · 1 comment
Open

Partial recv can cause failure #16

osresearch opened this issue Sep 12, 2020 · 1 comment

Comments

@osresearch
Copy link

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",
@danni
Copy link
Owner

danni commented Sep 14, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants