mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-10-03 15:38:36 +00:00
First destructive commit for 0.9.7, big python loaders refactoring
This commit is contained in:
+280
@@ -0,0 +1,280 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
/* notes
|
||||
|
||||
|
||||
exit(1) on every malloc error: apps can be dinamically loaded so on memory problem
|
||||
it is better to let the master process manager respawn the worker.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}};
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
|
||||
int init_uwsgi_app(struct uwsgi_server *uwsgi, PyObject *my_callable) {
|
||||
|
||||
PyObject *wsgi_module, *wsgi_dict;
|
||||
PyObject *zero;
|
||||
|
||||
int id = uwsgi->apps_cnt;
|
||||
|
||||
char *tmpstr;
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
int i;
|
||||
#endif
|
||||
|
||||
char *mountpoint;
|
||||
|
||||
struct uwsgi_app *wi;
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_req;
|
||||
|
||||
if (wsgi_req->script_name_len == 0) {
|
||||
wsgi_req->script_name = "";
|
||||
if (!uwsgi->vhost) id = 0;
|
||||
}
|
||||
else if (wsgi_req->script_name_len == 1) {
|
||||
if (wsgi_req->script_name[0] == '/') {
|
||||
if (!uwsgi->vhost) id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (uwsgi->vhost && wsgi_req->host_len > 0) {
|
||||
mountpoint = uwsgi_concat3n(wsgi_req->host, wsgi_req->host_len, "|", 1, wsgi_req->script_name, wsgi_req->script_name_len);
|
||||
}
|
||||
else {
|
||||
mountpoint = uwsgi_strncopy(wsgi_req->script_name, wsgi_req->script_name_len);
|
||||
}
|
||||
|
||||
if (uwsgi_get_app_id(uwsgi, mountpoint, strlen(mountpoint)) != -1) {
|
||||
uwsgi_log( "mountpoint %.*s already configured. skip.\n", strlen(mountpoint), mountpoint);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
wi = &uwsgi->apps[id];
|
||||
|
||||
memset(wi, 0, sizeof(struct uwsgi_app));
|
||||
wi->mountpoint = mountpoint;
|
||||
wi->mountpoint_len = strlen(mountpoint);
|
||||
|
||||
// dynamic chdir ?
|
||||
if (wsgi_req->chdir_len > 0) {
|
||||
wi->chdir = uwsgi_strncopy(wsgi_req->chdir, wsgi_req->chdir_len);
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("chdir to %s\n", wi->chdir);
|
||||
#endif
|
||||
if (chdir(wi->chdir)) {
|
||||
uwsgi_error("chdir()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Initialize a new environment for the new interpreter
|
||||
if (uwsgi->single_interpreter == 0) {
|
||||
|
||||
wi->interpreter = Py_NewInterpreter();
|
||||
if (!wi->interpreter) {
|
||||
uwsgi_log( "unable to initialize the new python interpreter\n");
|
||||
exit(1);
|
||||
}
|
||||
PyThreadState_Swap(wi->interpreter);
|
||||
init_pyargv(uwsgi);
|
||||
|
||||
#ifdef UWSGI_EMBEDDED
|
||||
// we need to inizialize an embedded module for every interpreter
|
||||
init_uwsgi_embedded_module();
|
||||
#endif
|
||||
init_uwsgi_vars();
|
||||
uwsgi_log( "interpreter for app %d initialized.\n", id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (my_callable) {
|
||||
wi->wsgi_callable = my_callable;
|
||||
Py_INCREF(my_callable);
|
||||
}
|
||||
else {
|
||||
if (wsgi_req->wsgi_script_len > 0) {
|
||||
wsgi_req->wsgi_callable = strchr(wsgi_req->wsgi_script, ':');
|
||||
if (wsgi_req->wsgi_callable) {
|
||||
wsgi_req->wsgi_callable[0] = 0;
|
||||
wsgi_req->wsgi_callable++;
|
||||
wsgi_req->wsgi_callable_len = wsgi_req->wsgi_script_len - strlen(wsgi_req->wsgi_script) - 1;
|
||||
wsgi_req->wsgi_script_len = strlen(wsgi_req->wsgi_script);
|
||||
}
|
||||
|
||||
tmpstr = uwsgi_strncopy(wsgi_req->wsgi_script, wsgi_req->wsgi_script_len);
|
||||
}
|
||||
else if (wsgi_req->wsgi_module_len > 0) {
|
||||
tmpstr = uwsgi_strncopy(wsgi_req->wsgi_module, wsgi_req->wsgi_module_len);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("ERROR: invalid dynamic app (please specify at least UWSGI_SCRIPT or UWSGI_MODULE/UWSGI_CALLABLE)\n");
|
||||
goto doh;
|
||||
}
|
||||
|
||||
wsgi_module = PyImport_ImportModule(tmpstr);
|
||||
free(tmpstr);
|
||||
if (!wsgi_module) goto doh;
|
||||
|
||||
wsgi_dict = PyModule_GetDict(wsgi_module);
|
||||
if (!wsgi_dict) goto doh;
|
||||
|
||||
if (!wsgi_req->wsgi_callable_len) {
|
||||
wsgi_req->wsgi_callable = "application";
|
||||
wsgi_req->wsgi_callable_len = 11;
|
||||
}
|
||||
|
||||
tmpstr = uwsgi_strncopy(wsgi_req->wsgi_callable, wsgi_req->wsgi_callable_len);
|
||||
wi->wsgi_callable = PyDict_GetItemString(wsgi_dict, tmpstr);
|
||||
free(tmpstr);
|
||||
}
|
||||
|
||||
if (!wi->wsgi_callable) {
|
||||
uwsgi_log("unable to find \"%.*s\" callable\n", wsgi_req->wsgi_callable_len, wsgi_req->wsgi_callable);
|
||||
goto doh;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_environ = malloc(sizeof(PyObject*)*uwsgi->async);
|
||||
if (!wi->wsgi_environ) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
wi->wsgi_environ[i] = PyDict_New();
|
||||
if (!wi->wsgi_environ[i]) {
|
||||
uwsgi_log("unable to allocate new env dictionary for app\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#else
|
||||
wi->wsgi_environ = PyDict_New();
|
||||
if (!wi->wsgi_environ) {
|
||||
uwsgi_log("unable to allocate new env dictionary for app\n");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// check function args
|
||||
// by defaut it is a WSGI app
|
||||
wi->argc = 2;
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__code__");
|
||||
if (!zero) {
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__call__");
|
||||
if (zero) {
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__code__");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("WARNING: unable to get the number of callable args. Fallback to WSGI\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (zero) {
|
||||
zero = PyObject_GetAttrString(zero, "co_argcount");
|
||||
wi->argc = (int) PyInt_AsLong(zero);
|
||||
}
|
||||
|
||||
if (wi->argc == 2) {
|
||||
uwsgi_log("-- WSGI callable detected --\n");
|
||||
wi->request_subhandler = uwsgi_request_subhandler_wsgi;
|
||||
wi->response_subhandler = uwsgi_response_subhandler_wsgi;
|
||||
}
|
||||
#ifdef UWSGI_WEB3
|
||||
else if (wi->argc == 1) {
|
||||
uwsgi_log("-- Web3 callable detected --\n");
|
||||
wi->request_subhandler = uwsgi_request_subhandler_web3;
|
||||
wi->response_subhandler = uwsgi_response_subhandler_web3;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
uwsgi_log("-- INVALID callable detected --\n");
|
||||
goto doh;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_args = malloc(sizeof(PyObject*)*uwsgi->async);
|
||||
if (!wi->wsgi_args) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
wi->wsgi_args[i] = PyTuple_New(wi->argc);
|
||||
if (!wi->wsgi_args[i]) {
|
||||
uwsgi_log("unable to allocate new tuple for app args\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// add start_response on WSGI app
|
||||
if (wi->argc == 2) {
|
||||
if (PyTuple_SetItem(wi->wsgi_args[i], 1, uwsgi->wsgi_spitout)) {
|
||||
uwsgi_log("unable to set start_response in args tuple\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
wi->wsgi_args = PyTuple_New(wi->argc);
|
||||
if (wi->argc == 2) {
|
||||
if (PyTuple_SetItem(wi->wsgi_args, 1, uwsgi->wsgi_spitout)) {
|
||||
uwsgi_log("unable to set start_response in args tuple\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (wi->argc == 2) {
|
||||
#ifdef UWSGI_SENDFILE
|
||||
// prepare sendfile() for WSGI app
|
||||
wi->wsgi_sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL);
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL);
|
||||
wi->wsgi_eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (uwsgi->single_interpreter == 0) {
|
||||
PyThreadState_Swap(uwsgi->main_thread);
|
||||
}
|
||||
|
||||
uwsgi_log( "application %d (%.*s) ready\n", id, wi->mountpoint_len, wi->mountpoint);
|
||||
|
||||
if (id == 0) {
|
||||
uwsgi_log( "setting default application to 0\n");
|
||||
uwsgi->default_app = 0;
|
||||
if (uwsgi->vhost) uwsgi->apps_cnt++;
|
||||
}
|
||||
else {
|
||||
uwsgi->apps_cnt++;
|
||||
}
|
||||
|
||||
return id;
|
||||
|
||||
doh:
|
||||
PyErr_Print();
|
||||
if (uwsgi->single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi->main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -442,13 +442,6 @@ inline struct wsgi_request *current_wsgi_req(struct uwsgi_server *uwsgi) {
|
||||
|
||||
void sanitize_args(struct uwsgi_server *uwsgi) {
|
||||
|
||||
#ifdef UWSGI_PROFILER
|
||||
if (uwsgi->enable_profiler) {
|
||||
uwsgi_log("*** Profiler enabled, do not use it in production environment !!! ***\n");
|
||||
uwsgi->async = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_UGREEN
|
||||
#ifdef UWSGI_THREADING
|
||||
if (uwsgi->ugreen) {
|
||||
|
||||
@@ -35,11 +35,6 @@ struct uwsgi_server uwsgi;
|
||||
|
||||
extern char **environ;
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}};
|
||||
#endif
|
||||
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"socket", required_argument, 0, 's'},
|
||||
{"processes", required_argument, 0, 'p'},
|
||||
@@ -353,17 +348,9 @@ void what_i_am_doing() {
|
||||
|
||||
}
|
||||
|
||||
PyObject *wsgi_spitout;
|
||||
|
||||
PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} };
|
||||
PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, 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
|
||||
|
||||
|
||||
// process manager is now (20090725) available on Unbit
|
||||
pid_t masterpid;
|
||||
pid_t diedpid;
|
||||
@@ -913,7 +900,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
uwsgi.apps_cnt = 0 ;
|
||||
}
|
||||
|
||||
wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL);
|
||||
uwsgi.wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL);
|
||||
uwsgi.wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL);
|
||||
|
||||
#ifdef UWSGI_EMBEDDED
|
||||
@@ -1973,450 +1960,6 @@ void init_uwsgi_vars() {
|
||||
|
||||
}
|
||||
|
||||
int init_uwsgi_app(PyObject * force_wsgi_dict, PyObject * my_callable) {
|
||||
PyObject *wsgi_module, *wsgi_dict = NULL;
|
||||
PyObject *zero;
|
||||
#ifdef UWSGI_PROFILER
|
||||
PyObject *pymain, *pycprof, *pycprof_dict;
|
||||
#endif
|
||||
char tmpstring[256];
|
||||
int id;
|
||||
#ifdef UWSGI_ASYNC
|
||||
int i;
|
||||
#endif
|
||||
|
||||
char *mountpoint;
|
||||
|
||||
struct uwsgi_app *wi;
|
||||
|
||||
memset(tmpstring, 0, 256);
|
||||
|
||||
|
||||
if (uwsgi.wsgi_req->wsgi_script_len == 0 && ((uwsgi.wsgi_req->wsgi_module_len == 0 || uwsgi.wsgi_req->wsgi_callable_len == 0) && uwsgi.wsgi_config == NULL && my_callable == NULL)) {
|
||||
uwsgi_log( "invalid application (%.*s). skip.\n", uwsgi.wsgi_req->script_name_len, uwsgi.wsgi_req->script_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi.wsgi_config && uwsgi.wsgi_req->wsgi_callable_len == 0 && my_callable == NULL) {
|
||||
uwsgi_log( "invalid application (%.*s). skip.\n", uwsgi.wsgi_req->script_name_len, uwsgi.wsgi_req->script_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi.wsgi_req->wsgi_script_len > 255 || uwsgi.wsgi_req->wsgi_module_len > 255 || uwsgi.wsgi_req->wsgi_callable_len > 255) {
|
||||
uwsgi_log( "invalid application's string size. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
id = uwsgi.apps_cnt;
|
||||
|
||||
|
||||
if (uwsgi.wsgi_req->script_name_len == 0) {
|
||||
uwsgi.wsgi_req->script_name = "";
|
||||
if (!uwsgi.vhost) {
|
||||
id = 0;
|
||||
}
|
||||
}
|
||||
else if (uwsgi.wsgi_req->script_name_len == 1) {
|
||||
if (uwsgi.wsgi_req->script_name[0] == '/') {
|
||||
if (!uwsgi.vhost) {
|
||||
id = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (uwsgi.vhost) {
|
||||
mountpoint = uwsgi_concat3n(uwsgi.wsgi_req->host, uwsgi.wsgi_req->host_len, "|", 1, uwsgi.wsgi_req->script_name, uwsgi.wsgi_req->script_name_len);
|
||||
}
|
||||
else {
|
||||
mountpoint = uwsgi_strncopy(uwsgi.wsgi_req->script_name, uwsgi.wsgi_req->script_name_len);
|
||||
}
|
||||
|
||||
if (uwsgi_get_app_id(&uwsgi, mountpoint, strlen(mountpoint)) != -1) {
|
||||
uwsgi_log( "mountpoint %.*s already configured. skip.\n", strlen(mountpoint), mountpoint);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
wi = &uwsgi.apps[id];
|
||||
|
||||
memset(wi, 0, sizeof(struct uwsgi_app));
|
||||
|
||||
wi->mountpoint = mountpoint;
|
||||
wi->mountpoint_len = strlen(mountpoint);
|
||||
|
||||
// dynamic chdir ?
|
||||
|
||||
|
||||
if (uwsgi.wsgi_req->chdir_len > 0) {
|
||||
wi->chdir = malloc(uwsgi.wsgi_req->chdir_len + 1);
|
||||
if (wi->chdir == NULL) {
|
||||
uwsgi_error("malloc()");
|
||||
}
|
||||
memcpy(wi->chdir, uwsgi.wsgi_req->chdir, uwsgi.wsgi_req->chdir_len);
|
||||
wi->chdir[uwsgi.wsgi_req->chdir_len] = 0;
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("chdir to %s\n", wi->chdir);
|
||||
#endif
|
||||
if (chdir(wi->chdir)) {
|
||||
uwsgi_error("chdir()");
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
|
||||
wi->interpreter = Py_NewInterpreter();
|
||||
if (!wi->interpreter) {
|
||||
uwsgi_log( "unable to initialize the new interpreter\n");
|
||||
exit(1);
|
||||
}
|
||||
PyThreadState_Swap(wi->interpreter);
|
||||
init_pyargv(&uwsgi);
|
||||
|
||||
#ifdef UWSGI_EMBEDDED
|
||||
// we need to inizialize an embedded module for every interpreter
|
||||
init_uwsgi_embedded_module();
|
||||
#endif
|
||||
init_uwsgi_vars();
|
||||
uwsgi_log( "interpreter for app %d initialized.\n", id);
|
||||
}
|
||||
|
||||
if (uwsgi.eval) {
|
||||
wi->wsgi_callable = my_callable;
|
||||
Py_INCREF(my_callable);
|
||||
}
|
||||
#ifdef UWSGI_PASTE
|
||||
else if (uwsgi.paste) {
|
||||
wi->wsgi_callable = my_callable;
|
||||
Py_INCREF(my_callable);
|
||||
}
|
||||
#endif
|
||||
else if (uwsgi.wsgi_file) {
|
||||
wi->wsgi_callable = my_callable;
|
||||
Py_INCREF(my_callable);
|
||||
}
|
||||
else {
|
||||
|
||||
if (uwsgi.wsgi_config == NULL) {
|
||||
if (uwsgi.wsgi_req->wsgi_script_len > 0) {
|
||||
uwsgi.wsgi_req->wsgi_callable = strchr(uwsgi.wsgi_req->wsgi_script, ':');
|
||||
if (uwsgi.wsgi_req->wsgi_callable) {
|
||||
uwsgi.wsgi_req->wsgi_callable[0] = 0;
|
||||
uwsgi.wsgi_req->wsgi_callable++;
|
||||
uwsgi.wsgi_req->wsgi_callable_len = uwsgi.wsgi_req->wsgi_script_len - strlen(uwsgi.wsgi_req->wsgi_script) - 1;
|
||||
uwsgi.wsgi_req->wsgi_script_len = strlen(uwsgi.wsgi_req->wsgi_script);
|
||||
}
|
||||
else {
|
||||
uwsgi.wsgi_req->wsgi_callable = "application";
|
||||
uwsgi.wsgi_req->wsgi_callable_len = 11;
|
||||
}
|
||||
memcpy(tmpstring, uwsgi.wsgi_req->wsgi_script, uwsgi.wsgi_req->wsgi_script_len);
|
||||
wsgi_module = PyImport_ImportModule(tmpstring);
|
||||
if (!wsgi_module) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
memcpy(tmpstring, uwsgi.wsgi_req->wsgi_module, uwsgi.wsgi_req->wsgi_module_len);
|
||||
wsgi_module = PyImport_ImportModule(tmpstring);
|
||||
if (!wsgi_module) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
wsgi_dict = PyModule_GetDict(wsgi_module);
|
||||
if (!wsgi_dict) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
wsgi_dict = force_wsgi_dict;
|
||||
}
|
||||
|
||||
|
||||
memset(tmpstring, 0, 256);
|
||||
memcpy(tmpstring, uwsgi.wsgi_req->wsgi_callable, uwsgi.wsgi_req->wsgi_callable_len);
|
||||
if (my_callable) {
|
||||
wi->wsgi_callable = my_callable;
|
||||
Py_INCREF(my_callable);
|
||||
}
|
||||
else if (wsgi_dict) {
|
||||
wi->wsgi_callable = PyDict_GetItemString(wsgi_dict, tmpstring);
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
wi->wsgi_dict = wsgi_dict ;
|
||||
|
||||
if (!wi->wsgi_callable) {
|
||||
PyErr_Print();
|
||||
uwsgi_log("unable to find \"%s\" callable\n", tmpstring);
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_environ = malloc(sizeof(PyObject*)*uwsgi.async);
|
||||
if (!wi->wsgi_environ) {
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
for(i=0;i<uwsgi.async;i++) {
|
||||
wi->wsgi_environ[i] = PyDict_New();
|
||||
// this will leak all the already allocated dictionary !!!
|
||||
if (!wi->wsgi_environ[i]) {
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
}
|
||||
#else
|
||||
wi->wsgi_environ = PyDict_New();
|
||||
if (!wi->wsgi_environ) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
if (wsgi_dict) {
|
||||
wi->wsgi_harakiri = PyDict_GetItemString(wsgi_dict, "harakiri");
|
||||
if (wi->wsgi_harakiri) {
|
||||
uwsgi_log( "initialized Harakiri custom handler: %p.\n", wi->wsgi_harakiri);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef UWSGI_PROFILER
|
||||
if (uwsgi.enable_profiler) {
|
||||
pymain = PyImport_AddModule("__main__");
|
||||
if (!pymain) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
wi->pymain_dict = PyModule_GetDict(pymain);
|
||||
if (!wi->pymain_dict) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
if (PyDict_SetItem(wi->pymain_dict, PyString_FromFormat("uwsgi_application__%d", id), wi->wsgi_callable)) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (PyDict_SetItem(wi->pymain_dict, PyString_FromFormat("uwsgi_spit__%d", id), wsgi_spitout)) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pycprof = PyImport_ImportModule("cProfile");
|
||||
if (!pycprof) {
|
||||
PyErr_Print();
|
||||
uwsgi_log( "trying old profile module...\n");
|
||||
pycprof = PyImport_ImportModule("profile");
|
||||
if (!pycprof) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
pycprof_dict = PyModule_GetDict(pycprof);
|
||||
if (!pycprof_dict) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
wi->wsgi_cprofile_run = PyDict_GetItemString(pycprof_dict, "run");
|
||||
if (!wi->wsgi_cprofile_run) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_args = malloc(sizeof(PyObject*));
|
||||
if (!wi->wsgi_args) {
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
wi->wsgi_args[0] = PyTuple_New(1);
|
||||
if (PyTuple_SetItem(wi->wsgi_args[0], 0, PyString_FromFormat("uwsgi_out = uwsgi_application__%d(uwsgi_environ__%d,uwsgi_spit__%d)", id, id, id))) {
|
||||
#else
|
||||
wi->wsgi_args = PyTuple_New(1);
|
||||
if (PyTuple_SetItem(wi->wsgi_args, 0, PyString_FromFormat("uwsgi_out = uwsgi_application__%d(uwsgi_environ__%d,uwsgi_spit__%d)", id, id, id))) {
|
||||
#endif
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_WEB3
|
||||
|
||||
|
||||
// check function args
|
||||
// by defaut is a WSGI app
|
||||
wi->argc = 2;
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__code__");
|
||||
if (!zero) {
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__call__");
|
||||
if (zero) {
|
||||
zero = PyObject_GetAttrString(wi->wsgi_callable, "__code__");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("WARNING: unable to get the number of callable args. Fallback to WSGI\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (zero) {
|
||||
zero = PyObject_GetAttrString(zero, "co_argcount");
|
||||
wi->argc = (int) PyInt_AsLong(zero);
|
||||
}
|
||||
|
||||
if (wi->argc == 1) {
|
||||
uwsgi_log("-- Web3 callable detected --\n");
|
||||
wi->request_subhandler = uwsgi_request_subhandler_web3;
|
||||
wi->response_subhandler = uwsgi_response_subhandler_web3;
|
||||
}
|
||||
else if (wi->argc == 2) {
|
||||
uwsgi_log("-- WSGI callable detected --\n");
|
||||
wi->request_subhandler = uwsgi_request_subhandler_wsgi;
|
||||
wi->response_subhandler = uwsgi_response_subhandler_wsgi;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("-- INVALID callable detected --\n");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_args = malloc(sizeof(PyObject*)*uwsgi.async);
|
||||
if (!wi->wsgi_args) {
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
for(i=0;i<uwsgi.async;i++) {
|
||||
wi->wsgi_args[i] = PyTuple_New(wi->argc);
|
||||
// this will leak all the already allocated dictionary !!!
|
||||
if (!wi->wsgi_args[i]) {
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
if (wi->argc > 1) {
|
||||
if (PyTuple_SetItem(wi->wsgi_args[i], 1, wsgi_spitout)) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
wi->wsgi_args = PyTuple_New(wi->argc);
|
||||
if (wi->argc > 1) {
|
||||
if (PyTuple_SetItem(wi->wsgi_args, 1, wsgi_spitout)) {
|
||||
PyErr_Print();
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_PROFILER
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
// prepare sendfile()
|
||||
wi->wsgi_sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL);
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL);
|
||||
wi->wsgi_eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL);
|
||||
#endif
|
||||
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
PyThreadState_Swap(uwsgi.main_thread);
|
||||
}
|
||||
|
||||
|
||||
uwsgi_log( "application %d (%.*s) ready\n", id, wi->mountpoint_len, wi->mountpoint);
|
||||
|
||||
if (id == 0) {
|
||||
uwsgi_log( "setting default application to 0\n");
|
||||
uwsgi.default_app = 0;
|
||||
if (uwsgi.vhost) {
|
||||
uwsgi.apps_cnt++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi.apps_cnt++;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_PASTE
|
||||
void uwsgi_paste_config() {
|
||||
PyObject *paste_module, *paste_dict, *paste_loadapp;
|
||||
@@ -2460,7 +2003,7 @@ void uwsgi_paste_config() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
init_uwsgi_app(NULL, paste_app);
|
||||
init_uwsgi_app(&uwsgi, paste_app);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -2527,7 +2070,7 @@ void uwsgi_eval_config(char *code) {
|
||||
}
|
||||
|
||||
|
||||
ret = init_uwsgi_app(NULL, wsgi_eval_callable);
|
||||
ret = init_uwsgi_app(&uwsgi, wsgi_eval_callable);
|
||||
|
||||
}
|
||||
|
||||
@@ -2593,7 +2136,7 @@ void uwsgi_wsgi_file_config() {
|
||||
}
|
||||
|
||||
|
||||
ret = init_uwsgi_app(NULL, wsgi_file_callable);
|
||||
ret = init_uwsgi_app(&uwsgi, wsgi_file_callable);
|
||||
|
||||
}
|
||||
|
||||
@@ -2813,10 +2356,10 @@ void uwsgi_wsgi_config(char *filename) {
|
||||
if (PyString_Check(app_app)) {
|
||||
uwsgi.wsgi_req->wsgi_callable = PyString_AsString(app_app);
|
||||
uwsgi.wsgi_req->wsgi_callable_len = strlen(uwsgi.wsgi_req->wsgi_callable);
|
||||
ret = init_uwsgi_app(wsgi_dict, NULL);
|
||||
ret = init_uwsgi_app(&uwsgi, NULL);
|
||||
}
|
||||
else {
|
||||
ret = init_uwsgi_app(wsgi_dict, app_app);
|
||||
ret = init_uwsgi_app(&uwsgi, app_app);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
@@ -2924,7 +2467,7 @@ void init_uwsgi_embedded_module() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (PyDict_SetItemString(uwsgi.embedded_dict, "start_response", wsgi_spitout)) {
|
||||
if (PyDict_SetItemString(uwsgi.embedded_dict, "start_response", uwsgi.wsgi_spitout)) {
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ struct uwsgi_app {
|
||||
|
||||
PyThreadState *interpreter;
|
||||
PyObject *pymain_dict;
|
||||
PyObject *wsgi_dict;
|
||||
//PyObject *wsgi_dict;
|
||||
PyObject *wsgi_callable;
|
||||
|
||||
|
||||
@@ -728,6 +728,8 @@ struct uwsgi_server {
|
||||
int get_tcp_info;
|
||||
#endif
|
||||
|
||||
PyObject *wsgi_spitout;
|
||||
|
||||
};
|
||||
|
||||
struct uwsgi_cluster_node {
|
||||
@@ -888,7 +890,7 @@ uint32_t uwsgi_swap32(uint32_t);
|
||||
uint64_t uwsgi_swap64(uint64_t);
|
||||
#endif
|
||||
|
||||
int init_uwsgi_app(PyObject *, PyObject *);
|
||||
int init_uwsgi_app(struct uwsgi_server *, PyObject *);
|
||||
|
||||
PyObject *uwsgi_send_message(const char *, int, uint8_t, uint8_t, char *, int, int);
|
||||
|
||||
|
||||
+1
-8
@@ -11,7 +11,6 @@ UDP=True
|
||||
MULTICAST=True
|
||||
THREADING=True
|
||||
SENDFILE=True
|
||||
PROFILER=False
|
||||
NAGIOS=True
|
||||
PROXY=True
|
||||
PASTE=True
|
||||
@@ -90,7 +89,7 @@ gcc_major = int(gcc_version.split('.')[0])
|
||||
gcc_minor = int(gcc_version.split('.')[1])
|
||||
|
||||
|
||||
gcc_list = ['utils', 'pyutils', 'protocol', 'socket', 'logging', 'wsgi_handlers', 'wsgi_subhandler', 'wsgi_headers', 'uwsgi_handlers', 'plugins', 'uwsgi']
|
||||
gcc_list = ['utils', 'pyutils', 'pyloader', 'protocol', 'socket', 'logging', 'wsgi_handlers', 'wsgi_subhandler', 'wsgi_headers', 'uwsgi_handlers', 'plugins', 'uwsgi']
|
||||
|
||||
cflags = ['-O2', '-Wall', '-Werror', '-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64'] + os.environ.get("CFLAGS", "").split()
|
||||
|
||||
@@ -193,9 +192,6 @@ def unbit_setup():
|
||||
global SENDFILE
|
||||
SENDFILE=True
|
||||
|
||||
global PROFILER
|
||||
PROFILER=False
|
||||
|
||||
global NAGIOS
|
||||
NAGIOS=False
|
||||
|
||||
@@ -377,9 +373,6 @@ def parse_vars():
|
||||
if THREADING:
|
||||
cflags.append("-DUWSGI_THREADING")
|
||||
|
||||
if PROFILER:
|
||||
cflags.append("-DUWSGI_PROFILER")
|
||||
|
||||
if PASTE:
|
||||
cflags.append('-DUWSGI_PASTE')
|
||||
|
||||
|
||||
+1
-1
@@ -155,7 +155,7 @@ int uwsgi_request_wsgi(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
/* unavailable app for this SCRIPT_NAME */
|
||||
wsgi_req->app_id = -1;
|
||||
if (wsgi_req->wsgi_script_len > 0 || (wsgi_req->wsgi_callable_len > 0 && wsgi_req->wsgi_module_len > 0)) {
|
||||
wsgi_req->app_id = init_uwsgi_app(NULL, NULL);
|
||||
wsgi_req->app_id = init_uwsgi_app(uwsgi, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-19
@@ -76,26 +76,9 @@ void *uwsgi_request_subhandler_wsgi(struct uwsgi_server *uwsgi, struct wsgi_requ
|
||||
|
||||
// call
|
||||
|
||||
#ifdef UWSGI_PROFILER
|
||||
if (uwsgi->enable_profiler == 1) {
|
||||
PyDict_SetItem(wi->pymain_dict, PyString_FromFormat("uwsgi_environ__%d", wsgi_req->app_id), wsgi_req->async_environ);
|
||||
wsgi_req->async_result = python_call(wi->wsgi_cprofile_run, wsgi_req->async_args, 0);
|
||||
if (wsgi_req->async_result) {
|
||||
wsgi_req->async_result = PyDict_GetItemString(wi->pymain_dict, "uwsgi_out");
|
||||
Py_INCREF((PyObject*)wsgi_req->async_result);
|
||||
Py_INCREF((PyObject*)wsgi_req->async_result);
|
||||
}
|
||||
}
|
||||
else {
|
||||
#endif
|
||||
|
||||
|
||||
PyTuple_SetItem(wsgi_req->async_args, 0, wsgi_req->async_environ);
|
||||
return python_call(wsgi_req->async_app, wsgi_req->async_args, uwsgi->catch_exceptions);
|
||||
|
||||
#ifdef UWSGI_PROFILER
|
||||
}
|
||||
#endif
|
||||
PyTuple_SetItem(wsgi_req->async_args, 0, wsgi_req->async_environ);
|
||||
return python_call(wsgi_req->async_app, wsgi_req->async_args, uwsgi->catch_exceptions);
|
||||
}
|
||||
|
||||
int uwsgi_response_subhandler_wsgi(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
@@ -179,7 +179,7 @@ next:
|
||||
}
|
||||
wsgi_req->wsgi_script = (char *) xml_uwsgi_script;
|
||||
wsgi_req->wsgi_script_len = strlen(wsgi_req->wsgi_script);
|
||||
init_uwsgi_app(NULL, NULL);
|
||||
init_uwsgi_app(&uwsgi, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -314,7 +314,7 @@ void uwsgi_textApp(void *userData, const char *s, int len) {
|
||||
if (current_xmlnode) {
|
||||
wsgi_req->wsgi_script = (char *) s;
|
||||
wsgi_req->wsgi_script_len = len;
|
||||
init_uwsgi_app(NULL, NULL);
|
||||
init_uwsgi_app(&uwsgi, NULL);
|
||||
current_xmlnode = 0;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user