Skip to content

Commit c2279c0

Browse files
committed
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: #1507 Link: <https://asgi.readthedocs.io/en/latest/specs/www.html#websocket-connection-scope> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
1 parent 1503782 commit c2279c0

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/python/nxt_python_asgi.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,17 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req,
693693
: nxt_py_1_0_str)
694694
SET_ITEM(scope, scheme, scheme)
695695

696-
v = PyString_FromStringAndSize(nxt_unit_sptr_get(&r->method),
697-
r->method_length);
698-
if (nxt_slow_path(v == NULL)) {
699-
nxt_unit_req_alert(req, "Python failed to create 'method' string");
700-
goto fail;
701-
}
696+
if (!r->websocket_handshake) {
697+
v = PyString_FromStringAndSize(nxt_unit_sptr_get(&r->method),
698+
r->method_length);
699+
if (nxt_slow_path(v == NULL)) {
700+
nxt_unit_req_alert(req, "Python failed to create 'method' string");
701+
goto fail;
702+
}
702703

703-
SET_ITEM(scope, method, v)
704-
Py_DECREF(v);
704+
SET_ITEM(scope, method, v)
705+
Py_DECREF(v);
706+
}
705707

706708
v = PyUnicode_DecodeUTF8(nxt_unit_sptr_get(&r->path), r->path_length,
707709
"replace");

0 commit comments

Comments
 (0)