summaryrefslogtreecommitdiffhomepage
path: root/src/python
diff options
context:
space:
mode:
authorKonstantin Pavlov <thresh@nginx.com>2023-08-31 09:41:46 -0700
committerKonstantin Pavlov <thresh@nginx.com>2023-08-31 09:41:46 -0700
commitc45c8919c7232eb20023484f6d1fc9f1f50395d8 (patch)
treecc12eb307c1611494948645e4b487fa06495c3d2 /src/python
parent88c90e1c351ab8c5bd487a5cd4b735014b08e271 (diff)
parent9b22b6957bc87b3df002d0bc691fdae6a20abdac (diff)
downloadunit-c45c8919c7232eb20023484f6d1fc9f1f50395d8.tar.gz
unit-c45c8919c7232eb20023484f6d1fc9f1f50395d8.tar.bz2
Merged with the default branch.1.31.0-1
Diffstat (limited to 'src/python')
-rw-r--r--src/python/nxt_python_asgi.c48
-rw-r--r--src/python/nxt_python_asgi_lifespan.c54
-rw-r--r--src/python/nxt_python_asgi_str.c2
-rw-r--r--src/python/nxt_python_asgi_str.h1
4 files changed, 89 insertions, 16 deletions
diff --git a/src/python/nxt_python_asgi.c b/src/python/nxt_python_asgi.c
index adf03e2b..8f300b53 100644
--- a/src/python/nxt_python_asgi.c
+++ b/src/python/nxt_python_asgi.c
@@ -450,6 +450,7 @@ static void
nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
{
PyObject *scope, *res, *task, *receive, *send, *done, *asgi;
+ PyObject *state, *newstate, *lifespan;
PyObject *stage2;
nxt_python_target_t *target;
nxt_py_asgi_ctx_data_t *ctx_data;
@@ -477,7 +478,7 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
}
send = PyObject_GetAttrString(asgi, "send");
- if (nxt_slow_path(receive == NULL)) {
+ if (nxt_slow_path(send == NULL)) {
nxt_unit_req_alert(req, "Python failed to get 'send' method");
nxt_unit_request_done(req, NXT_UNIT_ERROR);
@@ -485,7 +486,7 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
}
done = PyObject_GetAttrString(asgi, "_done");
- if (nxt_slow_path(receive == NULL)) {
+ if (nxt_slow_path(done == NULL)) {
nxt_unit_req_alert(req, "Python failed to get '_done' method");
nxt_unit_request_done(req, NXT_UNIT_ERROR);
@@ -493,15 +494,41 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
}
req->data = asgi;
+ ctx_data = req->ctx->data;
target = &nxt_py_targets->target[req->request->app_target];
+ lifespan = ctx_data->target_lifespans[req->request->app_target];
+ state = PyObject_GetAttr(lifespan, nxt_py_state_str);
+ if (nxt_slow_path(state == NULL)) {
+ nxt_unit_req_alert(req, "Python failed to get 'state' attribute");
+ nxt_unit_request_done(req, NXT_UNIT_ERROR);
+
+ goto release_done;
+ }
+
+ newstate = PyDict_Copy(state);
+ if (nxt_slow_path(newstate == NULL)) {
+ nxt_unit_req_alert(req, "Python failed to call state.copy()");
+ nxt_unit_request_done(req, NXT_UNIT_ERROR);
+ Py_DECREF(state);
+ goto release_done;
+ }
+ Py_DECREF(state);
scope = nxt_py_asgi_create_http_scope(req, target);
if (nxt_slow_path(scope == NULL)) {
nxt_unit_request_done(req, NXT_UNIT_ERROR);
-
+ Py_DECREF(newstate);
goto release_done;
}
+ if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_state_str, newstate)
+ == -1))
+ {
+ Py_DECREF(newstate);
+ goto release_scope;
+ }
+ Py_DECREF(newstate);
+
if (!target->asgi_legacy) {
nxt_unit_req_debug(req, "Python call ASGI 3.0 application");
@@ -555,7 +582,6 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req)
goto release_scope;
}
- ctx_data = req->ctx->data;
task = PyObject_CallFunctionObjArgs(ctx_data->loop_create_task, res, NULL);
if (nxt_slow_path(task == NULL)) {
@@ -828,7 +854,7 @@ nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port)
static PyObject *
nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port)
{
- char *p, *s;
+ char *p;
PyObject *pair, *v;
pair = PyTuple_New(2);
@@ -837,9 +863,8 @@ nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port)
}
p = nxt_unit_sptr_get(sptr);
- s = memchr(p, ':', len);
- v = PyString_FromStringAndSize(p, s == NULL ? len : s - p);
+ v = PyString_FromStringAndSize(p, len);
if (nxt_slow_path(v == NULL)) {
Py_DECREF(pair);
@@ -848,14 +873,7 @@ nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port)
PyTuple_SET_ITEM(pair, 0, v);
- if (s != NULL) {
- p += len;
- v = PyLong_FromString(s + 1, &p, 10);
-
- } else {
- v = PyLong_FromLong(port);
- }
-
+ v = PyLong_FromLong(port);
if (nxt_slow_path(v == NULL)) {
Py_DECREF(pair);
diff --git a/src/python/nxt_python_asgi_lifespan.c b/src/python/nxt_python_asgi_lifespan.c
index 1fc0e6b7..041cca21 100644
--- a/src/python/nxt_python_asgi_lifespan.c
+++ b/src/python/nxt_python_asgi_lifespan.c
@@ -12,6 +12,8 @@
#include <python/nxt_python_asgi.h>
#include <python/nxt_python_asgi_str.h>
+#include <structmember.h>
+
typedef struct {
PyObject_HEAD
@@ -25,6 +27,7 @@ typedef struct {
PyObject *startup_future;
PyObject *shutdown_future;
PyObject *receive_future;
+ PyObject *state;
} nxt_py_asgi_lifespan_t;
static PyObject *nxt_py_asgi_lifespan_target_startup(
@@ -41,6 +44,7 @@ static PyObject *nxt_py_asgi_lifespan_send_shutdown(
nxt_py_asgi_lifespan_t *lifespan, int v, PyObject *dict);
static PyObject *nxt_py_asgi_lifespan_disable(nxt_py_asgi_lifespan_t *lifespan);
static PyObject *nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future);
+static void nxt_py_asgi_lifespan_dealloc(PyObject *self);
static PyMethodDef nxt_py_asgi_lifespan_methods[] = {
@@ -50,6 +54,26 @@ static PyMethodDef nxt_py_asgi_lifespan_methods[] = {
{ NULL, NULL, 0, 0 }
};
+static PyMemberDef nxt_py_asgi_lifespan_members[] = {
+ {
+#if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 7)
+ .name = "state",
+#else
+ .name = (char *)"state",
+#endif
+ .type = T_OBJECT_EX,
+ .offset = offsetof(nxt_py_asgi_lifespan_t, state),
+ .flags = READONLY,
+#if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 7)
+ .doc = PyDoc_STR("lifespan.state")
+#else
+ .doc = (char *)PyDoc_STR("lifespan.state")
+#endif
+ },
+
+ { NULL, 0, 0, 0, NULL }
+};
+
static PyAsyncMethods nxt_py_asgi_async_methods = {
.am_await = nxt_py_asgi_await,
};
@@ -59,13 +83,14 @@ static PyTypeObject nxt_py_asgi_lifespan_type = {
.tp_name = "unit._asgi_lifespan",
.tp_basicsize = sizeof(nxt_py_asgi_lifespan_t),
- .tp_dealloc = nxt_py_asgi_dealloc,
+ .tp_dealloc = nxt_py_asgi_lifespan_dealloc,
.tp_as_async = &nxt_py_asgi_async_methods,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "unit ASGI Lifespan object",
.tp_iter = nxt_py_asgi_iter,
.tp_iternext = nxt_py_asgi_next,
.tp_methods = nxt_py_asgi_lifespan_methods,
+ .tp_members = nxt_py_asgi_lifespan_members,
};
@@ -163,12 +188,29 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data,
lifespan->shutdown_called = 0;
lifespan->shutdown_future = NULL;
lifespan->receive_future = NULL;
+ lifespan->state = NULL;
scope = nxt_py_asgi_new_scope(NULL, nxt_py_lifespan_str, nxt_py_2_0_str);
if (nxt_slow_path(scope == NULL)) {
goto release_future;
}
+ lifespan->state = PyDict_New();
+ if (nxt_slow_path(lifespan->state == NULL)) {
+ nxt_unit_req_error(NULL,
+ "Python failed to create 'state' dict");
+ goto release_future;
+ }
+
+ if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_state_str,
+ lifespan->state) == -1))
+ {
+ nxt_unit_req_error(NULL,
+ "Python failed to set 'scope.state' item");
+ Py_CLEAR(lifespan->state);
+ goto release_future;
+ }
+
if (!target->asgi_legacy) {
nxt_unit_req_debug(NULL, "Python call ASGI 3.0 application");
@@ -604,4 +646,14 @@ nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future)
}
+static void
+nxt_py_asgi_lifespan_dealloc(PyObject *self)
+{
+ nxt_py_asgi_lifespan_t *lifespan = (nxt_py_asgi_lifespan_t *)self;
+
+ Py_CLEAR(lifespan->state);
+ PyObject_Del(self);
+}
+
+
#endif /* NXT_HAVE_ASGI */
diff --git a/src/python/nxt_python_asgi_str.c b/src/python/nxt_python_asgi_str.c
index 7171d52b..3bea87d5 100644
--- a/src/python/nxt_python_asgi_str.c
+++ b/src/python/nxt_python_asgi_str.c
@@ -55,6 +55,7 @@ PyObject *nxt_py_subprotocol_str;
PyObject *nxt_py_subprotocols_str;
PyObject *nxt_py_text_str;
PyObject *nxt_py_type_str;
+PyObject *nxt_py_state_str;
PyObject *nxt_py_version_str;
PyObject *nxt_py_websocket_str;
PyObject *nxt_py_websocket_accept_str;
@@ -110,6 +111,7 @@ static nxt_python_string_t nxt_py_asgi_strings[] = {
{ nxt_string("subprotocols"), &nxt_py_subprotocols_str },
{ nxt_string("text"), &nxt_py_text_str },
{ nxt_string("type"), &nxt_py_type_str },
+ { nxt_string("state"), &nxt_py_state_str },
{ nxt_string("version"), &nxt_py_version_str },
{ nxt_string("websocket"), &nxt_py_websocket_str },
{ nxt_string("websocket.accept"), &nxt_py_websocket_accept_str },
diff --git a/src/python/nxt_python_asgi_str.h b/src/python/nxt_python_asgi_str.h
index 92969fd2..3c7a3ed9 100644
--- a/src/python/nxt_python_asgi_str.h
+++ b/src/python/nxt_python_asgi_str.h
@@ -50,6 +50,7 @@ extern PyObject *nxt_py_subprotocol_str;
extern PyObject *nxt_py_subprotocols_str;
extern PyObject *nxt_py_text_str;
extern PyObject *nxt_py_type_str;
+extern PyObject *nxt_py_state_str;
extern PyObject *nxt_py_version_str;
extern PyObject *nxt_py_websocket_str;
extern PyObject *nxt_py_websocket_accept_str;