41,42d40 < typedef struct nxt_python_run_ctx_s nxt_python_run_ctx_t; < 45d42 < } nxt_py_input_t; 46a44,51 > uint64_t content_length; > uint64_t bytes_sent; > PyObject *environ; > PyObject *start_resp; > PyObject *write; > nxt_unit_request_info_t *req; > PyThreadState *thread_state; > } nxt_python_ctx_t; 48,50d52 < typedef struct { < PyObject_HEAD < } nxt_py_error_t; 51a54,58 > static int nxt_python_wsgi_ctx_data_alloc(void **pdata); > static void nxt_python_wsgi_ctx_data_free(void *data); > static int nxt_python_wsgi_run(nxt_unit_ctx_t *ctx); > static void nxt_python_wsgi_done(void); > 54,56c61,63 < static PyObject *nxt_python_create_environ(nxt_task_t *task); < static PyObject *nxt_python_get_environ(nxt_python_run_ctx_t *ctx); < static int nxt_python_add_sptr(nxt_python_run_ctx_t *ctx, PyObject *name, --- > static PyObject *nxt_python_create_environ(nxt_python_app_conf_t *c); > static PyObject *nxt_python_get_environ(nxt_python_ctx_t *pctx); > static int nxt_python_add_sptr(nxt_python_ctx_t *pctx, PyObject *name, 58c65 < static int nxt_python_add_field(nxt_python_run_ctx_t *ctx, --- > static int nxt_python_add_field(nxt_python_ctx_t *pctx, 63c70 < static int nxt_python_add_obj(nxt_python_run_ctx_t *ctx, PyObject *name, --- > static int nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, 67c74 < static int nxt_python_response_add_field(nxt_python_run_ctx_t *ctx, --- > static int nxt_python_response_add_field(nxt_python_ctx_t *pctx, 73,77c80,86 < static void nxt_py_input_dealloc(nxt_py_input_t *self); < static PyObject *nxt_py_input_read(nxt_py_input_t *self, PyObject *args); < static PyObject *nxt_py_input_readline(nxt_py_input_t *self, PyObject *args); < static PyObject *nxt_py_input_getline(nxt_python_run_ctx_t *ctx, size_t size); < static PyObject *nxt_py_input_readlines(nxt_py_input_t *self, PyObject *args); --- > static void nxt_py_input_dealloc(nxt_python_ctx_t *pctx); > static PyObject *nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args); > static PyObject *nxt_py_input_readline(nxt_python_ctx_t *pctx, > PyObject *args); > static PyObject *nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size); > static PyObject *nxt_py_input_readlines(nxt_python_ctx_t *self, > PyObject *args); 79,80c88,89 < static PyObject *nxt_py_input_iter(PyObject *self); < static PyObject *nxt_py_input_next(PyObject *self); --- > static PyObject *nxt_py_input_iter(PyObject *pctx); > static PyObject *nxt_py_input_next(PyObject *pctx); 82c91 < static int nxt_python_write(nxt_python_run_ctx_t *ctx, PyObject *bytes); --- > static int nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes); 84,89d92 < struct nxt_python_run_ctx_s { < uint64_t content_length; < uint64_t bytes_sent; < PyObject *environ; < nxt_unit_request_info_t *req; < }; 91d93 < 114c116 < .tp_basicsize = sizeof(nxt_py_input_t), --- > .tp_basicsize = sizeof(nxt_python_ctx_t), 124,126c126 < static PyObject *nxt_py_start_resp_obj; < static PyObject *nxt_py_write_obj; < static PyObject *nxt_py_environ_ptyp; --- > static PyObject *nxt_py_environ_ptyp; 128,130d127 < static PyThreadState *nxt_python_thread_state; < static nxt_python_run_ctx_t *nxt_python_run_ctx; < 145a143 > static PyObject *nxt_py_wsgi_input_str; 163a162 > { nxt_string("wsgi.input"), &nxt_py_wsgi_input_str }, 167a167,172 > static nxt_python_proto_t nxt_py_wsgi_proto = { > .ctx_data_alloc = nxt_python_wsgi_ctx_data_alloc, > .ctx_data_free = nxt_python_wsgi_ctx_data_free, > .run = nxt_python_wsgi_run, > .done = nxt_python_wsgi_done, > }; 169,170c174,176 < nxt_int_t < nxt_python_wsgi_init(nxt_task_t *task, nxt_unit_init_t *init) --- > > int > nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) 176,177c182,185 < if (nxt_slow_path(nxt_python_init_strings(nxt_python_strings) != NXT_OK)) { < nxt_alert(task, "Python failed to init string objects"); --- > if (nxt_slow_path(nxt_python_init_strings(nxt_python_strings) > != NXT_UNIT_OK)) > { > nxt_unit_alert(NULL, "Python failed to init string objects"); 181c189 < obj = PyCFunction_New(nxt_py_start_resp_method, NULL); --- > obj = nxt_python_create_environ(init->data); 183,184d190 < nxt_alert(task, < "Python failed to initialize the \"start_response\" function"); 188c194,195 < nxt_py_start_resp_obj = obj; --- > nxt_py_environ_ptyp = obj; > obj = NULL; 190,193c197,220 < obj = PyCFunction_New(nxt_py_write_method, NULL); < if (nxt_slow_path(obj == NULL)) { < nxt_alert(task, "Python failed to initialize the \"write\" function"); < goto fail; --- > init->callbacks.request_handler = nxt_python_request_handler; > > *proto = nxt_py_wsgi_proto; > > return NXT_UNIT_OK; > > fail: > > Py_XDECREF(obj); > > return NXT_UNIT_ERROR; > } > > > static int > nxt_python_wsgi_ctx_data_alloc(void **pdata) > { > nxt_python_ctx_t *pctx; > > pctx = PyObject_New(nxt_python_ctx_t, &nxt_py_input_type); > if (nxt_slow_path(pctx == NULL)) { > nxt_unit_alert(NULL, > "Python failed to create the \"wsgi.input\" object"); > return NXT_UNIT_ERROR; 196c223 < nxt_py_write_obj = obj; --- > pctx->write = NULL; 198,199c225,229 < obj = nxt_python_create_environ(task); < if (nxt_slow_path(obj == NULL)) { --- > pctx->start_resp = PyCFunction_New(nxt_py_start_resp_method, > (PyObject *) pctx); > if (nxt_slow_path(pctx->start_resp == NULL)) { > nxt_unit_alert(NULL, > "Python failed to initialize the \"start_response\" function"); 203,204c233,238 < nxt_py_environ_ptyp = obj; < obj = NULL; --- > pctx->write = PyCFunction_New(nxt_py_write_method, (PyObject *) pctx); > if (nxt_slow_path(pctx->write == NULL)) { > nxt_unit_alert(NULL, > "Python failed to initialize the \"write\" function"); > goto fail; > } 206c240 < init->callbacks.request_handler = nxt_python_request_handler; --- > *pdata = pctx; 208c242 < return NXT_OK; --- > return NXT_UNIT_OK; 212c246 < Py_XDECREF(obj); --- > nxt_python_wsgi_ctx_data_free(pctx); 214c248 < return NXT_ERROR; --- > return NXT_UNIT_ERROR; 218c252,265 < int --- > static void > nxt_python_wsgi_ctx_data_free(void *data) > { > nxt_python_ctx_t *pctx; > > pctx = data; > > Py_XDECREF(pctx->start_resp); > Py_XDECREF(pctx->write); > Py_XDECREF(pctx); > } > > > static int 221c268,269 < int rc; --- > int rc; > nxt_python_ctx_t *pctx; 223c271 < nxt_python_thread_state = PyEval_SaveThread(); --- > pctx = ctx->data; 224a273,274 > pctx->thread_state = PyEval_SaveThread(); > 227c277 < PyEval_RestoreThread(nxt_python_thread_state); --- > PyEval_RestoreThread(pctx->thread_state); 233c283 < void --- > static void 238,239d287 < Py_XDECREF(nxt_py_start_resp_obj); < Py_XDECREF(nxt_py_write_obj); 247,250c295,298 < int rc; < PyObject *environ, *args, *response, *iterator, *item; < PyObject *close, *result; < nxt_python_run_ctx_t run_ctx = {-1, 0, NULL, req}; --- > int rc; > PyObject *environ, *args, *response, *iterator, *item; > PyObject *close, *result; > nxt_python_ctx_t *pctx; 252c300 < PyEval_RestoreThread(nxt_python_thread_state); --- > pctx = req->ctx->data; 254c302,308 < environ = nxt_python_get_environ(&run_ctx); --- > pctx->content_length = -1; > pctx->bytes_sent = 0; > pctx->req = req; > > PyEval_RestoreThread(pctx->thread_state); > > environ = nxt_python_get_environ(pctx); 272,273c326,327 < Py_INCREF(nxt_py_start_resp_obj); < PyTuple_SET_ITEM(args, 1, nxt_py_start_resp_obj); --- > Py_INCREF(pctx->start_resp); > PyTuple_SET_ITEM(args, 1, pctx->start_resp); 275,276d328 < nxt_python_run_ctx = &run_ctx; < 291c343 < rc = nxt_python_write(&run_ctx, response); --- > rc = nxt_python_write(pctx, response); 299c351 < while (run_ctx.bytes_sent < run_ctx.content_length) { --- > while (pctx->bytes_sent < pctx->content_length) { 315c367 < rc = nxt_python_write(&run_ctx, item); --- > rc = nxt_python_write(pctx, item); 364c416 < nxt_python_thread_state = PyEval_SaveThread(); --- > pctx->thread_state = PyEval_SaveThread(); 366c418,419 < nxt_python_run_ctx = NULL; --- > pctx->req = NULL; > 372c425 < nxt_python_create_environ(nxt_task_t *task) --- > nxt_python_create_environ(nxt_python_app_conf_t *c) 379c432,433 < nxt_alert(task, "Python failed to create the \"environ\" dictionary"); --- > nxt_unit_alert(NULL, > "Python failed to create the \"environ\" dictionary"); 386c440 < nxt_alert(task, --- > nxt_unit_alert(NULL, 394c448 < nxt_alert(task, --- > nxt_unit_alert(NULL, 404c458 < nxt_alert(task, --- > nxt_unit_alert(NULL, 411,412c465,466 < nxt_alert(task, < "Python failed to set the \"wsgi.version\" environ value"); --- > nxt_unit_alert(NULL, > "Python failed to set the \"wsgi.version\" environ value"); 421c475 < Py_False) --- > c->threads > 1 ? Py_True : Py_False) 424c478 < nxt_alert(task, --- > nxt_unit_alert(NULL, 433c487 < nxt_alert(task, --- > nxt_unit_alert(NULL, 442c496 < nxt_alert(task, --- > nxt_unit_alert(NULL, 449c503 < nxt_alert(task, --- > nxt_unit_alert(NULL, 454d507 < obj = (PyObject *) PyObject_New(nxt_py_input_t, &nxt_py_input_type); 456,470d508 < if (nxt_slow_path(obj == NULL)) { < nxt_alert(task, "Python failed to create the \"wsgi.input\" object"); < goto fail; < } < < if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.input", obj) != 0)) { < nxt_alert(task, < "Python failed to set the \"wsgi.input\" environ value"); < goto fail; < } < < Py_DECREF(obj); < obj = NULL; < < 474c512 < nxt_alert(task, "Python failed to get \"sys.stderr\" object"); --- > nxt_unit_alert(NULL, "Python failed to get \"sys.stderr\" object"); 480,481c518,519 < nxt_alert(task, < "Python failed to set the \"wsgi.errors\" environ value"); --- > nxt_unit_alert(NULL, > "Python failed to set the \"wsgi.errors\" environ value"); 497c535 < nxt_python_get_environ(nxt_python_run_ctx_t *ctx) --- > nxt_python_get_environ(nxt_python_ctx_t *pctx) 507c545 < nxt_unit_req_error(ctx->req, --- > nxt_unit_req_error(pctx->req, 513c551 < ctx->environ = environ; --- > pctx->environ = environ; 515c553 < r = ctx->req->request; --- > r = pctx->req->request; 525c563 < RC(nxt_python_add_sptr(ctx, nxt_py_request_method_str, &r->method, --- > RC(nxt_python_add_sptr(pctx, nxt_py_request_method_str, &r->method, 527c565 < RC(nxt_python_add_sptr(ctx, nxt_py_request_uri_str, &r->target, --- > RC(nxt_python_add_sptr(pctx, nxt_py_request_uri_str, &r->target, 529c567 < RC(nxt_python_add_sptr(ctx, nxt_py_query_string_str, &r->query, --- > RC(nxt_python_add_sptr(pctx, nxt_py_query_string_str, &r->query, 531c569 < RC(nxt_python_add_sptr(ctx, nxt_py_path_info_str, &r->path, --- > RC(nxt_python_add_sptr(pctx, nxt_py_path_info_str, &r->path, 534c572 < RC(nxt_python_add_sptr(ctx, nxt_py_remote_addr_str, &r->remote, --- > RC(nxt_python_add_sptr(pctx, nxt_py_remote_addr_str, &r->remote, 536c574 < RC(nxt_python_add_sptr(ctx, nxt_py_server_addr_str, &r->local, --- > RC(nxt_python_add_sptr(pctx, nxt_py_server_addr_str, &r->local, 540c578 < RC(nxt_python_add_obj(ctx, nxt_py_wsgi_uri_scheme_str, --- > RC(nxt_python_add_obj(pctx, nxt_py_wsgi_uri_scheme_str, 543c581 < RC(nxt_python_add_obj(ctx, nxt_py_wsgi_uri_scheme_str, --- > RC(nxt_python_add_obj(pctx, nxt_py_wsgi_uri_scheme_str, 547c585 < RC(nxt_python_add_sptr(ctx, nxt_py_server_protocol_str, &r->version, --- > RC(nxt_python_add_sptr(pctx, nxt_py_server_protocol_str, &r->version, 550c588 < RC(nxt_python_add_sptr(ctx, nxt_py_server_name_str, &r->server_name, --- > RC(nxt_python_add_sptr(pctx, nxt_py_server_name_str, &r->server_name, 552c590 < RC(nxt_python_add_obj(ctx, nxt_py_server_port_str, nxt_py_80_str)); --- > RC(nxt_python_add_obj(pctx, nxt_py_server_port_str, nxt_py_80_str)); 554c592 < nxt_unit_request_group_dup_fields(ctx->req); --- > nxt_unit_request_group_dup_fields(pctx->req); 572c610 < RC(nxt_python_add_field(ctx, f, j - i, vl)); --- > RC(nxt_python_add_field(pctx, f, j - i, vl)); 580c618 < RC(nxt_python_add_sptr(ctx, nxt_py_content_length_str, &f->value, --- > RC(nxt_python_add_sptr(pctx, nxt_py_content_length_str, &f->value, 587c625 < RC(nxt_python_add_sptr(ctx, nxt_py_content_type_str, &f->value, --- > RC(nxt_python_add_sptr(pctx, nxt_py_content_type_str, &f->value, 592a631,638 > if (nxt_slow_path(PyDict_SetItem(environ, nxt_py_wsgi_input_str, > (PyObject *) pctx) != 0)) > { > nxt_unit_req_error(pctx->req, > "Python failed to set the \"wsgi.input\" environ value"); > goto fail; > } > 604c650 < nxt_python_add_sptr(nxt_python_run_ctx_t *ctx, PyObject *name, --- > nxt_python_add_sptr(nxt_python_ctx_t *pctx, PyObject *name, 614c660 < nxt_unit_req_error(ctx->req, --- > nxt_unit_req_error(pctx->req, 622,623c668,669 < if (nxt_slow_path(PyDict_SetItem(ctx->environ, name, value) != 0)) { < nxt_unit_req_error(ctx->req, --- > if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { > nxt_unit_req_error(pctx->req, 638c684 < nxt_python_add_field(nxt_python_run_ctx_t *ctx, nxt_unit_field_t *field, int n, --- > nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, 648c694 < nxt_unit_req_error(ctx->req, --- > nxt_unit_req_error(pctx->req, 659c705 < nxt_unit_req_error(ctx->req, --- > nxt_unit_req_error(pctx->req, 668,669c714,715 < if (nxt_slow_path(PyDict_SetItem(ctx->environ, name, value) != 0)) { < nxt_unit_req_error(ctx->req, --- > if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { > nxt_unit_req_error(pctx->req, 764c810 < nxt_python_add_obj(nxt_python_run_ctx_t *ctx, PyObject *name, PyObject *value) --- > nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value) 766,767c812,813 < if (nxt_slow_path(PyDict_SetItem(ctx->environ, name, value) != 0)) { < nxt_unit_req_error(ctx->req, --- > if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { > nxt_unit_req_error(pctx->req, 781,786c827,832 < int rc, status; < char *status_str, *space_ptr; < uint32_t status_len; < PyObject *headers, *tuple, *string, *status_bytes; < Py_ssize_t i, n, fields_size, fields_count; < nxt_python_run_ctx_t *ctx; --- > int rc, status; > char *status_str, *space_ptr; > uint32_t status_len; > PyObject *headers, *tuple, *string, *status_bytes; > Py_ssize_t i, n, fields_size, fields_count; > nxt_python_ctx_t *pctx; 788,789c834,835 < ctx = nxt_python_run_ctx; < if (nxt_slow_path(ctx == NULL)) { --- > pctx = (nxt_python_ctx_t *) self; > if (nxt_slow_path(pctx->req == NULL)) { 854c900 < ctx->content_length = -1; --- > pctx->content_length = -1; 880c926 < rc = nxt_unit_response_init(ctx->req, status, fields_count, fields_size); --- > rc = nxt_unit_response_init(pctx->req, status, fields_count, fields_size); 889c935 < rc = nxt_python_response_add_field(ctx, PyTuple_GET_ITEM(tuple, 0), --- > rc = nxt_python_response_add_field(pctx, PyTuple_GET_ITEM(tuple, 0), 910,911c956,957 < if (ctx->content_length == 0) { < rc = nxt_unit_response_send(ctx->req); --- > if (pctx->content_length == 0) { > rc = nxt_unit_response_send(pctx->req); 918,919c964,965 < Py_INCREF(nxt_py_write_obj); < return nxt_py_write_obj; --- > Py_INCREF(pctx->write); > return pctx->write; 924c970 < nxt_python_response_add_field(nxt_python_run_ctx_t *ctx, PyObject *name, --- > nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, 946c992 < rc = nxt_unit_response_add_field(ctx->req, name_str, name_length, --- > rc = nxt_unit_response_add_field(pctx->req, name_str, name_length, 952c998 < if (ctx->req->response->fields[i].hash == NXT_UNIT_HASH_CONTENT_LENGTH) { --- > if (pctx->req->response->fields[i].hash == NXT_UNIT_HASH_CONTENT_LENGTH) { 955c1001 < nxt_unit_req_error(ctx->req, "failed to parse Content-Length " --- > nxt_unit_req_error(pctx->req, "failed to parse Content-Length " 959c1005 < ctx->content_length = content_length; --- > pctx->content_length = content_length; 1004c1050 < rc = nxt_python_write(nxt_python_run_ctx, str); --- > rc = nxt_python_write((nxt_python_ctx_t *) self, str); 1015c1061 < nxt_py_input_dealloc(nxt_py_input_t *self) --- > nxt_py_input_dealloc(nxt_python_ctx_t *self) 1022c1068 < nxt_py_input_read(nxt_py_input_t *self, PyObject *args) --- > nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args) 1024,1027c1070,1072 < char *buf; < PyObject *content, *obj; < Py_ssize_t size, n; < nxt_python_run_ctx_t *ctx; --- > char *buf; > PyObject *content, *obj; > Py_ssize_t size, n; 1029,1030c1074 < ctx = nxt_python_run_ctx; < if (nxt_slow_path(ctx == NULL)) { --- > if (nxt_slow_path(pctx->req == NULL)) { 1036c1080 < size = ctx->req->content_length; --- > size = pctx->req->content_length; 1060,1061c1104,1105 < if (size == -1 || size > (Py_ssize_t) ctx->req->content_length) { < size = ctx->req->content_length; --- > if (size == -1 || size > (Py_ssize_t) pctx->req->content_length) { > size = pctx->req->content_length; 1072c1116 < size = nxt_unit_request_read(ctx->req, buf, size); --- > size = nxt_unit_request_read(pctx->req, buf, size); 1079c1123 < nxt_py_input_readline(nxt_py_input_t *self, PyObject *args) --- > nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args) 1081,1084c1125,1127 < ssize_t ssize; < PyObject *obj; < Py_ssize_t n; < nxt_python_run_ctx_t *ctx; --- > ssize_t ssize; > PyObject *obj; > Py_ssize_t n; 1086,1087c1129 < ctx = nxt_python_run_ctx; < if (nxt_slow_path(ctx == NULL)) { --- > if (nxt_slow_path(pctx->req == NULL)) { 1105c1147 < return nxt_py_input_getline(ctx, ssize); --- > return nxt_py_input_getline(pctx, ssize); 1122c1164 < return nxt_py_input_getline(ctx, SSIZE_MAX); --- > return nxt_py_input_getline(pctx, SSIZE_MAX); 1127c1169 < nxt_py_input_getline(nxt_python_run_ctx_t *ctx, size_t size) --- > nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size) 1133c1175 < res = nxt_unit_request_readline_size(ctx->req, size); --- > res = nxt_unit_request_readline_size(pctx->req, size); 1149c1191 < res = nxt_unit_request_read(ctx->req, buf, res); --- > res = nxt_unit_request_read(pctx->req, buf, res); 1156c1198 < nxt_py_input_readlines(nxt_py_input_t *self, PyObject *args) --- > nxt_py_input_readlines(nxt_python_ctx_t *pctx, PyObject *args) 1158,1159c1200 < PyObject *res; < nxt_python_run_ctx_t *ctx; --- > PyObject *res; 1161,1162c1202 < ctx = nxt_python_run_ctx; < if (nxt_slow_path(ctx == NULL)) { --- > if (nxt_slow_path(pctx->req == NULL)) { 1174c1214 < PyObject *line = nxt_py_input_getline(ctx, SSIZE_MAX); --- > PyObject *line = nxt_py_input_getline(pctx, SSIZE_MAX); 1204,1205c1244,1245 < PyObject *line; < nxt_python_run_ctx_t *ctx; --- > PyObject *line; > nxt_python_ctx_t *pctx; 1207,1208c1247,1248 < ctx = nxt_python_run_ctx; < if (nxt_slow_path(ctx == NULL)) { --- > pctx = (nxt_python_ctx_t *) self; > if (nxt_slow_path(pctx->req == NULL)) { 1214c1254 < line = nxt_py_input_getline(ctx, SSIZE_MAX); --- > line = nxt_py_input_getline(pctx, SSIZE_MAX); 1230c1270 < nxt_python_write(nxt_python_run_ctx_t *ctx, PyObject *bytes) --- > nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes) 1251,1253c1291,1293 < if (nxt_slow_path(str_length > ctx->content_length - ctx->bytes_sent)) { < nxt_unit_req_error(ctx->req, "content length %"PRIu64" exceeded", < ctx->content_length); --- > if (nxt_slow_path(str_length > pctx->content_length - pctx->bytes_sent)) { > nxt_unit_req_error(pctx->req, "content length %"PRIu64" exceeded", > pctx->content_length); 1258c1298 < rc = nxt_unit_response_write(ctx->req, str_buf, str_length); --- > rc = nxt_unit_response_write(pctx->req, str_buf, str_length); 1260c1300 < ctx->bytes_sent += str_length; --- > pctx->bytes_sent += str_length;