Fix WebSocket binary send when text key is None

ASGI allows websocket.send messages to contain both text and bytes
keys where one is None. Check for truthy values instead of key existence.
This commit is contained in:
Benoit Chesneau 2026-04-03 23:53:03 +02:00
parent 746cc049d0
commit 725c03e8ff

View File

@ -145,10 +145,13 @@ class WebSocketProtocol:
if self.closed: if self.closed:
raise RuntimeError("WebSocket closed") raise RuntimeError("WebSocket closed")
if "text" in message: # Check for truthy values since both keys may be present with None
await self._send_frame(OPCODE_TEXT, message["text"].encode("utf-8")) text = message.get("text")
elif "bytes" in message: bytes_data = message.get("bytes")
await self._send_frame(OPCODE_BINARY, message["bytes"]) if text is not None:
await self._send_frame(OPCODE_TEXT, text.encode("utf-8"))
elif bytes_data is not None:
await self._send_frame(OPCODE_BINARY, bytes_data)
elif msg_type == "websocket.close": elif msg_type == "websocket.close":
code = message.get("code", CLOSE_NORMAL) code = message.get("code", CLOSE_NORMAL)