nxt_python_asgi.c (1979:f62d52f65cf1) nxt_python_asgi.c (1980:43553aa72111)
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <python/nxt_python.h>
8

--- 19 unchanged lines hidden (view full) ---

28static void nxt_py_asgi_close_handler(nxt_unit_request_info_t *req);
29
30static PyObject *nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req);
31static PyObject *nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len,
32 uint16_t port);
33static PyObject *nxt_py_asgi_create_header(nxt_unit_field_t *f);
34static PyObject *nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f);
35
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <python/nxt_python.h>
8

--- 19 unchanged lines hidden (view full) ---

28static void nxt_py_asgi_close_handler(nxt_unit_request_info_t *req);
29
30static PyObject *nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req);
31static PyObject *nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len,
32 uint16_t port);
33static PyObject *nxt_py_asgi_create_header(nxt_unit_field_t *f);
34static PyObject *nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f);
35
36static int nxt_python_asgi_ready(nxt_unit_ctx_t *ctx);
37
38static int nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
39static int nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
36static int nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
37static int nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port);
40static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_port_t *port);
38static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx,
39 nxt_unit_port_t *port);
41static void nxt_py_asgi_quit(nxt_unit_ctx_t *ctx);
42static void nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx);
43
44static PyObject *nxt_py_asgi_port_read(PyObject *self, PyObject *args);
45static void nxt_python_asgi_done(void);
46
47static PyObject *nxt_py_port_read;
40static void nxt_py_asgi_quit(nxt_unit_ctx_t *ctx);
41static void nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx);
42
43static PyObject *nxt_py_asgi_port_read(PyObject *self, PyObject *args);
44static void nxt_python_asgi_done(void);
45
46static PyObject *nxt_py_port_read;
48static nxt_unit_port_t *nxt_py_shared_port;
49
50static PyMethodDef nxt_py_port_read_method =
51 {"unit_port_read", nxt_py_asgi_port_read, METH_VARARGS, ""};
52
53static nxt_python_proto_t nxt_py_asgi_proto = {
54 .ctx_data_alloc = nxt_python_asgi_ctx_data_alloc,
55 .ctx_data_free = nxt_python_asgi_ctx_data_free,
56 .startup = nxt_python_asgi_startup,
57 .run = nxt_python_asgi_run,
47
48static PyMethodDef nxt_py_port_read_method =
49 {"unit_port_read", nxt_py_asgi_port_read, METH_VARARGS, ""};
50
51static nxt_python_proto_t nxt_py_asgi_proto = {
52 .ctx_data_alloc = nxt_python_asgi_ctx_data_alloc,
53 .ctx_data_free = nxt_python_asgi_ctx_data_free,
54 .startup = nxt_python_asgi_startup,
55 .run = nxt_python_asgi_run,
58 .ready = nxt_python_asgi_ready,
59 .done = nxt_python_asgi_done,
60};
61
62#define NXT_UNIT_HASH_WS_PROTOCOL 0xED0A
63
64
65int
66nxt_python_asgi_check(PyObject *obj)

--- 290 unchanged lines hidden (view full) ---

357 nxt_unit_alert(ctx, "Python failed to call loop.run_until_complete");
358 nxt_python_print_exception();
359
360 return NXT_UNIT_ERROR;
361 }
362
363 Py_DECREF(res);
364
56 .done = nxt_python_asgi_done,
57};
58
59#define NXT_UNIT_HASH_WS_PROTOCOL 0xED0A
60
61
62int
63nxt_python_asgi_check(PyObject *obj)

--- 290 unchanged lines hidden (view full) ---

354 nxt_unit_alert(ctx, "Python failed to call loop.run_until_complete");
355 nxt_python_print_exception();
356
357 return NXT_UNIT_ERROR;
358 }
359
360 Py_DECREF(res);
361
365 nxt_py_asgi_remove_reader(ctx, nxt_py_shared_port);
366 nxt_py_asgi_remove_reader(ctx, ctx_data->port);
367
368 if (ctx_data->port != NULL) {
369 ctx_data->port = NULL;
370 }
371
372 nxt_py_asgi_lifespan_shutdown(ctx);
373
374 return NXT_UNIT_OK;
375}
376
377
378static void
379nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)

--- 507 unchanged lines hidden (view full) ---

887
888 Py_DECREF(res);
889
890 return NULL;
891}
892
893
894static int
362 nxt_py_asgi_lifespan_shutdown(ctx);
363
364 return NXT_UNIT_OK;
365}
366
367
368static void
369nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)

--- 507 unchanged lines hidden (view full) ---

