Skip to content

Commit

Permalink
Fix invalid WebSocket responses (Fixes #332, Fixes #331, Fixes #338)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 28, 2023
1 parent 12a81f9 commit ccc7a86
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/engineio/async_drivers/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,14 @@ async def __call__(self, environ):
self.asgi_send = environ['asgi.send']
await self.asgi_send({'type': 'websocket.accept'})
await self.handler(self)
return '' # send nothing as response

async def close(self):
pass
try:
await self.asgi_send({'type': 'websocket.close'})
except Exception:
# if the socket is already close we don't care
pass

async def send(self, message):
msg_bytes = None
Expand Down
1 change: 1 addition & 0 deletions src/engineio/async_drivers/gevent_uwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def select_greenlet_runner(fd, event):

self.app(self)
uwsgi.disconnect()
return '' # send nothing as response

def close(self):
"""Disconnects uWSGI from the client."""
Expand Down
2 changes: 2 additions & 0 deletions src/engineio/async_drivers/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class WebSocket(object): # pragma: no cover
"""
def __init__(self, handler, server):
self.handler = handler
self.server = server
self._sock = None

async def __call__(self, environ):
Expand All @@ -123,6 +124,7 @@ async def __call__(self, environ):

self.environ = environ
await self.handler(self)
return self.server._ok()

async def close(self):
await self._sock.close()
Expand Down
2 changes: 1 addition & 1 deletion src/engineio/async_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def _upgrade_websocket(self, environ):
return self.server._bad_request()
ws = self.server._async['websocket'](
self._websocket_handler, self.server)
await ws(environ)
return await ws(environ)

async def _websocket_handler(self, ws):
"""Engine.IO handler for websocket transport."""
Expand Down
2 changes: 1 addition & 1 deletion src/engineio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def handle_request(self, environ, start_response):
r = self._method_not_found()

if not isinstance(r, dict):
return r or []
return r
if self.http_compression and \
len(r['response']) >= self.compression_threshold:
encodings = [e.split(';')[0].strip() for e in
Expand Down
3 changes: 1 addition & 2 deletions src/engineio/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def _upgrade_websocket(self, environ, start_response):
return self.server._bad_request()
ws = self.server._async['websocket'](
self._websocket_handler, self.server)
ws(environ, start_response)
return self.server._ok()
return ws(environ, start_response)

def _websocket_handler(self, ws):
"""Engine.IO handler for websocket transport."""
Expand Down

0 comments on commit ccc7a86

Please sign in to comment.