summaryrefslogtreecommitdiffhomepage
path: root/src/python/nxt_python_asgi.c
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2020-11-10 22:27:08 +0300
committerMax Romanov <max.romanov@nginx.com>2020-11-10 22:27:08 +0300
commit5fd2933d2e54a7b5781698a670abf89b1031db44 (patch)
tree7d682ba1785ac883760331b4d123b0c17eaed10e /src/python/nxt_python_asgi.c
parent4ca9ba34081c44f5d421b171ffaf874fb341d73f (diff)
downloadunit-5fd2933d2e54a7b5781698a670abf89b1031db44.tar.gz
unit-5fd2933d2e54a7b5781698a670abf89b1031db44.tar.bz2
Python: supporting ASGI legacy protocol.
Introducing manual protocol selection for 'universal' apps and frameworks.
Diffstat (limited to 'src/python/nxt_python_asgi.c')
-rw-r--r--src/python/nxt_python_asgi.c121
1 files changed, 98 insertions, 23 deletions
diff --git a/src/python/nxt_python_asgi.c b/src/python/nxt_python_asgi.c
index cab73239..e11a3b6c 100644
--- a/src/python/nxt_python_asgi.c
+++ b/src/python/nxt_python_asgi.c
@@ -16,6 +16,7 @@
#include <python/nxt_python_asgi_str.h>
+static PyObject *nxt_python_asgi_get_func(PyObject *obj);
static int nxt_python_asgi_ctx_data_alloc(void **pdata);
static void nxt_python_asgi_ctx_data_free(void *data);
static int nxt_python_asgi_startup(void *data);
@@ -42,6 +43,7 @@ static PyObject *nxt_py_asgi_port_read(PyObject *self, PyObject *args);
static void nxt_python_asgi_done(void);
+int nxt_py_asgi_legacy;
static PyObject *nxt_py_port_read;
static nxt_unit_port_t *nxt_py_shared_port;
@@ -64,56 +66,78 @@ int
nxt_python_asgi_check(PyObject *obj)
{
int res;
- PyObject *call;
+ PyObject *func;
PyCodeObject *code;
- if (PyFunction_Check(obj)) {
- code = (PyCodeObject *) PyFunction_GET_CODE(obj);
+ func = nxt_python_asgi_get_func(obj);
+
+ if (func == NULL) {
+ return 0;
+ }
+
+ code = (PyCodeObject *) PyFunction_GET_CODE(func);
+
+ nxt_unit_debug(NULL, "asgi_check: callable is %sa coroutine function with "
+ "%d argument(s)",
+ (code->co_flags & CO_COROUTINE) != 0 ? "" : "not ",
+ code->co_argcount);
+
+ res = (code->co_flags & CO_COROUTINE) != 0 || code->co_argcount == 1;
+
+ Py_DECREF(func);
+
+ return res;
+}
+
+
+static PyObject *
+nxt_python_asgi_get_func(PyObject *obj)
+{
+ PyObject *call;
- return (code->co_flags & CO_COROUTINE) != 0;
+ if (PyFunction_Check(obj)) {
+ Py_INCREF(obj);
+ return obj;
}
if (PyMethod_Check(obj)) {
obj = PyMethod_GET_FUNCTION(obj);
- code = (PyCodeObject *) PyFunction_GET_CODE(obj);
-
- return (code->co_flags & CO_COROUTINE) != 0;
+ Py_INCREF(obj);
+ return obj;
}
call = PyObject_GetAttrString(obj, "__call__");
if (call == NULL) {
- return 0;
+ return NULL;
}
if (PyFunction_Check(call)) {
- code = (PyCodeObject *) PyFunction_GET_CODE(call);
-
- res = (code->co_flags & CO_COROUTINE) != 0;
-
- } else {
- if (PyMethod_Check(call)) {
- obj = PyMethod_GET_FUNCTION(call);
+ return call;
+ }
- code = (PyCodeObject *) PyFunction_GET_CODE(obj);
+ if (PyMethod_Check(call)) {
+ obj = PyMethod_GET_FUNCTION(call);
- res = (code->co_flags & CO_COROUTINE) != 0;
+ Py_INCREF(obj);
+ Py_DECREF(call);
- } else {
- res = 0;
- }
+ return obj;
}
Py_DECREF(call);
- return res;
+ return NULL;
}
int
nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto)
{
+ PyObject *func;
+ PyCodeObject *code;
+
nxt_unit_debug(NULL, "asgi_init");
if (nxt_slow_path(nxt_py_asgi_str_init() != NXT_UNIT_OK)) {
@@ -136,6 +160,22 @@ nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto)
return NXT_UNIT_ERROR;
}
+ func = nxt_python_asgi_get_func(nxt_py_application);
+ if (nxt_slow_path(func == NULL)) {
+ nxt_unit_alert(NULL, "Python cannot find function for callable");
+ return NXT_UNIT_ERROR;
+ }
+
+ code = (PyCodeObject *) PyFunction_GET_CODE(func);
+
+ if ((code->co_flags & CO_COROUTINE) == 0) {
+ nxt_unit_debug(NULL, "asgi: callable is not a coroutine function "
+ "switching to legacy mode");
+ nxt_py_asgi_legacy = 1;
+ }
+
+ Py_DECREF(func);
+
init->callbacks.request_handler = nxt_py_asgi_request_handler;
init->callbacks.data_handler = nxt_py_asgi_http_data_handler;
init->callbacks.websocket_handler = nxt_py_asgi_websocket_handler;
@@ -366,6 +406,7 @@ static void
nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
{
PyObject *scope, *res, *task, *receive, *send, *done, *asgi;
+ PyObject *stage2;
nxt_py_asgi_ctx_data_t *ctx_data;
if (req->request->websocket_handshake) {
@@ -415,8 +456,42 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
req->data = asgi;
- res = PyObject_CallFunctionObjArgs(nxt_py_application,
- scope, receive, send, NULL);
+ if (!nxt_py_asgi_legacy) {
+ nxt_unit_req_debug(req, "Python call ASGI 3.0 application");
+
+ res = PyObject_CallFunctionObjArgs(nxt_py_application,
+ scope, receive, send, NULL);
+
+ } else {
+ nxt_unit_req_debug(req, "Python call legacy application");
+
+ res = PyObject_CallFunctionObjArgs(nxt_py_application, scope, NULL);
+
+ if (nxt_slow_path(res == NULL)) {
+ nxt_unit_req_error(req, "Python failed to call legacy app stage1");
+ nxt_python_print_exception();
+ nxt_unit_request_done(req, NXT_UNIT_ERROR);
+
+ goto release_scope;
+ }
+
+ if (nxt_slow_path(PyCallable_Check(res) == 0)) {
+ nxt_unit_req_error(req,
+ "Legacy ASGI application returns not a callable");
+ nxt_unit_request_done(req, NXT_UNIT_ERROR);
+
+ Py_DECREF(res);
+
+ goto release_scope;
+ }
+
+ stage2 = res;
+
+ res = PyObject_CallFunctionObjArgs(stage2, receive, send, NULL);
+
+ Py_DECREF(stage2);
+ }
+
if (nxt_slow_path(res == NULL)) {
nxt_unit_req_error(req, "Python failed to call the application");
nxt_python_print_exception();