877
878 Py_DECREF(res);
879
880 return NULL;
881}
882
883
884static int
895nxt_python_asgi_ready(nxt_unit_ctx_t *ctx)
896{
897 nxt_unit_port_t *port;
898
899 if (nxt_slow_path(nxt_py_shared_port == NULL)) {
900 return NXT_UNIT_ERROR;
901 }
902
903 port = nxt_py_shared_port;
904
905 nxt_unit_debug(ctx, "asgi_ready %d %p %p", port->in_fd, ctx, port);
906
907 return nxt_py_asgi_add_reader(ctx, port);
908}
909
910
911static int
912nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
913{
885nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
886{
914 int nb;
915 nxt_py_asgi_ctx_data_t *ctx_data;
887 int nb;
916
917 if (port->in_fd == -1) {
918 return NXT_UNIT_OK;
919 }
920
921 nb = 1;
922
923 if (nxt_slow_path(ioctl(port->in_fd, FIONBIO, &nb) == -1)) {
924 nxt_unit_alert(ctx, "ioctl(%d, FIONBIO, 0) failed: %s (%d)",
925 port->in_fd, strerror(errno), errno);
926
927 return NXT_UNIT_ERROR;
928 }
929
930 nxt_unit_debug(ctx, "asgi_add_port %d %p %p", port->in_fd, ctx, port);
931
888
889 if (port->in_fd == -1) {
890 return NXT_UNIT_OK;
891 }
892
893 nb = 1;
894
895 if (nxt_slow_path(ioctl(port->in_fd, FIONBIO, &nb) == -1)) {
896 nxt_unit_alert(ctx, "ioctl(%d, FIONBIO, 0) failed: %s (%d)",
897 port->in_fd, strerror(errno), errno);
898
899 return NXT_UNIT_ERROR;
900 }
901
902 nxt_unit_debug(ctx, "asgi_add_port %d %p %p", port->in_fd, ctx, port);
903
932 if (port->id.id == NXT_UNIT_SHARED_PORT_ID) {
933 nxt_py_shared_port = port;
934
935 return NXT_UNIT_OK;
936 }
937
938 ctx_data = ctx->data;
939
940 ctx_data->port = port;
941
942 return nxt_py_asgi_add_reader(ctx, port);
943}
944
945
946static int
947nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
948{
949 int rc;

--- 53 unchanged lines hidden (view full) ---

1003
1004 Py_DECREF(fd);
1005
1006 return rc;
1007}
1008
1009
1010static void
904 return nxt_py_asgi_add_reader(ctx, port);
905}
906
907
908static int
909nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
910{
911 int rc;

--- 53 unchanged lines hidden (view full) ---

965
966 Py_DECREF(fd);
967
968 return rc;
969}
970
971
972static void
1011nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_port_t *port)
973nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx,
974 nxt_unit_port_t *port)
1012{
975{
1013 if (port->in_fd == -1) {
976 if (port->in_fd == -1 || ctx == NULL) {
1014 return;
1015 }
1016
1017 nxt_unit_debug(NULL, "asgi_remove_port %d %p", port->in_fd, port);
1018
977 return;
978 }
979
980 nxt_unit_debug(NULL, "asgi_remove_port %d %p", port->in_fd, port);
981
1019 if (nxt_py_shared_port == port) {
1020 nxt_py_shared_port = NULL;
1021 }
982 nxt_py_asgi_remove_reader(ctx, port);
1022}
1023
1024
1025static void
1026nxt_py_asgi_quit(nxt_unit_ctx_t *ctx)
1027{
1028 PyObject *res, *p;
1029 nxt_py_asgi_ctx_data_t *ctx_data;
1030
1031 nxt_unit_debug(ctx, "asgi_quit %p", ctx);
1032
1033 ctx_data = ctx->data;
1034
983}
984
985
986static void
987nxt_py_asgi_quit(nxt_unit_ctx_t *ctx)
988{
989 PyObject *res, *p;
990 nxt_py_asgi_ctx_data_t *ctx_data;
991
992 nxt_unit_debug(ctx, "asgi_quit %p", ctx);
993
994 ctx_data = ctx->data;
995
1035 if (nxt_py_shared_port != NULL) {
1036 p = PyLong_FromLong(nxt_py_shared_port->in_fd);
1037 if (nxt_slow_path(p == NULL)) {
1038 nxt_unit_alert(NULL, "Python failed to create Long");
1039 nxt_python_print_exception();
1040
1041 } else {
1042 res = PyObject_CallFunctionObjArgs(ctx_data->loop_remove_reader,
1043 p, NULL);
1044 if (nxt_slow_path(res == NULL)) {
1045 nxt_unit_alert(NULL, "Python failed to remove_reader");
1046 nxt_python_print_exception();
1047
1048 } else {
1049 Py_DECREF(res);
1050 }
1051
1052 Py_DECREF(p);
1053 }
1054 }
1055
1056 p = PyLong_FromLong(0);
1057 if (nxt_slow_path(p == NULL)) {
1058 nxt_unit_alert(NULL, "Python failed to create Long");
1059 nxt_python_print_exception();
1060
1061 } else {
1062 res = PyObject_CallFunctionObjArgs(ctx_data->quit_future_set_result,
1063 p, NULL);

--- 450 unchanged lines hidden ---
996 p = PyLong_FromLong(0);
997 if (nxt_slow_path(p == NULL)) {
998 nxt_unit_alert(NULL, "Python failed to create Long");
999 nxt_python_print_exception();
1000
1001 } else {
1002 res = PyObject_CallFunctionObjArgs(ctx_data->quit_future_set_result,
1003 p, NULL);

--- 450 unchanged lines hidden ---