From 268fe12ffa68f7af8881c75695c287c09490cef9 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 30 Apr 2022 00:26:36 +0100 Subject: [PATCH] Do not send ACK packet for unknown events (Fixes #824) --- src/socketio/asyncio_server.py | 4 +++- src/socketio/server.py | 5 ++++- tests/asyncio/test_asyncio_server.py | 9 +++++++++ tests/common/test_server.py | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/socketio/asyncio_server.py b/src/socketio/asyncio_server.py index c08adab..03647a5 100644 --- a/src/socketio/asyncio_server.py +++ b/src/socketio/asyncio_server.py @@ -514,7 +514,7 @@ class AsyncServer(server.Server): async def _handle_event_internal(self, server, sid, eio_sid, data, namespace, id): r = await server._trigger_event(data[0], namespace, sid, *data[1:]) - if id is not None: + if r != self.not_handled and id is not None: # send ACK packet with the response returned by the handler # tuples are expanded as multiple arguments if r is None: @@ -553,6 +553,8 @@ class AsyncServer(server.Server): else: ret = handler(*args) return ret + else: + return self.not_handled # or else, forward the event to a namepsace handler if one exists elif namespace in self.namespace_handlers: # pragma: no branch diff --git a/src/socketio/server.py b/src/socketio/server.py index 6a0210e..b2220db 100644 --- a/src/socketio/server.py +++ b/src/socketio/server.py @@ -134,6 +134,7 @@ class Server(object): self.environ = {} self.handlers = {} self.namespace_handlers = {} + self.not_handled = object() self._binary_packet = {} @@ -720,7 +721,7 @@ class Server(object): def _handle_event_internal(self, server, sid, eio_sid, data, namespace, id): r = server._trigger_event(data[0], namespace, sid, *data[1:]) - if id is not None: + if r != self.not_handled and id is not None: # send ACK packet with the response returned by the handler # tuples are expanded as multiple arguments if r is None: @@ -748,6 +749,8 @@ class Server(object): elif event not in self.reserved_events and \ '*' in self.handlers[namespace]: return self.handlers[namespace]['*'](event, *args) + else: + return self.not_handled # or else, forward the event to a namespace handler if one exists elif namespace in self.namespace_handlers: # pragma: no branch diff --git a/tests/asyncio/test_asyncio_server.py b/tests/asyncio/test_asyncio_server.py index 704eea6..01244ca 100644 --- a/tests/asyncio/test_asyncio_server.py +++ b/tests/asyncio/test_asyncio_server.py @@ -703,6 +703,15 @@ class TestAsyncServer(unittest.TestCase): '123', '31000["foo"]' ) + def test_handle_unknown_event_with_ack(self, eio): + eio.return_value.send = AsyncMock() + s = asyncio_server.AsyncServer(async_handlers=False) + s.manager.connect('123', '/') + handler = mock.MagicMock(return_value='foo') + s.on('my message', handler) + _run(s._handle_eio_message('123', '21000["another message","foo"]')) + s.eio.send.mock.assert_not_called() + def test_handle_event_with_ack_none(self, eio): eio.return_value.send = AsyncMock() s = asyncio_server.AsyncServer(async_handlers=False) diff --git a/tests/common/test_server.py b/tests/common/test_server.py index 860e9d0..8730dd8 100644 --- a/tests/common/test_server.py +++ b/tests/common/test_server.py @@ -618,6 +618,14 @@ class TestServer(unittest.TestCase): handler.assert_called_once_with(sid, 'foo') s.eio.send.assert_called_once_with('123', '31000["foo"]') + def test_handle_unknown_event_with_ack(self, eio): + s = server.Server(async_handlers=False) + s.manager.connect('123', '/') + handler = mock.MagicMock(return_value='foo') + s.on('my message', handler) + s._handle_eio_message('123', '21000["another message","foo"]') + s.eio.send.assert_not_called() + def test_handle_event_with_ack_none(self, eio): s = server.Server(async_handlers=False) sid = s.manager.connect('123', '/')