Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ async def open_connection(url):
assert is_open


@pytest.mark.asyncio
@pytest.mark.parametrize("ws_protocol_cls", WS_PROTOCOLS)
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
async def test_extra_headers(ws_protocol_cls, http_protocol_cls):
class App(WebSocketResponse):
async def websocket_connect(self, message):
await self.send(
{"type": "websocket.accept", "headers": [(b"extra", b"header")]}
)

async def open_connection(url):
async with websockets.connect(url) as websocket:
return websocket.response_headers

config = Config(app=App, ws=ws_protocol_cls, http=http_protocol_cls, lifespan="off")
async with run_server(config):
extra_headers = await open_connection("ws://127.0.0.1:8000")
assert extra_headers.get("extra") == "header"


@pytest.mark.asyncio
@pytest.mark.parametrize("ws_protocol_cls", WS_PROTOCOLS)
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
Expand Down
8 changes: 8 additions & 0 deletions uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(
ping_timeout=self.config.ws_ping_timeout,
extensions=extensions,
logger=logging.getLogger("uvicorn.error"),
extra_headers=[],
)

def connection_made(self, transport):
Expand Down Expand Up @@ -236,6 +237,13 @@ async def asgi_send(self, message):
)
self.initial_response = None
self.accepted_subprotocol = message.get("subprotocol")
if "headers" in message:
self.extra_headers.extend(
Comment thread
Kludex marked this conversation as resolved.
# ASGI spec requires bytes
# But for compability we need to convert it to strings
(name.decode("latin-1"), value.decode("latin-1"))
for name, value in message.get("headers")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just small syntax comment:
for name, value in message["headers"]
due to already checking if "headers" in message

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, PR welcome :P

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t1waz are you going to open PR for that? I can do that also.

)
self.handshake_started_event.set()

elif message_type == "websocket.close":
Expand Down
5 changes: 4 additions & 1 deletion uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,15 @@ async def send(self, message):
)
self.handshake_complete = True
subprotocol = message.get("subprotocol")
extra_headers = message.get("headers", [])
extensions = []
if self.config.ws_per_message_deflate:
extensions.append(PerMessageDeflate())
output = self.conn.send(
wsproto.events.AcceptConnection(
subprotocol=subprotocol, extensions=extensions
subprotocol=subprotocol,
extensions=extensions,
extra_headers=extra_headers,
)
)
self.transport.write(output)
Expand Down