diff options
| author | Andrew Clayton <a.clayton@nginx.com> | 2025-01-27 16:48:41 +0000 |
|---|---|---|
| committer | Andrew Clayton <a.clayton@nginx.com> | 2025-02-03 19:06:50 +0000 |
| commit | c2279c0df85a7de23ba5def143ee8f367d599b4d (patch) | |
| tree | f9b82c293612127634cfbe36e1d327e89908be5b | |
| parent | 150378224f1d8b4e58765ce586c27f2fd36e47f0 (diff) | |
| download | unit-c2279c0df85a7de23ba5def143ee8f367d599b4d.tar.gz unit-c2279c0df85a7de23ba5def143ee8f367d599b4d.tar.bz2 | |
python: Fix Litestar WebSockets compatibility
It was reported on GitHub that Unit was unable to work with WebSockets
under Litestar Python applications.
This was due to Unit sending a 'method' variable in the WebSocket's
connection scope, which Litestar was interpreting as being a normal HTTP
connection.
The ASGI WebSocket specification makes no mention about setting a
'method', so let's not send it on WebSockets.
Also tested this change with basic ASGI WebSockets and FastAPI
WebSockets and obviously pytests still pass.
Closes: https://github.com/nginx/unit/issues/1507
Link: <https://asgi.readthedocs.io/en/latest/specs/www.html#websocket-connection-scope>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
| -rw-r--r-- | src/python/nxt_python_asgi.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/python/nxt_python_asgi.c b/src/python/nxt_python_asgi.c index 8f300b53..702f4d8d 100644 --- a/src/python/nxt_python_asgi.c +++ b/src/python/nxt_python_asgi.c @@ -693,15 +693,17 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, : nxt_py_1_0_str) SET_ITEM(scope, scheme, scheme) - v = PyString_FromStringAndSize(nxt_unit_sptr_get(&r->method), - r->method_length); - if (nxt_slow_path(v == NULL)) { - nxt_unit_req_alert(req, "Python failed to create 'method' string"); - goto fail; - } + if (!r->websocket_handshake) { + v = PyString_FromStringAndSize(nxt_unit_sptr_get(&r->method), + r->method_length); + if (nxt_slow_path(v == NULL)) { + nxt_unit_req_alert(req, "Python failed to create 'method' string"); + goto fail; + } - SET_ITEM(scope, method, v) - Py_DECREF(v); + SET_ITEM(scope, method, v) + Py_DECREF(v); + } v = PyUnicode_DecodeUTF8(nxt_unit_sptr_get(&r->path), r->path_length, "replace"); |
