Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.2.4 (unreleased)
------------------

* WebSocket ASGI scopes now include ``scheme`` (``ws`` / ``wss``), including
after TLS and ``X-Forwarded-Proto``. Quart and similar apps expect it.

4.2.3 (2026-07-21)
------------------

Expand Down
5 changes: 5 additions & 0 deletions daphne/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def process(self):
self.finish()
# Give it the raw query string
protocol._raw_query_string = self.query_string
# HTTP already resolved TLS / X-Forwarded-Proto; map to ws/wss
if getattr(self, "client_scheme", None) in ("https", "wss"):
protocol.client_scheme = "wss"
else:
protocol.client_scheme = "ws"
# Port across transport
transport, self.transport = self.transport, None
if isinstance(transport, ProtocolWrapper):
Expand Down
17 changes: 16 additions & 1 deletion daphne/ws_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,28 @@ def onConnect(self, request):
self.client_addr = None
self.server_addr = None

if not getattr(self, "client_scheme", None):
try:
secure = self.isSecure()
except Exception:
secure = False
self.client_scheme = "wss" if secure else "ws"

if self.server.proxy_forwarded_address_header:
self.client_addr, self.client_scheme = parse_x_forwarded_for(
self.client_addr, forwarded_scheme = parse_x_forwarded_for(
dict(self.clean_headers),
self.server.proxy_forwarded_address_header,
self.server.proxy_forwarded_port_header,
self.server.proxy_forwarded_proto_header,
self.client_addr,
self.client_scheme,
)
if forwarded_scheme in ("https", "wss"):
self.client_scheme = "wss"
elif forwarded_scheme in ("http", "ws"):
self.client_scheme = "ws"
elif forwarded_scheme:
self.client_scheme = forwarded_scheme
# Decode websocket subprotocol options
subprotocols = []
for header, value in self.clean_headers:
Expand All @@ -86,6 +100,7 @@ def onConnect(self, request):
"client": self.client_addr,
"server": self.server_addr,
"subprotocols": subprotocols,
"scheme": self.client_scheme,
},
)
if self.application_deferred is not None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def assert_valid_websocket_scope(
"raw_path",
"query_string",
"headers",
"scheme",
},
optional_keys={"scheme", "root_path", "client", "server", "subprotocols"},
optional_keys={"root_path", "client", "server", "subprotocols"},
actual_keys=scope.keys(),
)
self.assertEqual(scope["asgi"]["version"], "3.0")
Expand Down