use subprotocols or headers

This commit is contained in:
Khaleel Al-Adhami 2025-01-27 13:36:35 -08:00
parent bec7f1bbe2
commit f4a0403d71

View File

@ -417,10 +417,21 @@ class App(MiddlewareMixin, LifespanMixin):
original_send = send original_send = send
async def modified_send(message): async def modified_send(message):
if message["type"] == "websocket.accept" and ( if message["type"] == "websocket.accept":
subprotocols := scope.get("subprotocols") subprotocols = scope.get("subprotocols", [])
): if not subprotocols:
message["subprotocol"] = subprotocols[0] headers = scope.get("headers", {})
headers_key = b"sec-websocket-protocol"
if headers_key in headers:
subprotocols = (
headers[headers_key].decode().split(", ")
)
else:
subprotocols = None
else:
subprotocols = subprotocols[0]
message["subprotocol"] = subprotocols
return await original_send(message) return await original_send(message)