django / daphne

Django Channels HTTP/WebSocket server
BSD 3-Clause "New" or "Revised" License
2.41k stars 270 forks source link

Missing 'scheme' key in ws scope #538

Open FelixFunk opened 3 weeks ago

FelixFunk commented 3 weeks ago

Environment

python: 3.11.5 aiofiles==23.2.1 blinker==1.8.2 click==8.1.7 Flask==3.0.3 h11==0.14.0 h2==4.1.0 hpack==4.0.0 Hypercorn==0.17.3 hyperframe==6.0.1 itsdangerous==2.2.0 Jinja2==3.1.4 MarkupSafe==2.1.5 pathvalidate==3.2.1 priority==2.0.0 PyJWT==2.9.0 pyzmq==26.0.3 Quart==0.19.6 Quart-JWT-Extended==0.1.0 six==1.16.0 Werkzeug==3.0.3 wsproto==1.2.0

OS: OpenSUSE Tumbleweed Browser: Firefox 132.0.1

CMD

daphne run_service:quart_app -e ssl:44444:privateKey=../dev/ssl/key.pem:certKey=../dev/ssl/cert.pem

Issue

While running a Quart app i get the following stacktrace on a websocket request:

2024-11-06 17:01:34,181 | ERROR: Exception inside application: 'scheme' (server.py:297)
Traceback (most recent call last):
  File "/LOCAL/garcia/Projects/triplaystudio/.venv/lib/python3.11/site-packages/quart/app.py", line 1667, in __call__
    await self.asgi_app(scope, receive, send)
  File "/LOCAL/garcia/Projects/triplaystudio/.venv/lib/python3.11/site-packages/quart/app.py", line 1693, in asgi_app
    await asgi_handler(receive, send)
  File "/LOCAL/garcia/Projects/triplaystudio/.venv/lib/python3.11/site-packages/quart/asgi.py", line 166, in __call__
    websocket = self._create_websocket_from_scope(send)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/LOCAL/garcia/Projects/triplaystudio/.venv/lib/python3.11/site-packages/quart/asgi.py", line 204, in _create_websocket_from_scope
    self.scope["scheme"],
    ~~~~~~~~~~^^^^^^^^^^
KeyError: 'scheme'
10.242.2.5:57258 - - [06/Nov/2024:17:01:34] "WSDISCONNECT /conn" - -

It seems like the 'scheme' property is missing for the websocket connection scope. I am not 100% sure if this issue belongs to daphne, since the asgi documentation marks 'scheme' as an optional setting. However, the documentation also states 'but must not be empty'. If this is indeed related to the Quart project i am really sorry for posting it here. I tested the same code with Hypercorn, so the actual request itself should not be an issue here.

carltongibson commented 3 weeks ago

As I understand the spec, Optional (but must not be empty); default is "ws". should be read as if provided must not be empty, but optional, defaulting to ws (if not provided).

It wouldn't hurt to set though if we can get it from the client_scheme. Would it be something you'd be interested to add?

FelixFunk commented 2 weeks ago

Ok that makes sense. I guess Hypercorn (which was built by a Quart developer) then sets it by default and Quart never checks because of it. Sorry for bothering you then.

Currently i am on vacation. Also I am fairly new to programming and have never added to a repo before, therefore I am not sure if I am the right fit for this. But I can have a look at it once I am back.

carltongibson commented 2 weeks ago

@FelixFunk Thanks for the reply. Don't feel obliged. Only take it on if that's something you'd enjoy. Have a good holiday!