use scope subprotocols

This commit is contained in:
Khaleel Al-Adhami 2025-01-27 12:50:00 -08:00
parent ed3955a59a
commit bec7f1bbe2

View File

@ -27,6 +27,7 @@ from typing import (
Dict, Dict,
Generic, Generic,
List, List,
MutableMapping,
Optional, Optional,
Set, Set,
Type, Type,
@ -410,20 +411,16 @@ class App(MiddlewareMixin, LifespanMixin):
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
async def __call__(self, scope, receive, send): async def __call__(
self, scope: MutableMapping[str, Any], receive, send
):
original_send = send original_send = send
async def modified_send(message): async def modified_send(message):
headers = dict(scope["headers"]) if message["type"] == "websocket.accept" and (
protocol_key = b"sec-websocket-protocol" subprotocols := scope.get("subprotocols")
if (
message["type"] == "websocket.accept"
and protocol_key in headers
and isinstance(
(subprotocol := headers[protocol_key]), bytes
)
): ):
message["subprotocol"] = subprotocol.decode() message["subprotocol"] = subprotocols[0]
return await original_send(message) return await original_send(message)