first round of protocol parsers optimizations

preliminary api for language-independent body read

another step

completed body read language independent implementation

ported gevent to the new read/write api

ported websockets to the new read/write api

removed channels subsystem

removed channels subsystem

ported lua to the new read/write api

fixed post-buffering

readline is still broken

improved request body readline

very difficult test for readline()/read() combo passed

other improvements in postbuffering/read/readline

ported --http-socket to the new api

added X-Forwarded-SSL management

removed old api

more refactoring

ported the RACK plugin to the new api

ported psgi plugin to the new api

defintely removed clustering

simpified ifdel hell

simpified ifdef hell

removed useless configuration options
This commit is contained in:
Unbit
2013-02-03 10:38:13 +01:00
parent e50a7c518f
commit d4130dbc4b
70 changed files with 3507 additions and 6593 deletions
+5 -31
View File
@@ -101,27 +101,12 @@ void *uwsgi_request_subhandler_pump(struct wsgi_request *wsgi_req, struct uwsgi_
PyDict_SetItemString(wsgi_req->async_environ, "headers", headers);
Py_DECREF(headers);
// if async_post is mapped as a file, directly use it as wsgi.input
if (wsgi_req->async_post) {
#ifdef PYTHREE
wsgi_req->async_input = PyFile_FromFd(fileno((FILE *)wsgi_req->async_post), "pump_body", "rb", 0, NULL, NULL, NULL, 0);
#else
wsgi_req->async_input = PyFile_FromFile(wsgi_req->async_post, "pump_body", "r", NULL);
#endif
}
else {
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
((uwsgi_Input*)wsgi_req->async_input)->pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_max_size = 0;
}
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
PyDict_SetItemString(wsgi_req->async_environ, "body", wsgi_req->async_input);
if (wsgi_req->scheme_len > 0) {
zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len);
}
@@ -153,19 +138,8 @@ void *uwsgi_request_subhandler_pump(struct wsgi_request *wsgi_req, struct uwsgi_
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.core", PyInt_FromLong(wsgi_req->async_id));
}
// cache this ?
if (uwsgi.cluster_fd >= 0) {
zero = PyString_FromString(uwsgi.cluster);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster", zero);
Py_DECREF(zero);
zero = PyString_FromString(uwsgi.hostname);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster_node", zero);
Py_DECREF(zero);
}
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node);
// call
PyTuple_SetItem(wsgi_req->async_args, 0, wsgi_req->async_environ);
@@ -280,11 +254,11 @@ int uwsgi_response_subhandler_pump(struct wsgi_request *wsgi_req) {
if (!wsgi_req->async_placeholder) {
goto clear;
}
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
return UWSGI_AGAIN;
}
#endif
}
else {
uwsgi_log("invalid Pump response.\n");
-41
View File
@@ -15,12 +15,8 @@ extern char **environ;
PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}};
#ifdef UWSGI_ASYNC
PyMethodDef uwsgi_eventfd_read_method[] = { {"uwsgi_eventfd_read", py_eventfd_read, METH_VARARGS, ""}};
PyMethodDef uwsgi_eventfd_write_method[] = { {"uwsgi_eventfd_write", py_eventfd_write, METH_VARARGS, ""}};
#endif
#ifdef UWSGI_MINTERPRETERS
void set_dyn_pyhome(char *home, uint16_t pyhome_len) {
@@ -68,8 +64,6 @@ void set_dyn_pyhome(char *home, uint16_t pyhome_len) {
}
}
#endif
int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThreadState *interpreter, int app_type) {
@@ -161,7 +155,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
}
}
#ifdef UWSGI_MINTERPRETERS
if (interpreter == NULL && id) {
wi->interpreter = Py_NewInterpreter();
@@ -172,10 +165,8 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
PyThreadState_Swap(wi->interpreter);
init_pyargv();
#ifdef UWSGI_EMBEDDED
// we need to inizialize an embedded module for every interpreter
init_uwsgi_embedded_module();
#endif
init_uwsgi_vars();
}
@@ -189,9 +180,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
if (wsgi_req->pyhome_len) {
set_dyn_pyhome(wsgi_req->pyhome, wsgi_req->pyhome_len);
}
#else
wi->interpreter = up.main_thread;
#endif
if (wsgi_req->touch_reload_len > 0 && wsgi_req->touch_reload_len < 0xff) {
struct stat trst;
@@ -245,7 +233,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
Py_INCREF((PyObject *)wi->callable);
#ifdef UWSGI_ASYNC
wi->environ = malloc(sizeof(PyObject*)*uwsgi.cores);
if (!wi->environ) {
uwsgi_error("malloc()");
@@ -259,13 +246,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
exit(1);
}
}
#else
wi->environ = PyDict_New();
if (!wi->environ) {
uwsgi_log("unable to allocate new env dictionary for app\n");
exit(1);
}
#endif
wi->argc = 1;
@@ -292,7 +272,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
wi->response_subhandler = uwsgi_response_subhandler_pump;
}
#ifdef UWSGI_ASYNC
wi->args = malloc(sizeof(PyObject*)*uwsgi.cores);
if (!wi->args) {
uwsgi_error("malloc()");
@@ -315,27 +294,13 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
}
}
}
#else
// add start_response on WSGI app
Py_INCREF((PyObject *)up.wsgi_spitout);
wi->args = PyTuple_New(wi->argc);
if (app_type == PYTHON_APP_TYPE_WSGI) {
if (PyTuple_SetItem(wi->args, 1, up.wsgi_spitout)) {
uwsgi_log("unable to set start_response in args tuple\n");
exit(1);
}
}
#endif
if (app_type == PYTHON_APP_TYPE_WSGI) {
// prepare sendfile() for WSGI app
wi->sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL);
#ifdef UWSGI_ASYNC
wi->eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL);
wi->eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL);
#endif
}
// cache most used values
@@ -426,7 +391,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
doh:
if (PyErr_Occurred())
PyErr_Print();
#ifdef UWSGI_MINTERPRETERS
if (interpreter == NULL && id) {
Py_EndInterpreter(wi->interpreter);
if (uwsgi.threads > 1) {
@@ -436,7 +400,6 @@ doh:
PyThreadState_Swap(up.main_thread);
}
}
#endif
return -1;
}
@@ -481,9 +444,7 @@ PyObject *uwsgi_uwsgi_loader(void *arg1) {
PyObject *tmp_callable;
PyObject *applications;
#ifdef UWSGI_EMBEDDED
PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
#endif
char *module = (char *) arg1;
@@ -506,10 +467,8 @@ PyObject *uwsgi_uwsgi_loader(void *arg1) {
return NULL;
}
#ifdef UWSGI_EMBEDDED
applications = PyDict_GetItemString(uwsgi_dict, "applications");
if (applications && PyDict_Check(applications)) return applications;
#endif
applications = PyDict_GetItemString(wsgi_dict, "applications");
if (applications && PyDict_Check(applications)) return applications;
-42
View File
@@ -136,7 +136,6 @@ struct uwsgi_option uwsgi_python_options[] = {
{"py", required_argument, 0, "run a python script in the uWSGI environment", uwsgi_opt_pyrun, NULL, 0},
{"pyrun", required_argument, 0, "run a python script in the uWSGI environment", uwsgi_opt_pyrun, NULL, 0},
#ifdef UWSGI_THREADING
#ifndef UWSGI_PYPY
{"py-tracebacker", required_argument, 0, "enable the uWSGI python tracebacker", uwsgi_opt_set_str, &up.tracebacker, UWSGI_OPT_THREADS|UWSGI_OPT_MASTER},
#endif
@@ -145,7 +144,6 @@ struct uwsgi_option uwsgi_python_options[] = {
{"python-auto-reload", required_argument, 0, "monitor python modules mtime to trigger reload (use only in development)", uwsgi_opt_set_int, &up.auto_reload, UWSGI_OPT_THREADS|UWSGI_OPT_MASTER},
{"python-autoreload", required_argument, 0, "monitor python modules mtime to trigger reload (use only in development)", uwsgi_opt_set_int, &up.auto_reload, UWSGI_OPT_THREADS|UWSGI_OPT_MASTER},
{"py-auto-reload-ignore", required_argument, 0, "ignore the specified module during auto-reload scan (can be specified multiple times)", uwsgi_opt_add_string_list, &up.auto_reload_ignore, UWSGI_OPT_THREADS|UWSGI_OPT_MASTER},
#endif
{"wsgi-env-behaviour", required_argument, 0, "set the strategy for allocating/deallocating the WSGI env", uwsgi_opt_set_str, &up.wsgi_env_behaviour, 0},
{"wsgi-env-behavior", required_argument, 0, "set the strategy for allocating/deallocating the WSGI env", uwsgi_opt_set_str, &up.wsgi_env_behaviour, 0},
@@ -249,9 +247,6 @@ pep405:
up.wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL);
up.wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL);
up.hook_wsgi_input_read = uwsgi_python_hook_simple_input_read;
up.hook_wsgi_input_readline = uwsgi_python_hook_simple_input_readline;
up.main_thread = PyThreadState_Get();
// by default set a fake GIL (little impact on performance)
@@ -307,12 +302,9 @@ void uwsgi_python_atexit() {
if (uwsgi.workers[uwsgi.mywid].busy)
return;
#ifdef UWSGI_ASYNC
// managing atexit in async mode is a real pain...skip it for now
if (uwsgi.async > 1)
return;
#endif
realstuff:
// this time we use this higher level function
@@ -326,7 +318,6 @@ realstuff:
PyGILState_Ensure();
// no need to worry about freeing memory
#ifdef UWSGI_EMBEDDED
PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
if (uwsgi_dict) {
PyObject *ae = PyDict_GetItemString(uwsgi_dict, "atexit");
@@ -334,7 +325,6 @@ realstuff:
python_call(ae, PyTuple_New(0), 0, NULL);
}
}
#endif
// this part is a 1:1 copy of mod_wsgi 3.x
// it is required to fix some atexit bug with python 3
@@ -352,15 +342,12 @@ realstuff:
void uwsgi_python_post_fork() {
#ifdef UWSGI_SPOOLER
if (uwsgi.i_am_a_spooler) {
UWSGI_GET_GIL
}
#endif
uwsgi_python_reset_random_seed();
#ifdef UWSGI_EMBEDDED
// call the post_fork_hook
PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
if (uwsgi_dict) {
@@ -370,10 +357,8 @@ void uwsgi_python_post_fork() {
}
}
PyErr_Clear();
#endif
if (uwsgi.mywid > 0) {
#ifdef UWSGI_THREADING
if (up.auto_reload) {
// spawn the reloader thread
pthread_t par_tid;
@@ -385,7 +370,6 @@ void uwsgi_python_post_fork() {
pthread_t ptb_tid;
pthread_create(&ptb_tid, NULL, uwsgi_python_tracebacker_thread, NULL);
}
#endif
#endif
}
@@ -572,7 +556,6 @@ next:
#ifdef UWSGI_EMBEDDED
PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module.");
#ifdef PYTHREE
@@ -671,7 +654,6 @@ void init_uwsgi_embedded_module() {
}
}
#ifdef UWSGI_SPOOLER
if (uwsgi.spoolers) {
int sc = 0;
struct uwsgi_spooler *uspool = uwsgi.spoolers;
@@ -693,9 +675,6 @@ void init_uwsgi_embedded_module() {
exit(1);
}
}
#endif
if (PyDict_SetItemString(up.embedded_dict, "SPOOL_RETRY", PyInt_FromLong(-1))) {
PyErr_Print();
@@ -850,12 +829,9 @@ void init_uwsgi_embedded_module() {
init_uwsgi_module_advanced(new_uwsgi_module);
#ifdef UWSGI_SPOOLER
if (uwsgi.spoolers) {
init_uwsgi_module_spooler(new_uwsgi_module);
}
#endif
if (uwsgi.sharedareasize > 0 && uwsgi.sharedarea) {
init_uwsgi_module_sharedarea(new_uwsgi_module);
@@ -869,17 +845,14 @@ void init_uwsgi_embedded_module() {
init_uwsgi_module_queue(new_uwsgi_module);
}
#ifdef UWSGI_SNMP
if (uwsgi.snmp) {
init_uwsgi_module_snmp(new_uwsgi_module);
}
#endif
if (up.extension) {
up.extension();
}
}
#endif
@@ -995,15 +968,9 @@ void uwsgi_python_spooler_init(void) {
// from a python-programmer point of view it is a hack/cheat but it does not violate the WSGI standard
// and it is a bit faster than the "holy" allocator
void *uwsgi_python_create_env_cheat(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) {
#ifdef UWSGI_ASYNC
wsgi_req->async_args = wi->args[wsgi_req->async_id];
Py_INCREF((PyObject *)wi->environ[wsgi_req->async_id]);
return wi->environ[wsgi_req->async_id];
#else
wsgi_req->async_args = wi->args;
Py_INCREF((PyObject *)wi->environ);
return wi->environ;
#endif
}
void uwsgi_python_destroy_env_cheat(struct wsgi_request *wsgi_req) {
@@ -1045,14 +1012,10 @@ void uwsgi_python_preinit_apps() {
init_pyargv();
#ifdef UWSGI_EMBEDDED
init_uwsgi_embedded_module();
#endif
#ifdef __linux__
#ifdef UWSGI_EMBEDDED
uwsgi_init_symbol_import();
#endif
#endif
if (up.test_module != NULL) {
@@ -1206,7 +1169,6 @@ next:
}
#endif
#ifdef UWSGI_EMBEDDED
PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
if (uwsgi_dict) {
up.after_req_hook = PyDict_GetItemString(uwsgi_dict, "after_req_hook");
@@ -1216,8 +1178,6 @@ next:
Py_INCREF(up.after_req_hook_args);
}
}
#endif
// lazy ?
if (uwsgi.mywid > 0) {
UWSGI_RELEASE_GIL;
@@ -1319,7 +1279,6 @@ void uwsgi_python_init_thread(int core_id) {
}
#ifdef UWSGI_THREADING
int uwsgi_check_python_mtime(PyObject *times_dict, char *filename) {
struct stat st;
@@ -1463,7 +1422,6 @@ void *uwsgi_python_autoreloader_thread(void *foobar) {
return NULL;
}
#endif
#ifndef UWSGI_PYPY
void uwsgi_python_suspend(struct wsgi_request *wsgi_req) {
-2
View File
@@ -139,11 +139,9 @@ int uwsgi_python_call(struct wsgi_request *wsgi_req, PyObject *callable, PyObjec
if (wsgi_req->async_result) {
while ( manage_python_response(wsgi_req) != UWSGI_OK) {
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
return UWSGI_AGAIN;
}
#endif
}
}
File diff suppressed because it is too large Load Diff
-20
View File
@@ -26,13 +26,8 @@
#define PYTHREE
#endif
#ifdef UWSGI_THREADING
#define UWSGI_GET_GIL up.gil_get();
#define UWSGI_RELEASE_GIL up.gil_release();
#else
#define UWSGI_GET_GIL
#define UWSGI_RELEASE_GIL
#endif
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(x, y) PyObject_HEAD_INIT(x) y,
@@ -85,15 +80,8 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t);
#define LOADER_MAX 8
#define UWSGI_PY_READLINE_BUFSIZE 1024
typedef struct uwsgi_Input {
PyObject_HEAD
char readline[UWSGI_PY_READLINE_BUFSIZE];
size_t readline_size;
size_t readline_max_size;
size_t readline_pos;
size_t pos;
struct wsgi_request *wsgi_req;
} uwsgi_Input;
@@ -153,7 +141,6 @@ struct uwsgi_python {
void (*swap_ts)(struct wsgi_request *, struct uwsgi_app *);
void (*reset_ts)(struct wsgi_request *, struct uwsgi_app *);
#ifdef UWSGI_THREADING
pthread_key_t upt_save_key;
pthread_key_t upt_gil_key;
pthread_mutex_t lock_pyloaders;
@@ -162,7 +149,6 @@ struct uwsgi_python {
int auto_reload;
char *tracebacker;
struct uwsgi_string_list *auto_reload_ignore;
#endif
PyObject *workers_tuple;
PyObject *embedded_dict;
@@ -186,9 +172,6 @@ struct uwsgi_python {
char *pyrun;
int start_response_nodelay;
ssize_t (*hook_wsgi_input_read)(struct wsgi_request *, char *, size_t, size_t *);
ssize_t (*hook_wsgi_input_readline)(struct wsgi_request *, char *, size_t);
char *programname;
};
@@ -280,9 +263,6 @@ int uwsgi_python_do_send_headers(struct wsgi_request *);
void *uwsgi_python_tracebacker_thread(void *);
PyObject *uwsgi_python_setup_thread(char *);
ssize_t uwsgi_python_hook_simple_input_read(struct wsgi_request *, char *, size_t, size_t *);
ssize_t uwsgi_python_hook_simple_input_readline(struct wsgi_request *, char *, size_t);
#ifdef UWSGI_PYPY
#undef UWSGI_MINTERPRETERS
#endif
+5 -31
View File
@@ -28,8 +28,8 @@ void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_
Py_DECREF(pydictvalue);
}
if (wsgi_req->uh.modifier1 == UWSGI_MODIFIER_MANAGE_PATH_INFO) {
wsgi_req->uh.modifier1 = 0;
if (wsgi_req->uh->modifier1 == UWSGI_MODIFIER_MANAGE_PATH_INFO) {
wsgi_req->uh->modifier1 = 0;
pydictkey = PyDict_GetItemString(wsgi_req->async_environ, "SCRIPT_NAME");
if (pydictkey) {
if (PyString_Check(pydictkey)) {
@@ -44,23 +44,9 @@ void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_
}
}
// if async_post is mapped as a file, directly use it as wsgi.input
if (wsgi_req->async_post) {
#ifdef PYTHREE
wsgi_req->async_input = PyFile_FromFd(fileno((FILE *)wsgi_req->async_post), "web3_input", "rb", 0, NULL, NULL, NULL, 0);
#else
wsgi_req->async_input = PyFile_FromFile(wsgi_req->async_post, "web3_input", "r", NULL);
#endif
}
else {
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
((uwsgi_Input*)wsgi_req->async_input)->pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_max_size = 0;
}
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
PyDict_SetItemString(wsgi_req->async_environ, "web3.input", wsgi_req->async_input);
@@ -111,16 +97,6 @@ void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.core", PyInt_FromLong(wsgi_req->async_id));
}
// cache this ?
if (uwsgi.cluster_fd >= 0) {
zero = PyString_FromString(uwsgi.cluster);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster", zero);
Py_DECREF(zero);
zero = PyString_FromString(uwsgi.hostname);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster_node", zero);
Py_DECREF(zero);
}
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node);
@@ -187,11 +163,9 @@ int uwsgi_response_subhandler_web3(struct wsgi_request *wsgi_req) {
if (!wsgi_req->async_placeholder) {
goto clear;
}
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
return UWSGI_AGAIN;
}
#endif
}
else {
uwsgi_log("invalid Web3 response.\n");
+60 -176
View File
@@ -9,88 +9,37 @@ PyObject *uwsgi_Input_iter(PyObject *self) {
return self;
}
ssize_t uwsgi_python_hook_simple_input_readline(struct wsgi_request *wsgi_req, char *readline, size_t max_size) {
ssize_t rlen = 0;
UWSGI_RELEASE_GIL;
if (uwsgi_waitfd(wsgi_req->poll.fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]) <= 0) {
UWSGI_GET_GIL
return 0;
}
if (max_size > 0 && max_size < UWSGI_PY_READLINE_BUFSIZE) {
rlen = read(wsgi_req->poll.fd, readline, max_size);
}
else {
rlen = read(wsgi_req->poll.fd, readline, UWSGI_PY_READLINE_BUFSIZE);
}
UWSGI_GET_GIL;
return rlen;
}
PyObject *uwsgi_Input_getline(uwsgi_Input *self) {
size_t i;
ssize_t rlen;
PyObject *uwsgi_Input_getline(uwsgi_Input *self, long hint) {
struct wsgi_request *wsgi_req = self->wsgi_req;
PyObject *res;
ssize_t rlen = 0;
char *ptr = self->readline;
if (uwsgi.post_buffering > 0) {
ptr = wsgi_req->post_buffering_buf;
self->readline_size = wsgi_req->post_cl;
if (!self->readline_pos) {
self->pos += self->readline_size;
}
char *buf = uwsgi_request_body_readline(wsgi_req, hint, &rlen);
if (buf == uwsgi.empty) {
return PyString_FromString("");
}
if (buf) {
return PyString_FromStringAndSize(buf, rlen);
}
if (self->readline_pos > 0 || uwsgi.post_buffering) {
for(i=self->readline_pos;i<self->readline_size;i++) {
if (ptr[i] == '\n') {
res = PyString_FromStringAndSize(ptr+self->readline_pos, (i-self->readline_pos)+1);
self->readline_pos = i+1;
if (self->readline_pos >= self->readline_size) self->readline_pos = 0;
return res;
}
}
res = PyString_FromStringAndSize(ptr + self->readline_pos, self->readline_size - self->readline_pos);
self->readline_pos = 0;
return res;
}
rlen = up.hook_wsgi_input_readline(wsgi_req, self->readline, self->readline_max_size);
if (rlen < 0) {
return PyErr_Format(PyExc_IOError, "error reading for wsgi.input data (readline/getline)");
return PyErr_Format(PyExc_IOError, "error during readline(%ld) on wsgi.input", hint);
}
else if (rlen == 0) {
return PyErr_Format(PyExc_IOError, "error waiting for wsgi.input data (readline/getline)");
}
self->readline_size = rlen;
self->readline_pos = 0;
self->pos += rlen;
for(i=0;i<(size_t)rlen;i++) {
if (self->readline[i] == '\n') {
res = PyString_FromStringAndSize(self->readline, i+1);
self->readline_pos+= i+1;
if (self->readline_pos >= self->readline_size) self->readline_pos = 0;
return res;
}
}
self->readline_pos = 0;
return PyString_FromStringAndSize(self->readline, self->readline_size);
return PyErr_Format(PyExc_IOError, "timeout during readline(%ld) on wsgi.input", hint);
}
PyObject *uwsgi_Input_next(PyObject* self) {
if (!((uwsgi_Input *)self)->wsgi_req->post_cl || ((size_t) ((uwsgi_Input *)self)->pos >= ((uwsgi_Input *)self)->wsgi_req->post_cl && !((uwsgi_Input *)self)->readline_pos)) {
PyObject *line = uwsgi_Input_getline((uwsgi_Input *)self, 0);
if (!line) return NULL;
if (PyString_Size(line) == 0) {
Py_DECREF(line);
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
return uwsgi_Input_getline((uwsgi_Input *)self);
return line;
}
@@ -98,129 +47,77 @@ static void uwsgi_Input_free(uwsgi_Input *self) {
PyObject_Del(self);
}
ssize_t uwsgi_python_hook_simple_input_read(struct wsgi_request *wsgi_req, char *tmp_buf, size_t remains, size_t *tmp_pos) {
UWSGI_RELEASE_GIL
while(remains) {
if (uwsgi_waitfd(wsgi_req->poll.fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]) <= 0) {
UWSGI_GET_GIL
return 0;
}
ssize_t rlen = read(wsgi_req->poll.fd, tmp_buf+*tmp_pos, remains);
if (rlen <= 0) {
UWSGI_GET_GIL
return -1;
}
*tmp_pos += rlen;
remains -= rlen;
}
UWSGI_GET_GIL
return *tmp_pos;
}
static PyObject *uwsgi_Input_read(uwsgi_Input *self, PyObject *args) {
long len = 0;
size_t remains;
size_t tmp_pos = 0;
char *tmp_buf;
PyObject *res;
long arg_len = 0;
if (!PyArg_ParseTuple(args, "|l:read", &len)) {
if (!PyArg_ParseTuple(args, "|l:read", &arg_len)) {
return NULL;
}
// return empty string if no post_cl or pos >= post_cl
if ((!self->wsgi_req->post_cl || (size_t) self->pos >= self->wsgi_req->post_cl ) && !self->readline_pos) {
struct wsgi_request *wsgi_req = self->wsgi_req;
ssize_t rlen = 0;
char *buf = uwsgi_request_body_read(wsgi_req, arg_len, &rlen);
if (buf == uwsgi.empty) {
return PyString_FromString("");
}
// some residual data ?
if (self->readline_pos && self->readline_size) {
if (len > 0) {
if ((size_t) len < (self->readline_size - self->readline_pos)) {
res = PyString_FromStringAndSize(self->readline + self->readline_pos, len);
self->readline_pos+=len;
if (self->readline_pos >= self->readline_size) self->readline_pos = 0;
return res;
}
}
self->readline_pos = 0;
return PyString_FromStringAndSize(self->readline + self->readline_pos, self->readline_size - self->readline_pos);
if (buf) {
return PyString_FromStringAndSize(buf, rlen);
}
// return the whole input
if (len <= 0) {
remains = self->wsgi_req->post_cl;
}
else {
remains = len ;
}
if (remains + self->pos > self->wsgi_req->post_cl) {
remains = self->wsgi_req->post_cl - self->pos;
}
if (remains <= 0) {
return PyString_FromString("");
}
if (uwsgi.post_buffering > 0) {
res = PyString_FromStringAndSize( self->wsgi_req->post_buffering_buf+self->pos, remains);
self->pos += remains;
return res;
}
tmp_buf = uwsgi_malloc(remains);
ssize_t rlen = up.hook_wsgi_input_read(self->wsgi_req, tmp_buf, remains, &tmp_pos);
// error ?
if (rlen < 0) {
free(tmp_buf);
return PyErr_Format(PyExc_IOError, "error reading for wsgi.input data: Content-Length %llu requested %llu received %llu pos %llu+%llu", (unsigned long long) self->wsgi_req->post_cl, (unsigned long long) remains, (unsigned long long) tmp_pos, (unsigned long long) self->pos, (unsigned long long) tmp_pos);
}
else if (rlen == 0) {
free(tmp_buf);
return PyErr_Format(PyExc_IOError, "error waiting for wsgi.input data: Content-Length %llu requested %llu received %llu pos %llu+%llu", (unsigned long long) self->wsgi_req->post_cl, (unsigned long long) remains, (unsigned long long) tmp_pos, (unsigned long long) self->pos, (unsigned long long) tmp_pos);
return PyErr_Format(PyExc_IOError, "error during read(%ld) on wsgi.input", arg_len);
}
self->pos += tmp_pos;
res = PyString_FromStringAndSize(tmp_buf, tmp_pos);
free(tmp_buf);
return res;
// timeout ?
return PyErr_Format(PyExc_IOError, "timeout during read(%ld) on wsgi.input", arg_len);
}
static PyObject *uwsgi_Input_readline(uwsgi_Input *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "|l:readline", &((uwsgi_Input *)self)->readline_max_size)) {
long hint = 0;
if (!PyArg_ParseTuple(args, "|l:readline", &hint)) {
return NULL;
}
if (!((uwsgi_Input *)self)->wsgi_req->post_cl || ((size_t) ((uwsgi_Input *)self)->pos >= ((uwsgi_Input *)self)->wsgi_req->post_cl && !((uwsgi_Input *)self)->readline_pos)) {
PyObject *line = uwsgi_Input_getline(self, hint);
if (!line) return NULL;
if (PyString_Size(line) == 0) {
Py_DECREF(line);
return PyString_FromString("");
}
return uwsgi_Input_getline(self);
return line;
}
static PyObject *uwsgi_Input_readlines(uwsgi_Input *self, PyObject *args) {
PyObject *res;
long hint = 0;
if (!((uwsgi_Input *)self)->wsgi_req->post_cl || ((size_t) ((uwsgi_Input *)self)->pos >= ((uwsgi_Input *)self)->wsgi_req->post_cl && !((uwsgi_Input *)self)->readline_pos)) {
Py_INCREF(Py_None);
return Py_None;
}
if (!PyArg_ParseTuple(args, "|l:readline", &hint)) {
return NULL;
}
res = PyList_New(0);
while( ((size_t) ((uwsgi_Input *)self)->pos < ((uwsgi_Input *)self)->wsgi_req->post_cl || ((uwsgi_Input *)self)->readline_pos > 0)) {
PyObject *a_line = uwsgi_Input_getline(self);
PyList_Append(res, a_line);
Py_DECREF(a_line);
PyObject *res = PyList_New(0);
for(;;) {
PyObject *line = uwsgi_Input_getline(self, hint);
if (!line) {
Py_DECREF(res);
return NULL;
}
if (PyString_Size(line) == 0) {
Py_DECREF(line);
return res;
}
PyList_Append(res, line);
Py_DECREF(line);
}
return res;
@@ -234,7 +131,7 @@ static PyObject *uwsgi_Input_close(uwsgi_Input *self, PyObject *args) {
static PyObject *uwsgi_Input_fileno(uwsgi_Input *self, PyObject *args) {
return PyInt_FromLong(self->wsgi_req->poll.fd);
return PyInt_FromLong(self->wsgi_req->fd);
}
static PyMethodDef uwsgi_Input_methods[] = {
@@ -311,9 +208,6 @@ PyObject *py_uwsgi_write(PyObject * self, PyObject * args) {
return Py_None;
}
#ifdef UWSGI_ASYNC
PyObject *py_eventfd_read(PyObject * self, PyObject * args) {
int fd, timeout = 0;
@@ -346,7 +240,6 @@ PyObject *py_eventfd_write(PyObject * self, PyObject * args) {
return PyString_FromString("");
}
#endif
int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
@@ -355,7 +248,6 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
int tmp_stderr;
int free_appid = 0;
#ifdef UWSGI_ASYNC
if (wsgi_req->async_status == UWSGI_AGAIN) {
wi = &uwsgi_apps[wsgi_req->app_id];
UWSGI_GET_GIL
@@ -380,11 +272,9 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
UWSGI_RELEASE_GIL
return ret;
}
#endif
/* Standard WSGI request */
if (!wsgi_req->uh.pktsize) {
if (!wsgi_req->uh->pktsize) {
uwsgi_log( "Empty python request. skip.\n");
return -1;
}
@@ -477,17 +367,13 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
while (wi->response_subhandler(wsgi_req) != UWSGI_OK) {
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
UWSGI_RELEASE_GIL
return UWSGI_AGAIN;
}
else {
#endif
wsgi_req->switches++;
#ifdef UWSGI_ASYNC
}
#endif
}
@@ -502,7 +388,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
/*
sorry that is a hack to avoid the rewrite of PyErr_Print
temporarily map (using dup2) stderr to wsgi_req->poll.fd
temporarily map (using dup2) stderr to wsgi_req->fd
*/
tmp_stderr = dup(2);
if (tmp_stderr < 0) {
@@ -510,7 +396,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
goto clear;
}
// map 2 to wsgi_req
if (dup2(wsgi_req->poll.fd, 2) < 0) {
if (dup2(wsgi_req->fd, 2) < 0) {
close(tmp_stderr);
uwsgi_error("dup2()");
goto clear;
@@ -527,9 +413,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
}
// this object must be freed/cleared always
#ifdef UWSGI_ASYNC
end:
#endif
if (wsgi_req->async_input) {
Py_DECREF((PyObject *)wsgi_req->async_input);
}
+5 -33
View File
@@ -32,8 +32,8 @@ void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_
Py_DECREF(pydictvalue);
}
if (wsgi_req->uh.modifier1 == UWSGI_MODIFIER_MANAGE_PATH_INFO) {
wsgi_req->uh.modifier1 = 0;
if (wsgi_req->uh->modifier1 == UWSGI_MODIFIER_MANAGE_PATH_INFO) {
wsgi_req->uh->modifier1 = 0;
pydictkey = PyDict_GetItemString(wsgi_req->async_environ, "SCRIPT_NAME");
if (pydictkey) {
if (PyString_Check(pydictkey)) {
@@ -49,36 +49,20 @@ void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_
}
// if async_post is mapped as a file, directly use it as wsgi.input
if (wsgi_req->async_post) {
#ifdef PYTHREE
wsgi_req->async_input = PyFile_FromFd(fileno((FILE *)wsgi_req->async_post), "wsgi_input", "rb", 0, NULL, NULL, NULL, 0);
#else
wsgi_req->async_input = PyFile_FromFile(wsgi_req->async_post, "wsgi_input", "r", NULL);
#endif
}
else {
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
((uwsgi_Input*)wsgi_req->async_input)->pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_pos = 0;
((uwsgi_Input*)wsgi_req->async_input)->readline_max_size = 0;
}
// create wsgi.input custom object
wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType);
((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req;
PyDict_SetItemString(wsgi_req->async_environ, "wsgi.input", wsgi_req->async_input);
PyDict_SetItemString(wsgi_req->async_environ, "wsgi.file_wrapper", wi->sendfile);
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.readable", wi->eventfd_read);
PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.writable", wi->eventfd_write);
PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.timeout", Py_None);
}
#endif
PyDict_SetItemString(wsgi_req->async_environ, "wsgi.version", wi->gateway_version);
@@ -134,16 +118,6 @@ void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_
Py_DECREF(zero);
}
// cache this ?
if (uwsgi.cluster_fd >= 0) {
zero = PyString_FromString(uwsgi.cluster);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster", zero);
Py_DECREF(zero);
zero = PyString_FromString(uwsgi.hostname);
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster_node", zero);
Py_DECREF(zero);
}
PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node);
// call
@@ -185,11 +159,9 @@ int uwsgi_response_subhandler_wsgi(struct wsgi_request *wsgi_req) {
if (!wsgi_req->async_placeholder) {
goto exception;
}
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
return UWSGI_AGAIN;
}
#endif
}
pychunk = PyIter_Next(wsgi_req->async_placeholder);