Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logic to accept default headers in websocket #1

Merged
merged 6 commits into from
Jul 25, 2022
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
3 changes: 3 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ For more nuanced control over which file modifications trigger reloads, install
* `--lifespan <str>` - Set the Lifespan protocol implementation. **Options:** *'auto', 'on', 'off'.* **Default:** *'auto'*.
* `--h11-max-incomplete-event-size <int>` - Set the maximum number of bytes to buffer of an incomplete event. Only available for `h11` HTTP protocol implementation. **Default:** *'16384'* (16 KB).

!!! note
Selecting `websockets` as Websocket protocol doesn't adhere to `--no-server-header` settings of Uvicorn. This is a limitation.

## Application Interface

* `--interface` - Select ASGI3, ASGI2, or WSGI as the application interface.
Expand Down
2 changes: 2 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def load(self) -> None:
self.http_protocol_class = self.http

if isinstance(self.ws, str):
import websockets.http
websockets.http.USER_AGENT = ""
ws_protocol_class = import_from_string(WS_PROTOCOLS[self.ws])
self.ws_protocol_class: Optional[Type[asyncio.Protocol]] = ws_protocol_class
else:
Expand Down
14 changes: 10 additions & 4 deletions uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def __init__(
# Shared server state
self.connections = server_state.connections
self.tasks = server_state.tasks
self.default_headers = server_state.default_headers


# Connection state
self.transport: asyncio.Transport = None # type: ignore[assignment]
Expand Down Expand Up @@ -260,12 +262,16 @@ async def asgi_send(self, message: "ASGISendEvent") -> None:
self.accepted_subprotocol = cast(
Optional[Subprotocol], message.get("subprotocol")
)
if "headers" in message:
self.extra_headers.extend(
headers = list(message.get("headers", [])) + self.default_headers
_added_names = []
for name, value in headers:
if name.lower() in _added_names:
continue
_added_names.append(name.lower())
self.extra_headers.append((
# ASGI spec requires bytes
# But for compatibility we need to convert it to strings
(name.decode("latin-1"), value.decode("latin-1"))
for name, value in message["headers"]
name.decode("latin-1"), value.decode("latin-1"))
)
self.handshake_started_event.set()

Expand Down
3 changes: 3 additions & 0 deletions uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, config, server_state, _loop=None):
# Shared server state
self.connections = server_state.connections
self.tasks = server_state.tasks
self.default_headers = server_state.default_headers


# Connection state
self.transport = None
Expand Down Expand Up @@ -133,6 +135,7 @@ def on_task_complete(self, task):
def handle_connect(self, event):
self.connect_event = event
headers = [(b"host", event.host.encode())]
headers.extend(self.default_headers)
headers += [(key.lower(), value) for key, value in event.extra_headers]
raw_path, _, query_string = event.target.partition("?")
self.scope = {
Expand Down