From f7aa451af42cbcaf1f9bd94ae3c162d54721112f Mon Sep 17 00:00:00 2001 From: "roberto@sirius" Date: Wed, 20 Oct 2010 21:54:19 +0200 Subject: [PATCH] uWSGI is now language agnostic, all can be pluginized, python included --- async.c | 2 +- ini.c | 2 +- logging.c | 2 + loop.c | 15 +- master.c | 36 +- plugins.c | 52 +-- plugins/psgi/psgi_plugin.c | 13 + protocol.c | 3 + pyloader.c | 592 ------------------------ pyutils.c | 96 ---- sendfile.c | 29 +- snmp.c | 2 + utils.c | 43 +- uwsgi.c | 912 +++++++++---------------------------- uwsgi.h | 360 ++++----------- uwsgi_handlers.c | 55 --- uwsgiconfig.py | 534 ++++++++-------------- web3_subhandler.c | 165 ------- wsgi_handlers.c | 413 ----------------- wsgi_headers.c | 160 ------- wsgi_subhandler.c | 215 --------- xmlconf.c | 14 +- 22 files changed, 607 insertions(+), 3108 deletions(-) delete mode 100644 pyloader.c delete mode 100644 pyutils.c delete mode 100644 web3_subhandler.c delete mode 100644 wsgi_handlers.c delete mode 100644 wsgi_headers.c delete mode 100644 wsgi_subhandler.c diff --git a/async.c b/async.c index 0a7e52a7..db6d02e6 100644 --- a/async.c +++ b/async.c @@ -436,7 +436,7 @@ struct wsgi_request * async_loop() { uwsgi.async_running = 0 ; // st global wsgi_req for python functions uwsgi.wsgi_req = wsgi_req ; - wsgi_req->async_status = (*uwsgi.shared->hooks[wsgi_req->uh.modifier1]) (wsgi_req); + wsgi_req->async_status = (*uwsgi.shared->hook_request[wsgi_req->uh.modifier1]) (wsgi_req); if (wsgi_req->async_status < UWSGI_AGAIN) { return wsgi_req; diff --git a/ini.c b/ini.c index 653f0598..1d2b3ae4 100644 --- a/ini.c +++ b/ini.c @@ -164,7 +164,7 @@ void uwsgi_ini_config(char *file, struct option *long_options) { *aopt->flag = aopt->val; } else { - manage_opt(aopt->val, val); + manage_opt(aopt->val, val); } } lopt++; diff --git a/logging.c b/logging.c index 9b5c311c..4a0d7524 100644 --- a/logging.c +++ b/logging.c @@ -40,6 +40,8 @@ void log_request(struct wsgi_request *wsgi_req) { struct uwsgi_app *wi; + uwsgi_log("LOGGING...\n"); + if (wsgi_req->app_id >= 0) { wi = &uwsgi.apps[wsgi_req->app_id]; if (wi->requests > 0) { diff --git a/loop.c b/loop.c index fd0be76c..0912beb3 100644 --- a/loop.c +++ b/loop.c @@ -2,26 +2,37 @@ extern struct uwsgi_server uwsgi; +struct wsgi_request* threaded_current_wsgi_req() { return pthread_getspecific(uwsgi.tur_key); } +struct wsgi_request* simple_current_wsgi_req() { return uwsgi.wsgi_req ; } + void *simple_loop(void *arg1) { long core_id = (long) arg1; struct wsgi_request *wsgi_req = uwsgi.wsgi_requests[core_id]; + int i; #ifdef UWSGI_THREADING - PyThreadState *pts; + //PyThreadState *pts; sigset_t smask; if (uwsgi.threads > 1) { - pthread_setspecific(uwsgi.ut_key, (void *) wsgi_req); + pthread_setspecific(uwsgi.tur_key, (void *) wsgi_req); if (core_id > 0) { // block all signals on new threads sigfillset(&smask); pthread_sigmask(SIG_BLOCK, &smask, NULL); + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_init_thread[i]) { + uwsgi.shared->hook_init_thread[i](); + } + } + /* pts = PyThreadState_New(uwsgi.main_thread->interp); pthread_setspecific(uwsgi.ut_save_key, (void *) pts); + */ } } #endif diff --git a/master.c b/master.c index 661135e3..ad5bf4e9 100644 --- a/master.c +++ b/master.c @@ -45,20 +45,16 @@ void master_loop(char **argv, char **environ) { struct sockaddr_in udp_client; socklen_t udp_len; char udp_client_addr[16]; - PyObject *udp_callable; - PyObject *udp_callable_args = NULL; - PyObject *udp_response; + int udp_managed = 0 ; + int rlen; #endif int i,j; -#ifdef UWSGI_UDP - int rlen; -#endif struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; // release the GIL - UWSGI_RELEASE_GIL + //UWSGI_RELEASE_GIL /* route signals to workers... */ signal(SIGHUP, (void *) &grace_them_all); @@ -104,14 +100,6 @@ void master_loop(char **argv, char **environ) { } #endif -#ifdef UWSGI_UDP - UWSGI_GET_GIL - udp_callable = PyDict_GetItemString(uwsgi.embedded_dict, "udp_callable"); - if (udp_callable) { - udp_callable_args = PyTuple_New(3); - } - UWSGI_RELEASE_GIL -#endif for (;;) { if (ready_to_die >= uwsgi.numproc && uwsgi.to_hell) { #ifdef UWSGI_SPOOLER @@ -236,6 +224,18 @@ void master_loop(char **argv, char **environ) { } #endif else { + + // loop the various udp manager until one returns true + udp_managed = 0; + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_manage_udp[i]) { + if (uwsgi.shared->hook_manage_udp[i](udp_client_addr, udp_client.sin_port, uwsgi.wsgi_req->buffer, rlen)) { + udp_managed = 1; + break; + } + } + } + /* if (udp_callable && udp_callable_args) { UWSGI_GET_GIL PyTuple_SetItem(udp_callable_args, 0, PyString_FromString(udp_client_addr)); @@ -252,8 +252,12 @@ void master_loop(char **argv, char **environ) { } else { // a simple udp logger + */ + + if (!udp_managed) { uwsgi_log( "[udp:%s:%d] %.*s", udp_client_addr, ntohs(udp_client.sin_port), rlen, uwsgi.wsgi_req->buffer); } + //} } } else { @@ -376,7 +380,7 @@ void master_loop(char **argv, char **environ) { if (WIFEXITED(waitpid_status)) { if (WEXITSTATUS(waitpid_status) != UWSGI_END_CODE) { uwsgi_log( "OOOPS the proxy is no more...trying respawn...\n"); - uwsgi.shared->spooler_pid = proxy_start(1); + uwsgi.shared->proxy_pid = proxy_start(1); continue; } } diff --git a/plugins.c b/plugins.c index 147952e5..567a5577 100644 --- a/plugins.c +++ b/plugins.c @@ -23,26 +23,18 @@ void embed_plugins() { int uwsgi_load_plugin(int modifier, char *plugin, char *pargs, int absolute) { - char *plugin_name ; - void *plugin_handle; - int (*plugin_init) (char *); - int (*plugin_request) (struct wsgi_request *); - void (*plugin_after_request) (struct wsgi_request *); - + + char *plugin_name ; + char *plugin_entry_symbol; + struct uwsgi_plugin *up; + if (absolute) { plugin_name = malloc(strlen(plugin) + 1); memcpy(plugin_name, plugin, strlen(plugin) + 1); } else { - plugin_name = malloc(strlen(UWSGI_PLUGIN_DIR) + 1 + strlen(plugin) + 1); - if (!plugin_name) { - uwsgi_error("malloc()"); - return -1 ; - } - memcpy(plugin_name, UWSGI_PLUGIN_DIR, strlen(UWSGI_PLUGIN_DIR)); - memcpy(plugin_name + strlen(UWSGI_PLUGIN_DIR) , "/", 1); - memcpy(plugin_name + strlen(UWSGI_PLUGIN_DIR) + 1, plugin, strlen(plugin) + 1); + plugin_name = uwsgi_concat4(UWSGI_PLUGIN_DIR, "/", plugin, "_plugin.so"); } plugin_handle = dlopen(plugin_name, RTLD_NOW | RTLD_GLOBAL); free(plugin_name); @@ -51,33 +43,17 @@ int uwsgi_load_plugin(int modifier, char *plugin, char *pargs, int absolute) { uwsgi_log( "%s\n", dlerror()); } else { - plugin_init = dlsym(plugin_handle, "uwsgi_init"); - if (plugin_init) { - if ((*plugin_init) (pargs)) { - uwsgi_log( "plugin initialization returned error\n"); - if (dlclose(plugin_handle)) { - uwsgi_log( "unable to unload plugin\n"); - } - - return -1; - } - } - - plugin_request = dlsym(plugin_handle, "uwsgi_request"); - if (plugin_request) { - uwsgi.shared->hooks[modifier] = plugin_request; - plugin_after_request = dlsym(plugin_handle, "uwsgi_after_request"); - if (plugin_after_request) { - uwsgi.shared->after_hooks[modifier] = plugin_after_request; - } - return 0; - - } - else { - uwsgi_log( "%s\n", dlerror()); + plugin_entry_symbol = uwsgi_concat2(plugin, "_plugin"); + up = dlsym(plugin_handle, plugin_entry_symbol); + if (up) { + fill_plugin_table(up->modifier1, up); + return 1; } + uwsgi_log( "%s\n", dlerror()); } + return 0; + return -1; } diff --git a/plugins/psgi/psgi_plugin.c b/plugins/psgi/psgi_plugin.c index 52820132..2a056ced 100644 --- a/plugins/psgi/psgi_plugin.c +++ b/plugins/psgi/psgi_plugin.c @@ -420,3 +420,16 @@ void uwsgi_after_request(struct wsgi_request *wsgi_req) { if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) log_request(wsgi_req); } + +struct uwsgi_plugin psgi_plugin = { + + .init = uwsgi_init; + .post_fork = uwsgi_post_fork; + .options = uwsgi_perl_options; + .magic = uwsgi_perl_magic; + .help = uwsgi_perl_help; + .init_thread = uwsgi_init_thread; + .request = uwsgi_request; + .post_request = uwsgi_post_request; + .init_app = uwsgi_init_app; +}; diff --git a/protocol.c b/protocol.c index 627d2a04..f4d7b6eb 100644 --- a/protocol.c +++ b/protocol.c @@ -127,6 +127,7 @@ int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modif return uwsgi_poll.fd; } +/* PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint8_t modifier2, char *message, int size, int timeout) { struct pollfd uwsgi_mpoll; @@ -214,6 +215,8 @@ PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint return PyMarshal_ReadObjectFromString(buffer, uh.pktsize); } +*/ + int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header *uh, char *buffer) { int rlen, i; diff --git a/pyloader.c b/pyloader.c deleted file mode 100644 index 832c1d99..00000000 --- a/pyloader.c +++ /dev/null @@ -1,592 +0,0 @@ -#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. - - -*/ - -extern struct uwsgi_server uwsgi; - -#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(int loader, void *arg1, struct wsgi_request *wsgi_req, int new_interpreter) { - - PyObject *zero; - - int id = uwsgi.apps_cnt; - -#ifdef UWSGI_ASYNC - int i; -#endif - - char *mountpoint; - - struct uwsgi_app *wi; - - - 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(mountpoint, strlen(mountpoint)) != -1) { - uwsgi_log( "mountpoint %.*s already configured. skip.\n", strlen(mountpoint), mountpoint); - free(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 (new_interpreter && id) { - 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(); - -#ifdef UWSGI_EMBEDDED - // we need to inizialize an embedded module for every interpreter - init_uwsgi_embedded_module(); -#endif - init_uwsgi_vars(); - - } - else { - wi->interpreter = uwsgi.main_thread; - } - - - wi->wsgi_callable = uwsgi.loaders[loader](arg1); - - if (!wi->wsgi_callable) { - uwsgi_log("unable to load app SCRIPT_NAME=%s\n", mountpoint); - goto doh; - } - -#ifdef UWSGI_ASYNC - wi->wsgi_environ = malloc(sizeof(PyObject*)*uwsgi.cores); - if (!wi->wsgi_environ) { - uwsgi_error("malloc()"); - exit(1); - } - - for(i=0;iwsgi_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"); - } - } - - // avoid __code__ attr error propagation - PyErr_Clear(); - - if (zero) { - zero = PyObject_GetAttrString(zero, "co_argcount"); - wi->argc = (int) PyInt_AsLong(zero); - } - - if (wi->argc == 2) { -#ifdef UWSGI_DEBUG - uwsgi_log("-- WSGI callable detected --\n"); -#endif - wi->request_subhandler = uwsgi_request_subhandler_wsgi; - wi->response_subhandler = uwsgi_response_subhandler_wsgi; - } -#ifdef UWSGI_WEB3 - else if (wi->argc == 1) { -#ifdef UWSGI_DEBUG - uwsgi_log("-- Web3 callable detected --\n"); -#endif - 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.cores); - if (!wi->wsgi_args) { - uwsgi_error("malloc()"); - exit(1); - } - - for(i=0;iwsgi_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 (new_interpreter && id) { - // if we have multiple threads we need to initialize a PyThreadState for each one - if (uwsgi.threads > 1) { - for(i=0;its[id] = PyThreadState_New(wi->interpreter->interp); - if (!uwsgi.workers[uwsgi.mywid].cores[i]->ts[id]) { - uwsgi_log("unable to allocate new PyThreadState structure for app %s", mountpoint); - goto doh; - } - } - PyThreadState_Swap((PyThreadState *) pthread_getspecific(uwsgi.ut_save_key)); - } - else { - PyThreadState_Swap(uwsgi.main_thread); - } - } - - if (wi->argc == 1) { - uwsgi_log( "Web3 application %d (SCRIPT_NAME=%.*s) ready on interpreter %p", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter); - } - else { - uwsgi_log( "WSGI application %d (SCRIPT_NAME=%.*s) ready on interpreter %p", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter); - } - - if (id == 0) { - uwsgi_log(" (default app)"); - uwsgi.default_app = 0; - if (uwsgi.vhost) uwsgi.apps_cnt++; - } - else { - uwsgi.apps_cnt++; - } - - uwsgi_log("\n"); - - return id; - -doh: - free(mountpoint); - PyErr_Print(); - if (new_interpreter && id) { - Py_EndInterpreter(wi->interpreter); - if (uwsgi.threads > 1) { - PyThreadState_Swap((PyThreadState *) pthread_getspecific(uwsgi.ut_save_key)); - } - else { - PyThreadState_Swap(uwsgi.main_thread); - } - } - return -1; -} - -char *get_uwsgi_pymodule(char *module) { - - char *quick_callable; - - if ( (quick_callable = strchr(module, ':')) ) { - quick_callable[0] = 0 ; - quick_callable++; - return quick_callable; - } - - return NULL; -} - -PyObject *get_uwsgi_pydict(char *module) { - - PyObject *wsgi_module, *wsgi_dict; - - wsgi_module = PyImport_ImportModule(module); - if (!wsgi_module) { - PyErr_Print(); - return NULL; - } - - wsgi_dict = PyModule_GetDict(wsgi_module); - if (!wsgi_dict) { - PyErr_Print(); - return NULL; - } - - return wsgi_dict; - -} - -PyObject *uwsgi_uwsgi_loader(void *arg1) { - - PyObject *wsgi_dict; - - char *quick_callable; - - PyObject *tmp_callable; - - char *module = (char *) arg1 ; - - quick_callable = get_uwsgi_pymodule(module) ; - if (quick_callable == NULL) { - if (uwsgi.callable) { - quick_callable = uwsgi.callable ; - } - else { - quick_callable = "application"; - } - } - - wsgi_dict = get_uwsgi_pydict(module); - if (!wsgi_dict) { - return NULL; - } - - // quick callable -> thanks gunicorn for the idea - // we have extended the concept a bit... - if (quick_callable[strlen(quick_callable) -2 ] == '(' && quick_callable[strlen(quick_callable) -1] ==')') { - quick_callable[strlen(quick_callable) -2 ] = 0 ; - tmp_callable = PyDict_GetItemString(wsgi_dict, quick_callable); - if (tmp_callable) { - return python_call(tmp_callable, PyTuple_New(0), 0); - } - } - - return PyDict_GetItemString(wsgi_dict, quick_callable); - -} - -/* this is the mount loader, it loads app on mountpoint automagically */ -PyObject *uwsgi_mount_loader(void *arg1) { - - PyObject *callable = NULL ; - char *what = (char *) arg1; - - if ( !strcmp(what+strlen(what)-3, ".py") || !strcmp(what+strlen(what)-3, ".wsgi")) { - callable = uwsgi_file_loader((void *)what); - } -#ifdef UWSGI_PASTE - else if (!strcmp(what+strlen(what)-4, ".ini")) { - callable = uwsgi_paste_loader((void *)what); - } -#endif - else { - callable = uwsgi_uwsgi_loader((void *)what); - } - - return callable; -} - - -/* this is the dynamic loader, it loads app ireading nformation from a wsgi_request */ -PyObject *uwsgi_dyn_loader(void *arg1) { - - PyObject *callable = NULL ; - char *tmpstr; - - struct wsgi_request *wsgi_req = (struct wsgi_request *) arg1; - - // MANAGE UWSGI_SCRIPT - if (wsgi_req->wsgi_script_len > 0) { - tmpstr = uwsgi_strncopy(wsgi_req->wsgi_script, wsgi_req->wsgi_script_len); - callable = uwsgi_uwsgi_loader((void *)tmpstr); - free(tmpstr); - } - // MANAGE UWSGI_MODULE - else if (wsgi_req->wsgi_module_len > 0) { - if (wsgi_req->wsgi_callable_len > 0) { - tmpstr = uwsgi_concat3n(wsgi_req->wsgi_module, wsgi_req->wsgi_module_len, ":", 1, wsgi_req->wsgi_callable, wsgi_req->wsgi_callable_len); - } - else { - tmpstr = uwsgi_strncopy(wsgi_req->wsgi_module, wsgi_req->wsgi_module_len); - } - callable = uwsgi_uwsgi_loader((void *)tmpstr); - free(tmpstr); - } - // MANAGE UWSGI_FILE - else if (wsgi_req->wsgi_file_len > 0) { - tmpstr = uwsgi_strncopy(wsgi_req->wsgi_file, wsgi_req->wsgi_file_len); - callable = uwsgi_file_loader((void *)tmpstr); - free(tmpstr); - } -#ifdef UWSGI_PASTE - // MANAGE UWSGI_PASTE - else if (wsgi_req->wsgi_paste_len > 0) { - tmpstr = uwsgi_strncopy(wsgi_req->wsgi_paste, wsgi_req->wsgi_paste_len); - callable = uwsgi_paste_loader((void *)tmpstr); - free(tmpstr); - } -#endif - - return callable; -} - - -/* trying to emulate Graham's mod_wsgi, this will allows easy and fast migrations */ -PyObject *uwsgi_file_loader(void *arg1) { - - char *filename = (char *) arg1; - FILE *wsgifile; - struct _node *wsgi_file_node = NULL; - PyObject *wsgi_compiled_node, *wsgi_file_module, *wsgi_file_dict; - PyObject *wsgi_file_callable; - - wsgifile = fopen(filename, "r"); - if (!wsgifile) { - uwsgi_error("fopen()"); - exit(1); - } - - wsgi_file_node = PyParser_SimpleParseFile(wsgifile, filename, Py_file_input); - if (!wsgi_file_node) { - PyErr_Print(); - uwsgi_log( "failed to parse file %s\n", filename); - exit(1); - } - - fclose(wsgifile); - - wsgi_compiled_node = (PyObject *) PyNode_Compile(wsgi_file_node, filename); - - if (!wsgi_compiled_node) { - PyErr_Print(); - uwsgi_log( "failed to compile wsgi file %s\n", filename); - exit(1); - } - - wsgi_file_module = PyImport_ExecCodeModule("uwsgi_wsgi_file", wsgi_compiled_node); - if (!wsgi_file_module) { - PyErr_Print(); - exit(1); - } - - Py_DECREF(wsgi_compiled_node); - - wsgi_file_dict = PyModule_GetDict(wsgi_file_module); - if (!wsgi_file_dict) { - PyErr_Print(); - exit(1); - } - - - wsgi_file_callable = PyDict_GetItemString(wsgi_file_dict, "application"); - if (!wsgi_file_callable) { - PyErr_Print(); - uwsgi_log( "unable to find \"application\" callable in file %s\n", filename); - exit(1); - } - - if (!PyFunction_Check(wsgi_file_callable) && !PyCallable_Check(wsgi_file_callable)) { - uwsgi_log( "\"application\" must be a callable object in file %s\n", filename); - exit(1); - } - - - return wsgi_file_callable; - -} - -#ifdef UWSGI_PASTE -PyObject *uwsgi_paste_loader(void *arg1) { - - char *paste = (char *) arg1; - PyObject *paste_module, *paste_dict, *paste_loadapp; - PyObject *paste_arg, *paste_app; - - uwsgi_log( "Loading paste environment: %s\n", paste); - paste_module = PyImport_ImportModule("paste.deploy"); - if (!paste_module) { - PyErr_Print(); - exit(1); - } - - paste_dict = PyModule_GetDict(paste_module); - if (!paste_dict) { - PyErr_Print(); - exit(1); - } - - paste_loadapp = PyDict_GetItemString(paste_dict, "loadapp"); - if (!paste_loadapp) { - PyErr_Print(); - exit(1); - } - - paste_arg = PyTuple_New(1); - if (!paste_arg) { - PyErr_Print(); - exit(1); - } - - if (PyTuple_SetItem(paste_arg, 0, PyString_FromString(paste))) { - PyErr_Print(); - exit(1); - } - - paste_app = PyEval_CallObject(paste_loadapp, paste_arg); - if (!paste_app) { - PyErr_Print(); - exit(1); - } - - - return paste_app; -} - -#endif - -PyObject *uwsgi_eval_loader(void *arg1) { - - char *code = (char *) arg1; - - PyObject *wsgi_eval_module, *wsgi_eval_callable = NULL; - - struct _node *wsgi_eval_node = NULL; - PyObject *wsgi_compiled_node ; - - wsgi_eval_node = PyParser_SimpleParseString(code, Py_file_input); - if (!wsgi_eval_node) { - PyErr_Print(); - uwsgi_log( "failed to parse code\n"); - exit(1); - } - - wsgi_compiled_node = (PyObject *) PyNode_Compile(wsgi_eval_node, "uwsgi_eval_config"); - - if (!wsgi_compiled_node) { - PyErr_Print(); - uwsgi_log( "failed to compile eval code\n"); - exit(1); - } - - - wsgi_eval_module = PyImport_ExecCodeModule("uwsgi_eval_config", wsgi_compiled_node); - if (!wsgi_eval_module) { - PyErr_Print(); - exit(1); - } - - - Py_DECREF(wsgi_compiled_node); - - uwsgi.pyloader_dict = PyModule_GetDict(wsgi_eval_module); - if (!uwsgi.pyloader_dict) { - PyErr_Print(); - exit(1); - } - - - if (uwsgi.callable) { - wsgi_eval_callable = PyDict_GetItemString(uwsgi.pyloader_dict, uwsgi.callable); - } - else { - wsgi_eval_callable = PyDict_GetItemString(uwsgi.pyloader_dict, "application"); - } - - if (wsgi_eval_callable) { - uwsgi_log( "found the \"%s\" callable\n", uwsgi.callable); - if (!PyFunction_Check(wsgi_eval_callable) && !PyCallable_Check(wsgi_eval_callable)) { - uwsgi_log( "you must define a callable object in your code\n"); - exit(1); - } - } - - return wsgi_eval_callable; - -} - -PyObject *uwsgi_callable_loader(void *arg1) { - return (PyObject *) arg1; -} - -PyObject *uwsgi_string_callable_loader(void *arg1) { - char *callable = (char *) arg1; - - return PyDict_GetItem(uwsgi.pyloader_dict, UWSGI_PYFROMSTRING(callable)); -} diff --git a/pyutils.c b/pyutils.c deleted file mode 100644 index 141afd32..00000000 --- a/pyutils.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "uwsgi.h" - -extern struct uwsgi_server uwsgi; - -int manage_python_response(struct wsgi_request *wsgi_req) { - // use standard WSGI response parse - return uwsgi_response_subhandler_wsgi(wsgi_req); -} - -PyObject *python_call(PyObject *callable, PyObject *args, int catch) { - - PyObject *pyret; - - pyret = PyEval_CallObject(callable, args); - - if (PyErr_Occurred()) { - if (!catch) { - PyErr_Print(); - } - } - -#ifdef UWSGI_DEBUG - if (pyret) { - uwsgi_debug("called %p %p %d\n", callable, args, pyret->ob_refcnt); - } -#endif - - - return pyret; -} - - - -int uwsgi_python_call(struct wsgi_request *wsgi_req, PyObject *callable, PyObject *args) { - - wsgi_req->async_result = python_call(callable, args, 0); - - if (wsgi_req->async_result) { - while ( manage_python_response(wsgi_req) != UWSGI_OK) { -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - return UWSGI_AGAIN; - } -#endif - } - } - - return UWSGI_OK; -} - -void init_pyargv() { - - char *ap; - -#ifdef PYTHREE - wchar_t pname[6]; - mbstowcs(pname, "uwsgi", 6); - uwsgi.py_argv[0] = pname; -#else - uwsgi.py_argv[0] = "uwsgi"; -#endif - - if (uwsgi.pyargv != NULL && !uwsgi.pyargc) { - uwsgi.pyargc++; -#ifdef PYTHREE - wchar_t *wcargv = malloc( sizeof( wchar_t ) * (strlen(uwsgi.pyargv)+1)); - if (!wcargv) { - uwsgi_error("malloc()"); - exit(1); - } - memset(wcargv, 0, sizeof( wchar_t ) * (strlen(uwsgi.pyargv)+1)); -#endif - -#ifdef __sun__ - // FIX THIS !!! - ap = strtok(uwsgi.pyargv, " "); - while ((ap = strtok(NULL, " ")) != NULL) { -#else - while ((ap = strsep(&uwsgi.pyargv, " \t")) != NULL) { -#endif - if (*ap != '\0') { -#ifdef PYTHREE - mbstowcs( wcargv + strlen(ap), ap, strlen(ap)); - uwsgi.py_argv[uwsgi.pyargc] = wcargv + strlen(ap); -#else - uwsgi.py_argv[uwsgi.pyargc] = ap; -#endif - uwsgi.pyargc++; - } - if (uwsgi.pyargc + 1 > MAX_PYARGV) - break; - } - } - - PySys_SetArgv(uwsgi.pyargc, uwsgi.py_argv); -} diff --git a/sendfile.c b/sendfile.c index 190f147d..c80ba0f0 100644 --- a/sendfile.c +++ b/sendfile.c @@ -4,31 +4,6 @@ extern struct uwsgi_server uwsgi; -PyObject *py_uwsgi_sendfile(PyObject * self, PyObject * args) { - - struct wsgi_request *wsgi_req = current_wsgi_req(); - - if (!PyArg_ParseTuple(args, "O|i:uwsgi_sendfile", &wsgi_req->async_sendfile, &wsgi_req->sendfile_fd_chunk)) { - return NULL; - } - -#ifdef PYTHREE - wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile); -#else - if (PyFile_Check((PyObject *)wsgi_req->async_sendfile)) { - Py_INCREF((PyObject *)wsgi_req->async_sendfile); - wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile); - } -#endif - - // PEP 333 hack - wsgi_req->sendfile_obj = wsgi_req->async_sendfile; - //wsgi_req->sendfile_obj = (void *) PyTuple_New(0); - - Py_INCREF((PyObject *) wsgi_req->sendfile_obj); - return (PyObject *) wsgi_req->sendfile_obj; -} - ssize_t uwsgi_sendfile(struct wsgi_request *wsgi_req) { int fd = wsgi_req->sendfile_fd ; @@ -36,7 +11,7 @@ ssize_t uwsgi_sendfile(struct wsgi_request *wsgi_req) { struct stat stat_buf; ssize_t sst = 0; - UWSGI_RELEASE_GIL + //UWSGI_RELEASE_GIL if (!wsgi_req->sendfile_fd_size) { @@ -57,7 +32,7 @@ ssize_t uwsgi_sendfile(struct wsgi_request *wsgi_req) { } end: - UWSGI_GET_GIL + //UWSGI_GET_GIL return sst; } diff --git a/snmp.c b/snmp.c index 2b2d8d52..067f944d 100644 --- a/snmp.c +++ b/snmp.c @@ -369,6 +369,7 @@ static ssize_t build_snmp_response(uint8_t oid1, uint8_t oid2, uint8_t * buffer, } +/* PyObject *py_snmp_counter32(PyObject * self, PyObject * args) { uint8_t oid_num; @@ -484,6 +485,7 @@ void snmp_init() { } +*/ #else #warning "*** SNMP support is disabled ***" diff --git a/utils.c b/utils.c index 96dbb7f1..990ee6b6 100644 --- a/utils.c +++ b/utils.c @@ -350,7 +350,8 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) { } // after_request hook - (*uwsgi.shared->after_hooks[wsgi_req->uh.modifier1]) (wsgi_req); + uwsgi_log("CALL LOG %p\n", uwsgi.shared->hook_after_request[wsgi_req->uh.modifier1]); + uwsgi.shared->hook_after_request[wsgi_req->uh.modifier1](wsgi_req); // leave harakiri mode if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) { @@ -411,7 +412,10 @@ int wsgi_req_recv(struct wsgi_request *wsgi_req) { set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]); } - wsgi_req->async_status = (*uwsgi.shared->hooks[wsgi_req->uh.modifier1]) (wsgi_req); + uwsgi_log("oooops %p\n", uwsgi.shared->hook_request[wsgi_req->uh.modifier1]); + wsgi_req->async_status = uwsgi.shared->hook_request[wsgi_req->uh.modifier1] (wsgi_req); + + uwsgi_log("done\n"); return 0; } @@ -697,6 +701,28 @@ char *uwsgi_concat2(char *one, char *two) { } +char *uwsgi_concat4(char *one, char *two, char *three, char *four) { + + char *buf; + size_t len = strlen(one) + strlen(two) + strlen(three) + strlen(four) + 1; + + + buf = malloc(len); + if (buf == NULL) { + uwsgi_error("malloc()"); + exit(1); + } + buf[len-1] = 0; + + memcpy( buf, one, strlen(one)); + memcpy( buf + strlen(one) , two, strlen(two)); + memcpy( buf + strlen(one) + strlen(two) , three, strlen(three)); + memcpy( buf + strlen(one) + strlen(two) + strlen(three) , four, strlen(four)); + + return buf; + +} + char *uwsgi_concat3(char *one, char *two, char *three) { @@ -822,3 +848,16 @@ int uwsgi_get_app_id(char *script_name, int script_name_len) { return -1; } + +int count_options(struct option *lopt) { + struct option *aopt; + int count = 0; + + while ( (aopt = lopt) ) { + if (!aopt->name) break; + count++; + lopt++; + } + + return count; +} diff --git a/uwsgi.c b/uwsgi.c index 565318f9..4c73572f 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -18,14 +18,6 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -********* Note for Linux users ********* -uWSGI supports UNIX socket on abstract namespace. -Use them if you have filesystem permission problems. - -********* Note for Unbit users ********* -Try to keep the same configuration on your unbit account (threading mode -in particular) - */ @@ -35,7 +27,9 @@ struct uwsgi_server uwsgi; extern char **environ; -static struct option long_options[] = { +static struct option *long_options; + +static struct option long_base_options[] = { {"socket", required_argument, 0, 's'}, {"processes", required_argument, 0, 'p'}, {"harakiri", required_argument, 0, 't'}, @@ -44,7 +38,6 @@ static struct option long_options[] = { #endif {"daemonize", required_argument, 0, 'd'}, {"listen", required_argument, 0, 'l'}, - {"optimize", required_argument, 0, 'O'}, {"max-vars", required_argument, 0, 'v'}, {"buffer-size", required_argument, 0, 'b'}, {"memory-report", no_argument, 0, 'm'}, @@ -60,31 +53,18 @@ static struct option long_options[] = { {"reaper", no_argument, 0, 'r'}, {"max-requests", required_argument, 0, 'R'}, {"socket-timeout", required_argument, 0, 'z'}, - {"module", required_argument, 0, 'w'}, - {"test", required_argument, 0, 'j'}, - {"home", required_argument, 0, 'H'}, {"sharedarea", required_argument, 0, 'A'}, #ifdef UWSGI_SPOOLER {"spooler", required_argument, 0, 'Q'}, #endif {"disable-logging", no_argument, 0, 'L'}, - {"callable", required_argument, 0, LONG_ARGS_CALLABLE}, - {"pidfile", required_argument, 0, LONG_ARGS_PIDFILE}, {"chroot", required_argument, 0, LONG_ARGS_CHROOT}, {"gid", required_argument, 0, LONG_ARGS_GID}, {"uid", required_argument, 0, LONG_ARGS_UID}, - {"pythonpath", required_argument, 0, LONG_ARGS_PYTHONPATH}, - {"python-path", required_argument, 0, LONG_ARGS_PYTHONPATH}, - {"pp", required_argument, 0, LONG_ARGS_PYTHONPATH}, - {"pyargv", required_argument, 0, LONG_ARGS_PYARGV}, #ifdef UWSGI_INI {"ini", required_argument, 0, LONG_ARGS_INI}, - {"ini-paste", required_argument, 0, LONG_ARGS_INI_PASTE}, -#endif -#ifdef UWSGI_PASTE - {"paste", required_argument, 0, LONG_ARGS_PASTE}, #endif #ifdef UWSGI_LDAP {"ldap", required_argument, 0, LONG_ARGS_LDAP}, @@ -99,7 +79,6 @@ static struct option long_options[] = { {"prio", required_argument, 0, LONG_ARGS_PRIO}, {"post-buffering", required_argument, 0, LONG_ARGS_POST_BUFFERING}, {"post-buffering-bufsize", required_argument, 0, LONG_ARGS_POST_BUFFERING_SIZE}, - {"ignore-script-name", no_argument, &uwsgi.ignore_script_name, 1}, {"no-default-app", no_argument, &uwsgi.no_default_app, 1}, #ifdef UWSGI_UDP {"udp", required_argument, 0, LONG_ARGS_UDP}, @@ -126,22 +105,13 @@ static struct option long_options[] = { {"proxy-node", required_argument, 0, LONG_ARGS_PROXY_NODE}, {"proxy-max-connections", required_argument, 0, LONG_ARGS_PROXY_MAX_CONNECTIONS}, #endif - {"wsgi-file", required_argument, 0, LONG_ARGS_WSGI_FILE}, - {"file", required_argument, 0, LONG_ARGS_FILE_CONFIG}, - {"eval", required_argument, 0, LONG_ARGS_EVAL_CONFIG}, #ifdef UWSGI_ASYNC {"async", required_argument, 0, LONG_ARGS_ASYNC}, #endif -#ifdef UWSGI_STACKLESS - {"stackless", no_argument, &uwsgi.stackless, 1}, -#endif #ifdef UWSGI_UGREEN {"ugreen", no_argument, &uwsgi.ugreen, 1}, {"ugreen-stacksize", required_argument, 0, LONG_ARGS_UGREEN_PAGES}, #endif - UWSGI_PLUGIN_LONGOPT_PSGI - UWSGI_PLUGIN_LONGOPT_LUA - UWSGI_PLUGIN_LONGOPT_RACK {"logto", required_argument, 0, LONG_ARGS_LOGTO}, {"logdate", no_argument, &uwsgi.logdate, 1}, {"log-zero", no_argument, 0, LONG_ARGS_LOG_ZERO}, @@ -155,7 +125,6 @@ static struct option long_options[] = { {"mount", required_argument, 0, LONG_ARGS_MOUNT}, {"grunt", no_argument, &uwsgi.grunt, 1}, {"threads", required_argument, 0, LONG_ARGS_THREADS}, - {"no-site", no_argument, &Py_NoSiteFlag, 1}, {"vhost", no_argument, &uwsgi.vhost, 1}, #ifdef UWSGI_ROUTING {"routing", no_argument, &uwsgi.routing, 1}, @@ -167,13 +136,10 @@ static struct option long_options[] = { {"http-var", required_argument, 0, LONG_ARGS_HTTP_VAR}, {"http-modifier1", required_argument, 0, LONG_ARGS_HTTP_MODIFIER1}, #endif - {"catch-exceptions", no_argument, &uwsgi.catch_exceptions, 1}, {"close-on-exec", no_argument, &uwsgi.close_on_exec, 1}, {"mode", required_argument, 0, LONG_ARGS_MODE}, {"env", required_argument, 0, LONG_ARGS_ENV}, {"vacuum", no_argument, &uwsgi.vacuum, 1}, - {"ping", required_argument, 0, LONG_ARGS_PING}, - {"ping-timeout", required_argument, 0, LONG_ARGS_PING_TIMEOUT}, #ifdef __linux__ {"cgroup", required_argument, 0, LONG_ARGS_CGROUP}, {"cgroup-opt", required_argument, 0, LONG_ARGS_CGROUP_OPT}, @@ -182,54 +148,6 @@ static struct option long_options[] = { {0, 0, 0, 0} }; -void uwsgi_pthread_prepare(void) { - pthread_mutex_lock(&uwsgi.lock_pyloaders); -} - -void uwsgi_pthread_parent(void) { - pthread_mutex_unlock(&uwsgi.lock_pyloaders); -} - -void uwsgi_pthread_child(void) { - pthread_mutex_init(&uwsgi.lock_pyloaders, NULL); -} - -void ping() { - - struct uwsgi_header uh; - struct pollfd uwsgi_poll; - - // use a 3 secs timeout by default - if (!uwsgi.ping_timeout) uwsgi.ping_timeout = 3 ; - - uwsgi_poll.fd = uwsgi_connect(uwsgi.ping, uwsgi.ping_timeout); - if (uwsgi_poll.fd < 0) { - exit(1); - } - - uh.modifier1 = UWSGI_MODIFIER_PING; - uh.pktsize = 0; - uh.modifier2 = 0; - if (write(uwsgi_poll.fd, &uh, 4) != 4) { - uwsgi_error("write()"); - exit(2); - } - uwsgi_poll.events = POLLIN; - if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.ping_timeout, &uh, NULL)) { - exit(1); - } - else { - if (uh.pktsize > 0) { - exit(2); - } - else { - exit(0); - } - } - -} - - int find_worker_id(pid_t pid) { int i; for (i = 1; i <= uwsgi.numproc; i++) { @@ -242,10 +160,6 @@ int find_worker_id(pid_t pid) { } -PyMethodDef null_methods[] = { - { NULL, NULL}, -}; - void warn_pipe() { struct wsgi_request *wsgi_req = current_wsgi_req(); @@ -309,19 +223,13 @@ void reap_them_all() { void harakiri() { - PyThreadState *_myself; - - PyGILState_Ensure(); - _myself = PyThreadState_Get(); - - uwsgi_log("\nF*CK !!! i must kill myself (pid: %d app_id: %d) thread_state: %p frame: %p...\n", uwsgi.mypid, uwsgi.wsgi_req->app_id, _myself, _myself->frame); - + uwsgi_log("\nF*CK !!! i must kill myself (pid: %d app_id: %d)...\n", uwsgi.mypid, uwsgi.wsgi_req->app_id); if (!uwsgi.master_process) { uwsgi_log("*** if you want your workers to be automatically respawned consider enabling the uWSGI master process ***\n"); } - Py_FatalError("HARAKIRI !\n"); + exit(0); } void stats() { @@ -356,8 +264,6 @@ void what_i_am_doing() { } -PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} }; -PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} }; pid_t masterpid; struct timeval last_respawn; @@ -436,8 +342,6 @@ int main(int argc, char *argv[], char *envp[]) { struct sockaddr_in *isa; socklen_t socket_type_len; - PyObject *random_module, *random_dict, *random_seed; - #ifdef UWSGI_DEBUG struct utsname uuts; int so_bufsize ; @@ -488,8 +392,8 @@ int main(int argc, char *argv[], char *envp[]) { #endif for (i = 0; i <= 0xFF; i++) { - uwsgi.shared->hooks[i] = unconfigured_hook; - uwsgi.shared->after_hooks[i] = unconfigured_after_hook; + uwsgi.shared->hook_request[i] = unconfigured_hook; + uwsgi.shared->hook_after_request[i] = unconfigured_after_hook; } uwsgi.cores = 1; @@ -543,10 +447,75 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi.binary_path = argv[0]; + // initialize embedded plugins + //LEP(python) + + char *plugins_requested = getenv("UWSGI_PLUGINS"); + if (plugins_requested) { + char *p = strtok(plugins_requested, ","); + while(p != NULL) { + uwsgi_load_plugin(0, p, NULL, 0); + p = strtok(NULL, ","); + } + } + + int opt_count = count_options(long_base_options); + uwsgi_log("count base %d\n", opt_count); + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_options[i]) { + opt_count += count_options(uwsgi.shared->hook_options[i]); + } + } + uwsgi_log("count new base %d\n", opt_count); + + long_options = malloc( sizeof(struct option) * (opt_count+1)); + if (!long_options) { + uwsgi_error("malloc()"); + exit(1); + } + + struct option *lopt, *aopt; + + opt_count = 0; + lopt = long_base_options; + while ((aopt = lopt)) { + if (!aopt->name) break; + long_options[opt_count].name = aopt->name; + long_options[opt_count].has_arg = aopt->has_arg; + long_options[opt_count].flag = aopt->flag; + long_options[opt_count].val = aopt->val; + opt_count++; + lopt++; + } + + for(i=0;i<0xFF;i++) { + lopt = uwsgi.shared->hook_options[i]; + if (!lopt) continue; + + while ((aopt = lopt)) { + if (!aopt->name) break; + long_options[opt_count].name = aopt->name; + long_options[opt_count].has_arg = aopt->has_arg; + long_options[opt_count].flag = aopt->flag; + long_options[opt_count].val = aopt->val; + opt_count++; + lopt++; + } + + } + + long_options[opt_count].name = 0; + long_options[opt_count].has_arg = 0; + long_options[opt_count].flag = 0; + long_options[opt_count].val = 0; + while ((i = getopt_long(argc, argv, "s:p:t:x:d:l:O:v:b:mcaCTiMhrR:z:w:j:H:A:Q:L", long_options, &option_index)) != -1) { manage_opt(i, optarg); } + +/* + if (optind < argc) { char *lazy = argv[optind]; char *qc = strchr(lazy, ':'); @@ -571,12 +540,12 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi.file_config = lazy; } else if (lazy[0] == '/' && strchr(lazy,'=')) { - if (uwsgi.mounts_cnt < MAX_MOUNTPOINTS) { + if (uwsgi.mounts_cnt < MAX_APPS) { uwsgi.mounts[uwsgi.mounts_cnt] = lazy ; uwsgi.mounts_cnt++; } else { - uwsgi_log("unable to add more that %d mountpoints\n", MAX_MOUNTPOINTS); + uwsgi_log("unable to add more that %d mountpoints\n", MAX_APPS); } } else { @@ -585,6 +554,8 @@ int main(int argc, char *argv[], char *envp[]) { } } +*/ + #ifdef UWSGI_XML if (uwsgi.xml_config != NULL) { uwsgi_xml_config(uwsgi.wsgi_req, long_options); @@ -605,9 +576,7 @@ int main(int argc, char *argv[], char *envp[]) { parse_sys_envs(environ, long_options); - if (uwsgi.ping) { - ping(&uwsgi); - } + // call after_opt hooks if (uwsgi.binary_path == argv[0]) { uwsgi.binary_path = malloc(strlen(argv[0]) + 1); @@ -637,12 +606,6 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi_log("*** big endian arch detected ***\n"); #endif -#ifdef PYTHREE - uwsgi_log("*** Warning Python3.x support is experimental, do not use it in production environment ***\n"); -#endif - - uwsgi_log("Python version: %s\n", Py_GetVersion()); - if (uwsgi.pidfile && !uwsgi.is_a_reload) { uwsgi_log( "writing pidfile to %s\n", uwsgi.pidfile); pidfile = fopen(uwsgi.pidfile, "w"); @@ -861,49 +824,24 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi.cores); } - if (uwsgi.pyhome != NULL) { - uwsgi_log("Setting PythonHome to %s...\n", uwsgi.pyhome); -#ifdef PYTHREE - wchar_t *wpyhome; - wpyhome = malloc((sizeof(wchar_t) * strlen(uwsgi.pyhome)) + 2); - if (!wpyhome) { - uwsgi_error("malloc()"); - exit(1); - } - mbstowcs(wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome)); - Py_SetPythonHome(wpyhome); - free(wpyhome); -#else - Py_SetPythonHome(uwsgi.pyhome); -#endif - } - - - - -#ifdef PYTHREE - wchar_t pname[6]; - mbstowcs(pname, "uWSGI", 6); - Py_SetProgramName(pname); -#else - - Py_SetProgramName("uWSGI"); -#endif - - - Py_Initialize(); - - - init_pyargv(); - if (uwsgi.vhost) { uwsgi_log("VirtualHosting mode enabled.\n"); uwsgi.apps_cnt = 0 ; } - uwsgi.wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL); - uwsgi.wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL); + // fill the hook table for embedded plugins + //UWSGI_FILL_HOOK_TABLE_EMBEDDED + // fill the hook table for dynamic plugins + //getenv("UWSGI_PLUGINS"); + + + /* run the plugins initialization routine */ + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_init[i]) { + (*uwsgi.shared->hook_init[i])(); + } + } #ifdef UWSGI_EMBEDDED if (uwsgi.sharedareasize > 0) { @@ -947,17 +885,14 @@ int main(int argc, char *argv[], char *envp[]) { } - init_uwsgi_embedded_module(); #endif - Py_OptimizeFlag = uwsgi.py_optimize; - - uwsgi.main_thread = PyThreadState_Get(); - +/* uwsgi.gil_get = gil_fake_get; uwsgi.gil_release = gil_fake_release; +*/ uwsgi.current_wsgi_req = simple_current_wsgi_req; @@ -976,18 +911,10 @@ int main(int argc, char *argv[], char *envp[]) { #ifdef UWSGI_THREADING if (uwsgi.has_threads) { - PyEval_InitThreads(); - uwsgi_log("threads support enabled\n"); - if (pthread_key_create(&uwsgi.ut_save_key, NULL)) { - uwsgi_error("pthread_key_create()"); - exit(1); - } - pthread_setspecific(uwsgi.ut_save_key, (void *) PyThreadState_Get()); - pthread_mutex_init(&uwsgi.lock_pyloaders, NULL); - pthread_atfork(uwsgi_pthread_prepare, uwsgi_pthread_parent, uwsgi_pthread_child); - uwsgi.gil_get = gil_real_get; - uwsgi.gil_release = gil_real_release; uwsgi.current_wsgi_req = threaded_current_wsgi_req; + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_enable_threads[i]) uwsgi.shared->hook_enable_threads[i](); + } } #endif @@ -1206,14 +1133,8 @@ int main(int argc, char *argv[], char *envp[]) { #endif - init_uwsgi_vars(); - memset(uwsgi.apps, 0, sizeof(uwsgi.apps)); - - - - /* shared area for workers */ uwsgi.workers = (struct uwsgi_worker *) mmap(NULL, sizeof(struct uwsgi_worker) * (uwsgi.numproc + 1 + uwsgi.grunt), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (!uwsgi.workers) { @@ -1251,7 +1172,9 @@ int main(int argc, char *argv[], char *envp[]) { /* save the masterpid */ uwsgi.workers[0].pid = masterpid; - uwsgi_log( "initializing hooks..."); + uwsgi_log( "initializing hooks..." ); + +/* uwsgi.shared->hooks[0] = uwsgi_request_wsgi; uwsgi.shared->after_hooks[0] = uwsgi_after_request_wsgi; @@ -1268,6 +1191,7 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi.shared->hooks[UWSGI_MODIFIER_MESSAGE_MARSHAL] = uwsgi_request_marshal; //33 uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100 +*/ uwsgi_log( "done.\n"); @@ -1322,44 +1246,16 @@ int main(int argc, char *argv[], char *envp[]) { #ifdef UWSGI_SNMP if (uwsgi.snmp) { - snmp_init(); + //snmp_init(); } #endif -// setup app loaders -#ifdef UWSGI_MINTERPRETERS - uwsgi.loaders[LOADER_DYN] = uwsgi_dyn_loader; -#endif - uwsgi.loaders[LOADER_UWSGI] = uwsgi_uwsgi_loader; - uwsgi.loaders[LOADER_FILE] = uwsgi_file_loader; -#ifdef UWSGI_PASTE - uwsgi.loaders[LOADER_PASTE] = uwsgi_paste_loader; -#endif - uwsgi.loaders[LOADER_EVAL] = uwsgi_eval_loader; - uwsgi.loaders[LOADER_MOUNT] = uwsgi_mount_loader; - uwsgi.loaders[LOADER_CALLABLE] = uwsgi_callable_loader; - uwsgi.loaders[LOADER_STRING_CALLABLE] = uwsgi_string_callable_loader; - - - - - // the command line app loaders will be loaded in the main interpreter - // (for performance reason, as this will avoid context switch) - if (uwsgi.wsgi_config != NULL) { - init_uwsgi_app(LOADER_UWSGI, uwsgi.wsgi_config, uwsgi.wsgi_req, 0); - } - - if (uwsgi.file_config != NULL) { - init_uwsgi_app(LOADER_FILE, uwsgi.file_config, uwsgi.wsgi_req, 0); - } -#ifdef UWSGI_PASTE - if (uwsgi.paste != NULL) { - init_uwsgi_app(LOADER_PASTE, uwsgi.paste, uwsgi.wsgi_req, 0); - } -#endif - if (uwsgi.eval != NULL) { - init_uwsgi_app(LOADER_EVAL, uwsgi.eval, uwsgi.wsgi_req, 0); + // init apps hook + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_init_apps[i]) { + uwsgi.shared->hook_init_apps[i](); + } } // parse xml for tags @@ -1376,7 +1272,7 @@ int main(int argc, char *argv[], char *envp[]) { what++; uwsgi.wsgi_req->script_name = uwsgi.mounts[i]; uwsgi.wsgi_req->script_name_len = strlen(uwsgi.mounts[i]); - init_uwsgi_app(LOADER_MOUNT, what, uwsgi.wsgi_req, 0); + //init_uwsgi_app(LOADER_MOUNT, what, uwsgi.wsgi_req, 0); } else { uwsgi_log("invalid mountpoint: %s\n", uwsgi.mounts[i]); @@ -1385,14 +1281,6 @@ int main(int argc, char *argv[], char *envp[]) { } - if (uwsgi.test_module != NULL) { - if (PyImport_ImportModule(uwsgi.test_module)) { - exit(0); - } - exit(1); - } - - if (uwsgi.no_server) { uwsgi_log( "no-server mode requested. Goodbye.\n"); exit(0); @@ -1486,38 +1374,12 @@ int main(int argc, char *argv[], char *envp[]) { // from now on the process is a real worker } - // reinitialize the random seed (thanks Jonas Borgström) - random_module = PyImport_ImportModule("random"); - if (random_module) { - random_dict = PyModule_GetDict(random_module); - if (random_dict) { - random_seed = PyDict_GetItemString(random_dict, "seed"); - if (random_seed) { - PyObject *random_args = PyTuple_New(1); - // pass no args - PyTuple_SetItem(random_args, 0, Py_None); - PyEval_CallObject( random_seed, random_args ); - if (PyErr_Occurred()) { - PyErr_Print(); - } - } + for(i=0;i<0xFF;i++) { + if (uwsgi.shared->hook_post_fork[i]) { + uwsgi.shared->hook_post_fork[i](); } } -#ifdef UWSGI_EMBEDDED - // call the post_fork_hook - PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); - if (uwsgi_dict) { - PyObject *pfh = PyDict_GetItemString(uwsgi_dict, "post_fork_hook"); - if (pfh) { - python_call(pfh, PyTuple_New(0), 0); - } - } - PyErr_Clear(); -#endif - - - // postpone the queue initialization as kevent do not pass kfd after fork() #ifdef UWSGI_ASYNC if (uwsgi.async > 1) { @@ -1594,7 +1456,7 @@ int main(int argc, char *argv[], char *envp[]) { #endif // release the GIL - UWSGI_RELEASE_GIL + //UWSGI_RELEASE_GIL #ifdef UWSGI_ASYNC @@ -1637,7 +1499,7 @@ int main(int argc, char *argv[], char *envp[]) { exit(1); } - if (pthread_key_create(&uwsgi.ut_key, NULL)) { + if (pthread_key_create(&uwsgi.tur_key, NULL)) { uwsgi_error("pthread_key_create()"); exit(1); } @@ -1667,352 +1529,6 @@ int main(int argc, char *argv[], char *envp[]) { return 0; } -void init_uwsgi_vars() { - - int i; - PyObject *pysys, *pysys_dict, *pypath; - -#ifdef UWSGI_MINTERPRETERS - char venv_version[15] ; - PyObject *site_module; -#endif - - /* add cwd to pythonpath */ - pysys = PyImport_ImportModule("sys"); - if (!pysys) { - PyErr_Print(); - exit(1); - } - pysys_dict = PyModule_GetDict(pysys); - pypath = PyDict_GetItemString(pysys_dict, "path"); - if (!pypath) { - PyErr_Print(); - exit(1); - } - -#ifdef UWSGI_MINTERPRETERS - // simulate a pythonhome directive - if (uwsgi.wsgi_req->pyhome_len > 0) { - - PyObject *venv_path = UWSGI_PYFROMSTRINGSIZE(uwsgi.wsgi_req->pyhome, uwsgi.wsgi_req->pyhome_len) ; - -#ifdef UWSGI_DEBUG - uwsgi_debug("setting dynamic virtualenv to %.*s\n", uwsgi.wsgi_req->pyhome_len, uwsgi.wsgi_req->pyhome); -#endif - - PyDict_SetItemString(pysys_dict, "prefix", venv_path); - PyDict_SetItemString(pysys_dict, "exec_prefix", venv_path); - - venv_version[14] = 0 ; - if (snprintf(venv_version, 15, "/lib/python%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION) == -1) { - return ; - } - - // check here - PyString_Concat( &venv_path, PyString_FromString(venv_version) ); - - if ( PyList_Insert(pypath, 0, venv_path) ) { - PyErr_Print(); - } - - site_module = PyImport_ImportModule("site"); - if (site_module) { - PyImport_ReloadModule(site_module); - } - - } -#endif - - if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(".") ) != 0) { - PyErr_Print(); - } - - for (i = 0; i < uwsgi.python_path_cnt; i++) { - if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(uwsgi.python_path[i]) ) != 0) { - PyErr_Print(); - } - else { - uwsgi_log( "added %s to pythonpath.\n", uwsgi.python_path[i]); - } - } - -} - -void uwsgi_uwsgi_config(char *module) { - -#ifdef UWSGI_EMBEDDED - PyObject *uwsgi_module, *uwsgi_dict; -#endif - PyObject *applications; - PyObject *app_list; - Py_ssize_t i; - PyObject *app_mnt, *app_app = NULL; - char *quick_callable ; - - quick_callable = get_uwsgi_pymodule(module); - if (quick_callable == NULL) { - if (uwsgi.callable) { - quick_callable = uwsgi.callable ; - } - else { - quick_callable = "application"; - } - } - - uwsgi.pyloader_dict = get_uwsgi_pydict(module); - if (!uwsgi.pyloader_dict) { - exit(1); - } - - uwsgi_log( "...getting the applications list from the '%s' module...\n", module); - -#ifdef UWSGI_EMBEDDED - uwsgi_module = PyImport_ImportModule("uwsgi"); - if (!uwsgi_module) { - PyErr_Print(); - exit(1); - } - - uwsgi_dict = PyModule_GetDict(uwsgi_module); - if (!uwsgi_dict) { - PyErr_Print(); - exit(1); - } - - applications = PyDict_GetItemString(uwsgi_dict, "applications"); - if (!PyDict_Check(applications)) { - uwsgi_log( "uwsgi.applications dictionary is not defined, trying with the \"applications\" one...\n"); -#endif - applications = PyDict_GetItemString(uwsgi.pyloader_dict, "applications"); - if (!applications) { - uwsgi_log( "applications dictionary is not defined, trying with the \"application\" callable.\n"); - quick_callable = uwsgi_concat3(module, ":", quick_callable); - if (init_uwsgi_app(LOADER_UWSGI, (void *) quick_callable, uwsgi.wsgi_req, 0) < 0) { - uwsgi_log( "...goodbye cruel world...\n"); - exit(1); - } - free(quick_callable); - return; - } -#ifdef UWSGI_EMBEDDED - } -#endif - - if (!PyDict_Check(applications)) { - uwsgi_log( "The 'applications' object must be a dictionary.\n"); - exit(1); - } - - app_list = PyDict_Keys(applications); - if (!app_list) { - PyErr_Print(); - exit(1); - } - if (PyList_Size(app_list) < 1) { - uwsgi_log( "You must define an app.\n"); - exit(1); - } - - for (i = 0; i < PyList_Size(app_list); i++) { - app_mnt = PyList_GetItem(app_list, i); - - if (!PyString_Check(app_mnt)) { - uwsgi_log( "the app mountpoint must be a bytestring.\n"); - exit(1); - } - - - uwsgi.wsgi_req->script_name = PyString_AsString(app_mnt); - uwsgi.wsgi_req->script_name_len = strlen(uwsgi.wsgi_req->script_name); - - app_app = PyDict_GetItem(applications, app_mnt); - - if (!PyString_Check(app_app) && !PyFunction_Check(app_app) && !PyCallable_Check(app_app)) { - uwsgi_log( "the app callable must be a string, a function or a callable. (found %s)\n", app_app->ob_type->tp_name); - exit(1); - } - -#ifdef PYTHREE - if (PyUnicode_Check(app_app)) { -#else - if (PyString_Check(app_app)) { -#endif - if (init_uwsgi_app(LOADER_STRING_CALLABLE, (void *) PyString_AsString(app_app), uwsgi.wsgi_req, 0) < 0) { - uwsgi_log( "...goodbye cruel world...\n"); - exit(1); - } - - - } - else { - if (init_uwsgi_app(LOADER_CALLABLE, (void *) app_app, uwsgi.wsgi_req, 0) < 0) { - uwsgi_log( "...goodbye cruel world...\n"); - exit(1); - } - } - - Py_DECREF(app_mnt); - Py_DECREF(app_app); - } - -} - - -#ifdef PYTHREE -static PyModuleDef uwsgi_module3 = { - PyModuleDef_HEAD_INIT, - "uwsgi", - NULL, - -1, - null_methods, -}; -PyObject *init_uwsgi3(void) { - return PyModule_Create(&uwsgi_module3); -} -#endif - -#ifdef UWSGI_EMBEDDED -void init_uwsgi_embedded_module() { - PyObject *new_uwsgi_module, *zero; - int i; - - /* initialize for stats */ - uwsgi.workers_tuple = PyTuple_New(uwsgi.numproc); - for (i = 0; i < uwsgi.numproc; i++) { - zero = PyDict_New(); - Py_INCREF(zero); - PyTuple_SetItem(uwsgi.workers_tuple, i, zero); - } - - - -#ifdef PYTHREE - PyImport_AppendInittab("uwsgi", init_uwsgi3); - new_uwsgi_module = PyImport_AddModule("uwsgi"); -#else - new_uwsgi_module = Py_InitModule("uwsgi", null_methods); -#endif - if (new_uwsgi_module == NULL) { - uwsgi_log( "could not initialize the uwsgi python module\n"); - exit(1); - } - - uwsgi.embedded_dict = PyModule_GetDict(new_uwsgi_module); - if (!uwsgi.embedded_dict) { - uwsgi_log( "could not get uwsgi module __dict__\n"); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "version", PyString_FromString(UWSGI_VERSION))) { - PyErr_Print(); - exit(1); - } - - if (uwsgi.mode) { - if (PyDict_SetItemString(uwsgi.embedded_dict, "mode", PyString_FromString(uwsgi.mode))) { - PyErr_Print(); - exit(1); - } - } - - if (uwsgi.pidfile) { - if (PyDict_SetItemString(uwsgi.embedded_dict, "pidfile", PyString_FromString(uwsgi.pidfile))) { - PyErr_Print(); - exit(1); - } - } - - - if (PyDict_SetItemString(uwsgi.embedded_dict, "SPOOL_RETRY", PyInt_FromLong(17))) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "numproc", PyInt_FromLong(uwsgi.numproc))) { - PyErr_Print(); - exit(1); - } - -#ifdef UNBIT - if (PyDict_SetItemString(uwsgi.embedded_dict, "unbit", Py_True)) { -#else - if (PyDict_SetItemString(uwsgi.embedded_dict, "unbit", Py_None)) { -#endif - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "buffer_size", PyInt_FromLong(uwsgi.buffer_size))) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "started_on", PyInt_FromLong(uwsgi.start_tv.tv_sec))) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "start_response", uwsgi.wsgi_spitout)) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "fastfuncs", PyList_New(256))) { - PyErr_Print(); - exit(1); - } - - - if (PyDict_SetItemString(uwsgi.embedded_dict, "applications", Py_None)) { - PyErr_Print(); - exit(1); - } - - if (uwsgi.is_a_reload) { - if (PyDict_SetItemString(uwsgi.embedded_dict, "is_a_reload", Py_True)) { - PyErr_Print(); - exit(1); - } - } - else { - if (PyDict_SetItemString(uwsgi.embedded_dict, "is_a_reload", Py_False)) { - PyErr_Print(); - exit(1); - } - } - - uwsgi.embedded_args = PyTuple_New(2); - if (!uwsgi.embedded_args) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(uwsgi.embedded_dict, "message_manager_marshal", Py_None)) { - PyErr_Print(); - exit(1); - } - - uwsgi.fastfuncslist = PyDict_GetItemString(uwsgi.embedded_dict, "fastfuncs"); - if (!uwsgi.fastfuncslist) { - PyErr_Print(); - exit(1); - } - - init_uwsgi_module_advanced(new_uwsgi_module); - -#ifdef UWSGI_SPOOLER - if (uwsgi.spool_dir != NULL) { - init_uwsgi_module_spooler(new_uwsgi_module); - } -#endif - - - if (uwsgi.sharedareasize > 0 && uwsgi.sharedarea) { - init_uwsgi_module_sharedarea(new_uwsgi_module); - } -} -#endif - #ifdef UWSGI_PROXY pid_t proxy_start(int has_master) { @@ -2058,7 +1574,8 @@ pid_t proxy_start(int has_master) { } #endif -void manage_opt(int i, char *optarg) { +static int manage_base_opt(int i, char *optarg) { + switch (i) { @@ -2067,60 +1584,57 @@ void manage_opt(int i, char *optarg) { uwsgi_error("chdir()"); exit(1); } - break; + return 1; case LONG_ARGS_CHDIR2: uwsgi.chdir2 = optarg; - break; + return 1; case LONG_ARGS_PING: uwsgi.ping = optarg; - break; + return 1; case LONG_ARGS_PING_TIMEOUT: uwsgi.ping_timeout = atoi(optarg); - break; - case LONG_ARGS_CALLABLE: - uwsgi.callable = optarg; - break; + return 1; #ifdef UWSGI_HTTP case LONG_ARGS_HTTP: uwsgi.http = optarg; - break; + return 1; #endif #ifdef UWSGI_LDAP case LONG_ARGS_LDAP: uwsgi.ldap = optarg; - break; + return 1; case LONG_ARGS_LDAP_SCHEMA: uwsgi_ldap_schema_dump(long_options); - break; + return 1; case LONG_ARGS_LDAP_SCHEMA_LDIF: uwsgi_ldap_schema_dump_ldif(long_options); - break; + return 1; #endif case LONG_ARGS_MODE: uwsgi.mode = optarg; - break; + return 1; case LONG_ARGS_ENV: if (putenv(optarg)) { uwsgi_error("putenv()"); } - break; + return 1; #ifdef UWSGI_THREADING case LONG_ARGS_THREADS: uwsgi.threads = atoi(optarg); - break; + return 1; #endif #ifdef UWSGI_ASYNC case LONG_ARGS_ASYNC: uwsgi.async = atoi(optarg); - break; + return 1; #endif case LONG_ARGS_LOGTO: logto(optarg); - break; + return 1; #ifdef UWSGI_UGREEN case LONG_ARGS_UGREEN_PAGES: uwsgi.ugreen_stackpages = atoi(optarg); - break; + return 1; #endif case LONG_ARGS_VERSION: fprintf(stdout, "uWSGI %s\n", UWSGI_VERSION); @@ -2128,30 +1642,30 @@ void manage_opt(int i, char *optarg) { #ifdef UWSGI_SNMP case LONG_ARGS_SNMP: uwsgi.snmp = 1; - break; + return 1; case LONG_ARGS_SNMP_COMMUNITY: uwsgi.snmp = 1; uwsgi.snmp_community = optarg; - break; + return 1; #endif case LONG_ARGS_PIDFILE: uwsgi.pidfile = optarg; - break; + return 1; #ifdef UWSGI_UDP case LONG_ARGS_UDP: uwsgi.udp_socket = optarg; uwsgi.master_process = 1; - break; + return 1; #endif #ifdef UWSGI_MULTICAST case LONG_ARGS_MULTICAST: uwsgi.multicast_group = optarg; uwsgi.master_process = 1; - break; + return 1; #endif case LONG_ARGS_CHROOT: uwsgi.chroot = optarg; - break; + return 1; case LONG_ARGS_GID: uwsgi.gid = atoi(optarg); if (!uwsgi.gid) { @@ -2164,7 +1678,7 @@ void manage_opt(int i, char *optarg) { exit(1); } } - break; + return 1; case LONG_ARGS_UID: uwsgi.uid = atoi(optarg); if (!uwsgi.uid) { @@ -2177,29 +1691,29 @@ void manage_opt(int i, char *optarg) { exit(1); } } - break; + return 1; case LONG_ARGS_BINARY_PATH: uwsgi.binary_path = optarg; - break; + return 1; case LONG_ARGS_WSGI_FILE: case LONG_ARGS_FILE_CONFIG: uwsgi.file_config = optarg; - break; + return 1; #ifdef UWSGI_PROXY case LONG_ARGS_PROXY_NODE: uwsgi_cluster_add_node(optarg, 1); - break; + return 1; case LONG_ARGS_PROXY: uwsgi.proxy_socket_name = optarg; - break; + return 1; #endif #ifdef UWSGI_ERLANG case LONG_ARGS_ERLANG: uwsgi.erlang_node = optarg; - break; + return 1; case LONG_ARGS_ERLANG_COOKIE: uwsgi.erlang_cookie = optarg; - break; + return 1; #endif #ifdef UWSGI_HTTP case LONG_ARGS_HTTP_VAR: @@ -2210,15 +1724,15 @@ void manage_opt(int i, char *optarg) { else { uwsgi_log( "you can specify at most 64 --http-var options\n"); } - break; + return 1; case LONG_ARGS_HTTP_MODIFIER1: uwsgi.http_modifier1 = (uint8_t) atoi(optarg); - break; + return 1; #endif #ifdef __linux__ case LONG_ARGS_CGROUP: uwsgi.cgroup = optarg; - break; + return 1; case LONG_ARGS_CGROUP_OPT: if (uwsgi.cgroup_opt_cnt < 63) { uwsgi.cgroup_opt[uwsgi.cgroup_opt_cnt] = optarg; @@ -2227,40 +1741,31 @@ void manage_opt(int i, char *optarg) { else { uwsgi_log( "you can specify at most 64 --cgroup_opt options\n"); } - break; + return 1; #endif - case LONG_ARGS_PYTHONPATH: - if (uwsgi.python_path_cnt < MAX_PYTHONPATH) { - uwsgi.python_path[uwsgi.python_path_cnt] = optarg; - uwsgi.python_path_cnt++; - } - else { - uwsgi_log( "you can specify at most %d --pythonpath options\n", MAX_PYTHONPATH); - } - break; case LONG_ARGS_LIMIT_AS: uwsgi.rl.rlim_cur = (atoi(optarg)) * 1024 * 1024; uwsgi.rl.rlim_max = uwsgi.rl.rlim_cur; - break; + return 1; case LONG_ARGS_LIMIT_POST: uwsgi.limit_post = (int) strtol(optarg, NULL, 10); - break; + return 1; case LONG_ARGS_PRIO: uwsgi.prio = (int) strtol(optarg, NULL, 10); - break; + return 1; case LONG_ARGS_POST_BUFFERING: uwsgi.post_buffering = atoi(optarg); - break; + return 1; case LONG_ARGS_POST_BUFFERING_SIZE: uwsgi.post_buffering_bufsize = atoi(optarg); - break; + return 1; #ifdef UWSGI_INI case LONG_ARGS_INI: uwsgi.ini = optarg; - break; + return 1; case LONG_ARGS_EVAL_CONFIG: uwsgi.eval = optarg; - break; + return 1; #ifdef UWSGI_PASTE case LONG_ARGS_INI_PASTE: uwsgi.ini = optarg; @@ -2286,59 +1791,50 @@ void manage_opt(int i, char *optarg) { memcpy(uwsgi.paste, "config:", 7); memcpy(uwsgi.paste + 7, uwsgi.ini, strlen(uwsgi.ini)); } - break; + return 1; #endif #endif #ifdef UWSGI_PASTE case LONG_ARGS_PASTE: uwsgi.paste = optarg; - break; + return 1; #endif case LONG_ARGS_CHECK_INTERVAL: uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL] = atoi(optarg); - break; - case LONG_ARGS_PYARGV: - uwsgi.pyargv = optarg; - break; - case 'j': - uwsgi.test_module = optarg; - break; - case 'H': - uwsgi.pyhome = optarg; - break; + return 1; case 'A': uwsgi.sharedareasize = atoi(optarg); - break; + return 1; case 'L': uwsgi.shared->options[UWSGI_OPTION_LOGGING] = 0; - break; + return 1; case LONG_ARGS_LOG_ZERO: uwsgi.shared->options[UWSGI_OPTION_LOG_ZERO] = 1; - break; + return 1; case LONG_ARGS_LOG_SLOW: uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW] = atoi(optarg); - break; + return 1; case LONG_ARGS_LOG_4xx: uwsgi.shared->options[UWSGI_OPTION_LOG_4xx] = 1; - break; + return 1; case LONG_ARGS_LOG_5xx: uwsgi.shared->options[UWSGI_OPTION_LOG_5xx] = 1; - break; + return 1; case LONG_ARGS_LOG_BIG: uwsgi.shared->options[UWSGI_OPTION_LOG_BIG] = atoi(optarg); - break; + return 1; case LONG_ARGS_LOG_SENDFILE: uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE] = 1; - break; + return 1; case LONG_ARGS_MOUNT: - if (uwsgi.mounts_cnt < MAX_MOUNTPOINTS) { + if (uwsgi.mounts_cnt < MAX_APPS) { uwsgi.mounts[uwsgi.mounts_cnt] = optarg; uwsgi.mounts_cnt++; } else { - uwsgi_log( "you can specify at most %d --mount options\n", MAX_MOUNTPOINTS); + uwsgi_log( "you can specify at most %d --mount options\n", MAX_APPS); } - break; + return 1; #ifdef UWSGI_SPOOLER case 'Q': uwsgi.spool_dir = malloc(PATH_MAX); @@ -2355,14 +1851,14 @@ void manage_opt(int i, char *optarg) { exit(1); } uwsgi.master_process = 1; - break; + return 1; #endif case 'd': if (!uwsgi.is_a_reload) { daemonize(optarg); } - break; + return 1; case 's': if (uwsgi.sockets_cnt < 8) { uwsgi.sockets[uwsgi.sockets_cnt].name = optarg; @@ -2371,46 +1867,43 @@ void manage_opt(int i, char *optarg) { else { uwsgi_log( "you can specify at most 8 --socket options\n"); } - break; + return 1; #ifdef UWSGI_XML case 'x': uwsgi.xml_config = optarg; - break; + return 1; #endif case 'l': uwsgi.listen_queue = atoi(optarg); - break; + return 1; case 'v': uwsgi.max_vars = atoi(optarg); uwsgi.vec_size = 4 + 1 + (4 * uwsgi.max_vars); - break; + return 1; case 'p': uwsgi.numproc = atoi(optarg); - break; + return 1; case 'r': uwsgi.shared->options[UWSGI_OPTION_REAPER] = 1; - break; - case 'w': - uwsgi.wsgi_config = optarg; - break; + return 1; case 'm': uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] = 1; - break; + return 1; case 'O': uwsgi.py_optimize = atoi(optarg); - break; + return 1; case 't': uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] = atoi(optarg); - break; + return 1; case 'b': uwsgi.buffer_size = atoi(optarg); - break; + return 1; case 'c': uwsgi.shared->options[UWSGI_OPTION_CGI_MODE] = 1; - break; + return 1; case 'a': uwsgi.abstract_socket = 1; - break; + return 1; case 'C': uwsgi.chmod_socket = 1; if (optarg) { @@ -2429,28 +1922,25 @@ void manage_opt(int i, char *optarg) { uwsgi.chmod_socket_value = (uwsgi.chmod_socket_value << 3) + (optarg[1] - '0'); uwsgi.chmod_socket_value = (uwsgi.chmod_socket_value << 3) + (optarg[2] - '0'); } - break; + return 1; case 'M': uwsgi.master_process = 1; - break; + return 1; case 'R': uwsgi.shared->options[UWSGI_OPTION_MAX_REQUESTS] = atoi(optarg); - break; + return 1; case 'z': if (atoi(optarg) > 0) { uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = atoi(optarg); } - break; + return 1; case 'T': uwsgi.has_threads = 1; uwsgi.shared->options[UWSGI_OPTION_THREADS] = 1; - break; + return 1; case 'i': uwsgi.single_interpreter = 1; - break; - LONG_ARGS_PLUGIN_EMBED_PSGI - LONG_ARGS_PLUGIN_EMBED_LUA - LONG_ARGS_PLUGIN_EMBED_RACK + return 1; case 'h': fprintf(stdout, "Usage: %s [options...]\n\ \t-s|--socket \t\tpath (or name) of UNIX/TCP socket to bind to\n\ @@ -2550,17 +2040,33 @@ void manage_opt(int i, char *optarg) { \t--cgroup-opt KEY=VAL\t\tset cgroup option (Linux only)\n\ \t--version\t\t\tprint server version\n\ \t-d|--daemonize \tdaemonize and log into or udp \n", uwsgi.binary_path); - exit(1); - case 0: - break; - default: - if (i != '?') { - uwsgi_log( "invalid argument -%c exiting \n", i); - } - exit(1); + return 0; } + + return 0; } +void manage_opt(int i, char *optarg) { + + int j; + + if (manage_base_opt(i, optarg)) { + return; + } + + for(j=0;j<0xFF;j++) { + if (uwsgi.shared->hook_manage_opt[j]) { + uwsgi_log("MANAGE_OPT\n"); + if (uwsgi.shared->hook_manage_opt[j](i, optarg)) { + return; + } + } + } + + // never here + exit(1); + +} void uwsgi_cluster_add_node(char *nodename, int workers) { diff --git a/uwsgi.h b/uwsgi.h index cf5f4c89..86a5e10e 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -10,16 +10,25 @@ #define wsgi_req_time ((wsgi_req->end_of_request.tv_sec * 1000000 + wsgi_req->end_of_request.tv_usec) - (wsgi_req->start_of_request.tv_sec * 1000000 + wsgi_req->start_of_request.tv_usec))/1000 #define MAX_APPS 64 -#define MAX_MOUNTPOINTS 64 -#define MAX_PYTHONPATH 64 -#ifdef UWSGI_THREADING -#define UWSGI_GET_GIL (*uwsgi.gil_get)(); -#define UWSGI_RELEASE_GIL (*uwsgi.gil_release)(); -#else -#define UWSGI_GET_GIL -#define UWSGI_RELEASE_GIL -#endif +#define LEP(lepname) \ + extern struct uwsgi_plugin lepname##_plugin;\ + fill_plugin_table(lepup->modifier1, lepname); + +#define fill_plugin_table(x, up)\ + uwsgi.shared->hook_init[x] = up->init;\ + uwsgi.shared->hook_post_fork[x] = up->post_fork;\ + uwsgi.shared->hook_options[x] = up->options;\ + uwsgi.shared->hook_manage_opt[x] = up->manage_opt;\ + uwsgi.shared->hook_request[x] = up->request;\ + uwsgi.shared->hook_after_request[x] = up->after_request;\ + uwsgi.shared->hook_init_apps[x] = up->init_apps;\ + uwsgi.shared->hook_enable_threads[x] = up->enable_threads;\ + uwsgi.shared->hook_init_thread[x] = up->init_thread;\ + uwsgi.shared->hook_manage_udp[x] = up->manage_udp;\ + + + #include #include @@ -30,6 +39,10 @@ #include #include #include +#include +#include +#include + #include @@ -128,19 +141,6 @@ #endif #undef _XOPEN_SOURCE -#include - -#ifdef UWSGI_STACKLESS -#include -#endif - -#if PY_MINOR_VERSION == 4 && PY_MAJOR_VERSION == 2 -#define Py_ssize_t ssize_t -#endif - -#if PY_MAJOR_VERSION > 2 -#define PYTHREE -#endif /* this value are taken from nginx */ #if defined(__APPLE__) || defined(__freebsd__) @@ -149,8 +149,6 @@ #define UWSGI_LISTEN_QUEUE 511 #endif -PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *, int); -PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t); #define MAX_CLUSTER_NODES 100 @@ -287,26 +285,6 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t); #define UWSGI_MODIFIER_RESPONSE 255 -#ifdef PYTHREE -#define UWSGI_PYFROMSTRING(x) PyUnicode_FromString(x) -#define UWSGI_PYFROMSTRINGSIZE(x, y) PyUnicode_FromStringAndSize(x, y) -#define PyInt_FromLong PyLong_FromLong -#define PyInt_AsLong PyLong_AsLong -#define PyInt_Check PyLong_Check -#define PyString_Check PyBytes_Check -#define PyString_FromStringAndSize PyBytes_FromStringAndSize -#define PyString_FromFormat PyBytes_FromFormat -#define PyString_FromString PyBytes_FromString -#define PyString_Size PyBytes_Size -#define PyString_Concat PyBytes_Concat -#define PyString_AsString (char *) PyBytes_AsString -#define PyFile_FromFile(A,B,C,D) PyFile_FromFd(fileno((A)), (B), (C), -1, NULL, NULL, NULL, 0) - -#else -#define UWSGI_PYFROMSTRING(x) PyString_FromString(x) -#define UWSGI_PYFROMSTRINGSIZE(x, y) PyString_FromStringAndSize(x, y) -#endif - #define NL_SIZE 2 #define H_SEP_SIZE 2 @@ -328,30 +306,30 @@ struct uwsgi_server; struct uwsgi_app { + uint8_t modifier1; + char *mountpoint; int mountpoint_len; - PyThreadState *interpreter; - PyObject *pymain_dict; + void *interpreter; + //PyObject *pymain_dict; //PyObject *wsgi_dict; - PyObject *wsgi_callable; + void *callable; #ifdef UWSGI_ASYNC - PyObject **wsgi_args; - PyObject **wsgi_environ; + void **args; + void **environ; #else - PyObject *wsgi_args; - PyObject *wsgi_environ; + void *args; + void *environ; #endif - PyObject *wsgi_harakiri; - PyObject *wsgi_sendfile; - PyObject *wsgi_cprofile_run; + void *sendfile; #ifdef UWSGI_ASYNC - PyObject *wsgi_eventfd_read; - PyObject *wsgi_eventfd_write; + void *eventfd_read; + void *eventfd_write; #endif void* (*request_subhandler)(struct wsgi_request *, struct uwsgi_app *); @@ -497,10 +475,6 @@ struct wsgi_request { char *post_buffering_buf; uint64_t post_buffering_read; -#ifdef UWSGI_STACKLESS - PyTaskletObject* tasklet; -#endif - char *buffer; }; @@ -528,7 +502,6 @@ struct uwsgi_server { struct wsgi_request **wsgi_requests ; struct wsgi_request *wsgi_req ; - PyThreadState *_save; char *chroot; gid_t gid; uid_t uid; @@ -673,11 +646,9 @@ struct uwsgi_server { char *file_config; - char *python_path[MAX_PYTHONPATH]; + //char *python_path[MAX_PYTHONPATH]; int python_path_cnt; - char *pyargv; - int pyargc; #ifdef PYTHREE wchar_t *py_argv[MAX_PYARGV]; #else @@ -705,23 +676,6 @@ struct uwsgi_server { int single_interpreter; int py_optimize; - PyObject *embedded_dict; - PyObject *embedded_args; - PyObject *fastfuncslist; - - PyObject *wsgi_writeout; - -#ifdef UWSGI_STACKLESS - PyObject *wsgi_stackless; - PyChannelObject *workers_channel; - struct stackless_req **stackless_table; -#endif - - PyObject *workers_tuple; - - PyThreadState *main_thread; - - struct uwsgi_shared *shared; struct uwsgi_app apps[MAX_APPS]; @@ -733,23 +687,6 @@ struct uwsgi_server { int no_orphans; -#ifdef UWSGI_EMBED_PLUGINS - -#ifdef UWSGI_EMBED_PLUGIN_PSGI - char *plugin_arg_psgi; -#endif - -#ifdef UWSGI_EMBED_PLUGIN_LUA - char *plugin_arg_lua; -#endif - -#ifdef UWSGI_EMBED_PLUGIN_RACK - char *plugin_arg_rack; - char *plugin_arg_rails; -#endif - -#endif - char *chdir2; int catch_exceptions; @@ -763,8 +700,6 @@ struct uwsgi_server { char *ping; int ping_timeout; - char *callable; - int xml_round2; char *cwd; @@ -782,39 +717,26 @@ struct uwsgi_server { int cgroup_opt_cnt; #endif - PyObject *wsgi_spitout; - int sockets_cnt; struct uwsgi_socket sockets[8]; struct pollfd sockets_poll[8]; time_t respawn_delta; - PyObject *pyloader_dict; - - PyObject* (*loaders[LOADER_MAX]) (void *); - - char *mounts[MAX_MOUNTPOINTS]; + char *mounts[MAX_APPS]; int mounts_cnt; int cores; int threads; - pthread_key_t ut_key; - pthread_key_t ut_save_key; + + // this key old the u_request structure per core/thread + pthread_key_t tur_key; -#ifdef UWSGI_THREADING - void (*gil_get) (void); - void (*gil_release) (void); -#endif - struct wsgi_request* (*current_wsgi_req)(void); int close_on_exec; -#ifdef UWSGI_THREADING - pthread_mutex_t lock_pyloaders; -#endif }; struct uwsgi_cluster_node { @@ -847,8 +769,16 @@ struct uwsgi_shared { // vga 80x25 specific ! char warning_message[81]; - int (*hooks[256]) (struct wsgi_request *); - void (*after_hooks[256]) (struct wsgi_request *); + int (*hook_init[0xFF]) (void); + void (*hook_post_fork[0xFF]) (void); + void (*hook_enable_threads[0xFF]) (void); + int (*hook_request[0xFF]) (struct wsgi_request *); + void (*hook_after_request[0xFF]) (struct wsgi_request *); + void (*hook_init_thread[0xFF]) (void); + void (*hook_init_apps[0xFF]) (void); + struct option *hook_options[0xFF]; + int (*hook_manage_opt[0xFF]) (int, char*); + int (*hook_manage_udp[0xFF]) (char *, int, char*, int); uint32_t options[256]; struct uwsgi_cluster_node nodes[MAX_CLUSTER_NODES]; @@ -888,7 +818,7 @@ struct uwsgi_core { uint64_t failed_requests; // multiple ts per-core are needed only with multiple_interpreter + threads - PyThreadState *ts[MAX_APPS]; + void *ts[MAX_APPS]; }; @@ -948,28 +878,12 @@ void harakiri(void); void stats(void); -void init_uwsgi_vars(void); -void init_uwsgi_embedded_module(void); - #ifdef UWSGI_XML void uwsgi_xml_config(struct wsgi_request *, struct option *); #endif -void uwsgi_wsgi_config(char *); -#ifdef UWSGI_PASTE -void uwsgi_paste_config(char *); -#endif -void uwsgi_file_config(char *); -void uwsgi_eval_config(char *); - void internal_server_error(int, char *); -void init_uwsgi_module_sharedarea(PyObject *); -void init_uwsgi_module_advanced(PyObject *); -#ifdef UWSGI_SPOOLER -void init_uwsgi_module_spooler(PyObject *); -#endif - #ifdef UWSGI_SNMP void manage_snmp(int, uint8_t *, int, struct sockaddr_in *); void snmp_init(void); @@ -990,10 +904,6 @@ uint32_t uwsgi_swap32(uint32_t); uint64_t uwsgi_swap64(uint64_t); #endif -int init_uwsgi_app(int, void *, struct wsgi_request *wsgi_req, int); - -PyObject *uwsgi_send_message(const char *, int, uint8_t, uint8_t, char *, int, int); - #ifdef UWSGI_UDP ssize_t send_udp_message(uint8_t, char *, char *, uint16_t); #endif @@ -1003,19 +913,6 @@ int uwsgi_parse_vars(struct wsgi_request *); int uwsgi_enqueue_message(char *, int, uint8_t, uint8_t, char *, int, int); -/* included HOOKS */ -int uwsgi_request_wsgi(struct wsgi_request *); -void uwsgi_after_request_wsgi(struct wsgi_request *); - -int uwsgi_request_admin(struct wsgi_request *); -#ifdef UWSGI_SPOOLER -int uwsgi_request_spooler(struct wsgi_request *); -#endif -int uwsgi_request_eval(struct wsgi_request *); -int uwsgi_request_fastfunc(struct wsgi_request *); -int uwsgi_request_marshal(struct wsgi_request *); -int uwsgi_request_ping(struct wsgi_request *); - #ifdef UWSGI_ERLANG #include @@ -1023,8 +920,6 @@ int uwsgi_request_ping(struct wsgi_request *); int init_erlang(char *, char *); void erlang_loop(struct wsgi_request *); -PyObject *eterm_to_py(ETERM *); -ETERM *py_to_eterm(PyObject *); #endif @@ -1092,29 +987,8 @@ void async_unpause_all(void); #define ASYNC_IS_OUT ASYNC_EV == ASYNC_OUT #endif -PyObject *py_eventfd_read(PyObject *, PyObject *) ; -PyObject *py_eventfd_write(PyObject *, PyObject *) ; - - #endif -#ifdef UWSGI_STACKLESS -PyObject *py_uwsgi_stackless(PyObject *, PyObject *) ; -#endif - -int manage_python_response(struct wsgi_request *); -int uwsgi_python_call(struct wsgi_request *, PyObject *, PyObject *); -PyObject *python_call(PyObject *, PyObject *, int); - -#ifdef UWSGI_SENDFILE -PyObject *py_uwsgi_sendfile(PyObject *, PyObject *) ; -ssize_t uwsgi_sendfile(struct wsgi_request *); -ssize_t uwsgi_do_sendfile(int, int, size_t, size_t, off_t*, int); -#endif - -PyObject *py_uwsgi_write(PyObject *, PyObject *) ; -PyObject *py_uwsgi_spit(PyObject *, PyObject *) ; - void uwsgi_as_root(void); #ifdef UWSGI_NAGIOS @@ -1122,18 +996,6 @@ void nagios(void); #endif -#ifdef UWSGI_STACKLESS -struct stackless_req { - PyTaskletObject *tasklet; - struct wsgi_request *wsgi_req; - PyChannelObject *channel; -}; -struct wsgi_request *find_request_by_tasklet(PyTaskletObject *); - -void stackless_init(void); -void stackless_loop(void); -#endif - void uwsgi_close_request(struct wsgi_request *) ; void wsgi_req_setup(struct wsgi_request *, int); @@ -1164,78 +1026,10 @@ void uwsgi_log(const char *, ...); void uwsgi_log_verbose(const char *, ...); -#ifdef UWSGI_EVDIS -#define EVDIS_TYPE_FILE -#define EVDIS_TYPE_DNSSD -#endif - int uwsgi_load_plugin(int, char *, char *, int); void embed_plugins(void); -// PLUGINS - -#define UWSGI_PLUGIN_LONGOPT_PSGI -#define UWSGI_PLUGIN_LONGOPT_LUA -#define UWSGI_PLUGIN_LONGOPT_RACK -#define LONG_ARGS_PLUGIN_EMBED_PSGI -#define LONG_ARGS_PLUGIN_EMBED_LUA -#define LONG_ARGS_PLUGIN_EMBED_RACK - - - -#ifdef UWSGI_EMBED_PLUGINS - - -#ifdef UWSGI_EMBED_PLUGIN_PSGI - -#undef UWSGI_PLUGIN_LONGOPT_PSGI -#define UWSGI_PLUGIN_LONGOPT_PSGI {"psgi", required_argument, 0, 30005}, - -#undef LONG_ARGS_PLUGIN_EMBED_PSGI -#define LONG_ARGS_PLUGIN_EMBED_PSGI case 30005:\ - uwsgi.plugin_arg_psgi = optarg;\ - break; -#endif - -#ifdef UWSGI_EMBED_PLUGIN_LUA - -#undef UWSGI_PLUGIN_LONGOPT_LUA -#define UWSGI_PLUGIN_LONGOPT_LUA {"lua", required_argument, 0, 30006}, - -#undef LONG_ARGS_PLUGIN_EMBED_LUA -#define LONG_ARGS_PLUGIN_EMBED_LUA case 30006:\ - uwsgi.plugin_arg_lua = optarg;\ - break; -#endif - -#ifdef UWSGI_EMBED_PLUGIN_RACK - -#undef UWSGI_PLUGIN_LONGOPT_RACK -#define UWSGI_PLUGIN_LONGOPT_RACK {"rack", required_argument, 0, 30007},\ - {"rails", required_argument, 0, 30008}, - -#undef LONG_ARGS_PLUGIN_EMBED_RACK -#define LONG_ARGS_PLUGIN_EMBED_RACK case 30007:\ - uwsgi.plugin_arg_rack = optarg;\ - break;\ - case 30008:\ - uwsgi.plugin_arg_rails = optarg;\ - break; -#endif - - -#endif - -#ifdef UWSGI_ROUTING -void routing_setup(void); -void check_route(struct wsgi_request *); -void uwsgi_route_action_uwsgi(struct wsgi_request *, struct uwsgi_route *); -void uwsgi_route_action_wsgi(struct wsgi_request *, struct uwsgi_route *); -#endif - -void init_pyargv(void); - void http_loop(); int unconfigured_hook(struct wsgi_request *); @@ -1263,16 +1057,9 @@ char *uwsgi_concatn(int, ...); char *uwsgi_concat2(char *, char *); char *uwsgi_concat3(char *, char *, char *); char *uwsgi_concat3n(char *, int, char *, int, char *, int); +char *uwsgi_concat4(char *, char *, char *, char *); -void *uwsgi_request_subhandler_wsgi(struct wsgi_request *, struct uwsgi_app *); -int uwsgi_response_subhandler_wsgi(struct wsgi_request *); - -#ifdef UWSGI_WEB3 -void *uwsgi_request_subhandler_web3(struct wsgi_request *, struct uwsgi_app *); -int uwsgi_response_subhandler_web3(struct wsgi_request *); -#endif - int uwsgi_get_app_id(char *, int); char *uwsgi_strncopy(char *, int ); @@ -1280,31 +1067,36 @@ void master_loop(char **, char **); int find_worker_id(pid_t); -void init_uwsgi_py_interpreter(struct wsgi_request *); - -PyObject *uwsgi_uwsgi_loader(void *); -PyObject *uwsgi_dyn_loader(void *); -PyObject *uwsgi_file_loader(void *); -PyObject *uwsgi_eval_loader(void *); -PyObject *uwsgi_paste_loader(void *); -PyObject *uwsgi_callable_loader(void *); -PyObject *uwsgi_string_callable_loader(void *); -PyObject *uwsgi_mount_loader(void *); - -char *get_uwsgi_pymodule(char *); -PyObject *get_uwsgi_pydict(char *); - void *simple_loop(void *); void complex_loop(void); +struct uwsgi_plugin { + + char *name; + uint8_t modifier1; + void *data; + int (*init)(void); + void (*post_fork)(void); + struct option *options; + int (*manage_opt)(int, char*); + void (*magic)(char *); + void (*enable_threads)(void); + void (*init_thread)(void); + int (*request)(struct wsgi_request*); + void (*after_request)(struct wsgi_request*); + void (*init_apps)(void); + int (*manage_udp)(char *, int, char *, int); + +}; + +int count_options(struct option*); + +#ifdef UWSGI_SENDFILE +ssize_t uwsgi_do_sendfile(int , int , size_t , size_t , off_t *, int ); +#endif -void gil_real_get(void); -void gil_real_release(void); -void gil_fake_get(void); -void gil_fake_release(void); - -struct wsgi_request* threaded_current_wsgi_req(void); struct wsgi_request* simple_current_wsgi_req(void); +struct wsgi_request* threaded_current_wsgi_req(void); diff --git a/uwsgi_handlers.c b/uwsgi_handlers.c index 070f5f42..267d4c9f 100644 --- a/uwsgi_handlers.c +++ b/uwsgi_handlers.c @@ -2,61 +2,6 @@ extern struct uwsgi_server uwsgi; -/* uwsgi PING|100 */ -int uwsgi_request_ping(struct wsgi_request *wsgi_req) { - char len; - - uwsgi_log( "PING\n"); - wsgi_req->uh.modifier2 = 1; - wsgi_req->uh.pktsize = 0; - - len = strlen(uwsgi.shared->warning_message); - if (len > 0) { - // TODO: check endianess ? - wsgi_req->uh.pktsize = len; - } - if (write(wsgi_req->poll.fd, wsgi_req, 4) != 4) { - uwsgi_error("write()"); - } - - if (len > 0) { - if (write(wsgi_req->poll.fd, uwsgi.shared->warning_message, len) - != len) { - uwsgi_error("write()"); - } - } - - return UWSGI_OK; -} - -/* uwsgi ADMIN|10 */ -int uwsgi_request_admin(struct wsgi_request *wsgi_req) { - uint32_t opt_value = 0; - int i; - - if (wsgi_req->uh.pktsize >= 4) { - memcpy(&opt_value, wsgi_req->buffer, 4); - // TODO: check endianess ? - } - - uwsgi_log( "setting internal option %d to %d\n", wsgi_req->uh.modifier2, opt_value); - uwsgi.shared->options[wsgi_req->uh.modifier2] = opt_value; - - // ACK - wsgi_req->uh.modifier1 = 255; - wsgi_req->uh.pktsize = 0; - wsgi_req->uh.modifier2 = 1; - - i = write(wsgi_req->poll.fd, wsgi_req, 4); - if (i != 4) { - uwsgi_error("write()"); - } - - - - return UWSGI_OK; -} - int uwsgi_request_eval(struct wsgi_request *wsgi_req) { PyObject *code, *py_dict; diff --git a/uwsgiconfig.py b/uwsgiconfig.py index 1a7f8549..ca3a2d57 100644 --- a/uwsgiconfig.py +++ b/uwsgiconfig.py @@ -1,53 +1,4 @@ -# uWSGI configuration - -XML=True -INI=True -SNMP=True -SCTP=False -ERLANG=False -SPOOLER=True -EMBEDDED=True -UDP=True -MULTICAST=True -THREADING=True -SENDFILE=True -NAGIOS=True -PROXY=True -PASTE=True -MINTERPRETERS=True -ASYNC=True -UGREEN=False -HTTP=True -EVDIS=False -LDAP=True -WEB3=True -ROUTING=False -STACKLESS=False -#PLUGINS = ['psgi'] -PLUGINS = ['psgi'] -USWALLOW=False -UNBIT=False -DEBUG=False -EMBED_PLUGINS=True -UWSGI_BIN_NAME = 'uwsgi' -UWSGI_PLUGIN_DIR = '.' - - -# specific compilation flags -# libxml2 or expat -XML_IMPLEMENTATION = 'libxml2' -# if you want to use alternative python lib, specifiy its path here -#PYLIB_PATH = '/home/roberto/uwsgi/STACKLESS/slp/lib' -PYLIB_PATH = '' -ERLANG_CFLAGS = '' -ERLANG_LDFLAGS = '-lerl_interface -lei' -# for source distribution installed in /usr/local -#ERLANG_CFLAGS = '-I /usr/local/lib/erlang/lib/erl_interface-3.6.5/include/' -#ERLANG_LDFLAGS = '-L/usr/local/lib/erlang/lib/erl_interface-3.6.5/lib -lerl_interface -lei' - - - -# end of configuration +# uWSGI build system import os uwsgi_os = os.uname()[0] @@ -58,6 +9,8 @@ import subprocess from distutils import sysconfig +import ConfigParser + GCC = os.environ.get('CC', sysconfig.get_config_var('CC')) if not GCC: GCC = 'gcc' @@ -83,42 +36,6 @@ def spcall2(cmd): else: return None -gcc_version = str(spcall2("%s -v" % GCC)).split('\n')[-1].split()[2] - -gcc_major = int(gcc_version.split('.')[0]) -gcc_minor = int(gcc_version.split('.')[1]) - - -gcc_list = ['utils', 'pyutils', 'pyloader', 'protocol', 'socket', 'logging', 'master', 'wsgi_handlers', 'wsgi_subhandler', 'wsgi_headers', 'uwsgi_handlers', 'plugins', 'loop', 'gil', 'uwsgi'] - -cflags = ['-O2', '-Wall', '-Werror', '-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64'] + os.environ.get("CFLAGS", "").split() - -# add -fno-strict-aliasing only on python2 and gcc < 4.3 -if (sys.version_info[0] == 2) or (gcc_major < 4) or (gcc_major == 4 and gcc_minor < 3): - cflags = cflags + ['-fno-strict-aliasing'] - -if gcc_major >= 4: - cflags = cflags + [ '-Wextra', '-Wno-unused-parameter', '-Wno-missing-field-initializers' ] - -cflags = cflags + ['-I' + sysconfig.get_python_inc(), '-I' + sysconfig.get_python_inc(plat_specific=True) ] - -ldflags = os.environ.get("LDFLAGS", "").split() -libs = ['-lpthread', '-rdynamic'] + sysconfig.get_config_var('LIBS').split() + sysconfig.get_config_var('SYSLIBS').split() -if not sysconfig.get_config_var('Py_ENABLE_SHARED'): - libs.append('-L' + sysconfig.get_config_var('LIBPL')) - - -if USWALLOW: - cflags = cflags + sysconfig.get_config_var('LLVM_CXXFLAGS').split() - ldflags = ldflags + sysconfig.get_config_var('LLVM_LDFLAGS').split() + sysconfig.get_config_var('LINKFORSHARED').split() - GCC = 'clang' - -def depends_on(what, dep): - for d in dep: - if not globals()[d]: - print("%s needs %s support." % (what, d)) - sys.exit(1) - def add_o(x): if x == 'uwsgi': @@ -126,24 +43,65 @@ def add_o(x): x = x + '.o' return x +def compile(file, objfile, cflags): + cmdline = "%s -c %s -o %s %s" % (GCC, cflags, objfile, file) + print(cmdline) + ret = os.system(cmdline) + if ret != 0: + sys.exit(1) -def build_uwsgi(bin_name): - print("*** uWSGI compiling ***") +def build_uwsgi(uc): + + gcc_list, cflags, ldflags, libs = uc.get_gcll() + + print("*** uWSGI compiling server core ***") for file in gcc_list: objfile = file if objfile == 'uwsgi': objfile = 'main' - cmdline = "%s -c %s -o %s %s" % (GCC, ' '.join(cflags), objfile + '.o', file + '.c') - print(cmdline) - ret = os.system(cmdline) - if ret != 0: - sys.exit(1) + compile(' '.join(cflags), objfile + '.o', file + '.c') - if len(PLUGINS) > 0: - print("*** uWSGI building plugins ***") - for plugin in PLUGINS: - print("*** building plugin: %s ***" % plugin) - build_plugin("plugins/%s" % plugin) + if uc.get('embedded_plugins'): + ep = uc.get('embedded_plugins').split(',') + + if len(ep) > 0: + print("*** uWSGI compiling embedded plugins ***") + for p in ep: + path = 'plugins/%s' % p + path = path.rstrip('/') + + sys.path.insert(0, path) + import uwsgiplugin as up + reload(up) + + p_cflags = cflags[:] + p_ldflags = ldflags[:] + + p_cflags += up.CFLAGS + p_ldflags += up.LDFLAGS + + for cfile in up.GCC_LIST: + compile(' '.join(p_cflags), path + '/' + cfile + '.o', path + '/' + cfile + '.c') + gcc_list.append('%s/%s' % (path, cfile)) + + libs += up.LIBS + + up.CFLAGS = None + up.LDFLAGS = None + up.LIBS = None + up.GCC_LIST = None + + if uc.get('plugins'): + + plugins = uc.get('plugins').split(',') + if len(plugins) > 0: + print("*** uWSGI building plugins ***") + + for p in plugins: + print("*** building plugin: %s ***" % p) + build_plugin("plugins/%s" % p, uc, cflags, ldflags, libs) + + bin_name = uc.get('bin_name') print("*** uWSGI linking ***") ldline = "%s -o %s %s %s %s" % (GCC, bin_name, ' '.join(ldflags), ' '.join(map(add_o, gcc_list)), ' '.join(libs)) @@ -157,180 +115,100 @@ def build_uwsgi(bin_name): bin_name = './' + bin_name print("*** uWSGI is ready, launch it with %s ***" % bin_name) -def unbit_setup(): +class uConf(): - global XML - XML=True + def __init__(self, filename): + self.config = ConfigParser.ConfigParser() + self.config.read(filename) + self.gcc_list = ['utils', 'protocol', 'socket', 'logging', 'master', 'plugins', 'loop', 'uwsgi'] + self.cflags = ['-O2', '-Wall', '-Werror', '-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64'] + os.environ.get("CFLAGS", "").split() + gcc_version = str(spcall2("%s -v" % GCC)).split('\n')[-1].split()[2] + gcc_major = int(gcc_version.split('.')[0]) + gcc_minor = int(gcc_version.split('.')[1]) + if (sys.version_info[0] == 2) or (gcc_major < 4) or (gcc_major == 4 and gcc_minor < 3): + self.cflags = self.cflags + ['-fno-strict-aliasing'] + # add -fno-strict-aliasing only on python2 and gcc < 4.3 + if gcc_major >= 4: + self.cflags = self.cflags + [ '-Wextra', '-Wno-unused-parameter', '-Wno-missing-field-initializers' ] - global INI - INI=False + self.ldflags = os.environ.get("LDFLAGS", "").split() + self.libs = ['-lpthread', '-rdynamic'] - global SNMP - SNMP=False + def get(self,key): + try: + value = self.config.get('uwsgi', key) + if value == "" or value == "false": + return None + return value + except: + return None - global SCTP - SCTP=False - global ERLANG - ERLANG=False - global SPOOLER - SPOOLER=True + def depends_on(self, what, dep): + for d in dep: + if not self.get(d): + print("%s needs %s support." % (what, d)) + sys.exit(1) - global EMBEDDED - EMBEDDED=True - - global UDP - UDP=False - global MULTICAST - MULTICAST=False + def get_gcll(self): + kvm_list = ['FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly'] - global THREADING - THREADING=False + if uwsgi_os == 'SunOS': + self.libs.append('-lsendfile') + self.libs.remove('-rdynamic') - global SENDFILE - SENDFILE=True + if uwsgi_os in kvm_list: + self.libs.append('-lkvm') - global NAGIOS - NAGIOS=False + if uwsgi_os == 'OpenBSD' or uwsgi_cpu[0:3] == 'arm' or uwsgi_os == 'Haiku': + self.set('ugreen', 'false') - global PROXY - PROXY=False + if uwsgi_os == 'Haiku': + self.set('async', 'false') + self.set('proxy', 'false') + self.libs.remove('-rdynamic') + self.libs.remove('-lpthread') + self.libs.append('-lroot') - global PASTE - PASTE=False + if self.get('embedded'): + self.cflags.append('-DUWSGI_EMBEDDED') + self.gcc_list.append('uwsgi_pymodule') - global MINTERPRETERS - MINTERPRETERS=False - - global ASYNC - ASYNC=False + if self.get('udp'): + self.cflags.append("-DUWSGI_UDP") - global UGREEN - UGREEN=False + if self.get('async'): + self.cflags.append("-DUWSGI_ASYNC") + self.gcc_list.append('async') - global HTTP - HTTP=False + if self.get('multicast'): + self.depends_on('multicast', ['udp']) + self.cflags.append("-DUWSGI_MULTICAST") - global EVDIS - EVDIS=False + if self.get('minterpreters'): + self.cflags.append("-DUWSGI_MINTERPRETERS") - global WEB3 - WEB3=False + if self.get('nagios'): + self.cflags.append("-DUWSGI_NAGIOS") + self.gcc_list.append('nagios') - global LDAP - LDAP=False + if self.get('ini'): + self.cflags.append("-DUWSGI_INI") + self.gcc_list.append('ini') - global ROUTING - ROUTING=False + if self.get('proxy'): + self.depends_on('proxy', ['async']) + self.cflags.append("-DUWSGI_PROXY") + self.gcc_list.append('proxy') - global STACKLESS - STACKLESS=False - - global PLUGINS - #PLUGINS = ['psgi'] - PLUGINS = [] - - global USWALLOW - USWALLOW=False - - global UNBIT - UNBIT=True - - global DEBUG - DEBUG=False - - global EMBED_PLUGINS - EMBED_PLUGINS=True - - global UWSGI_BIN_NAME - UWSGI_BIN_NAME = '../bin/uwsgi' - - global UWSGI_PLUGIN_DIR - UWSGI_PLUGIN_DIR = '../bin' - - global PYLIB_PATH - PYLIB_PATH = '/proc/unbit/opt/python26/lib' - -def parse_vars(): - - global UGREEN, ASYNC, PROXY - - version = sys.version_info - uver = "%d.%d" % (version[0], version[1]) - - libs.append('-lpython' + uver) - - if str(PYLIB_PATH) != '': - libs.insert(0,'-L' + PYLIB_PATH) - os.environ['LD_RUN_PATH'] = PYLIB_PATH - - kvm_list = ['FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly'] - - if uwsgi_os == 'SunOS': - libs.append('-lsendfile') - libs.remove('-rdynamic') - - if uwsgi_os in kvm_list: - libs.append('-lkvm') - - if uwsgi_os == 'OpenBSD' or uwsgi_cpu[0:3] == 'arm' or uwsgi_os == 'Haiku': - UGREEN = False - - if uwsgi_os == 'Haiku': - ASYNC = False - PROXY = False - libs.remove('-rdynamic') - libs.remove('-lpthread') - libs.append('-lroot') - - if EMBEDDED: - cflags.append('-DUWSGI_EMBEDDED') - gcc_list.append('uwsgi_pymodule') - - if UDP: - cflags.append("-DUWSGI_UDP") - - if ASYNC: - cflags.append("-DUWSGI_ASYNC") - gcc_list.append('async') - - if MULTICAST: - depends_on("MULTICAST", ['UDP']) - cflags.append("-DUWSGI_MULTICAST") - - if STACKLESS: - if not cflags.__contains__('-DSTACKLESS_FRHACK=1'): - print("you need Stackless Python to use Tasklet") - sys.exit(1) - cflags.append("-DUWSGI_STACKLESS") - gcc_list.append('stackless') - - if MINTERPRETERS: - cflags.append("-DUWSGI_MINTERPRETERS") - - if NAGIOS: - cflags.append("-DUWSGI_NAGIOS") - gcc_list.append('nagios') - - if INI: - cflags.append("-DUWSGI_INI") - gcc_list.append('ini') - - if DEBUG: - cflags.append("-DUWSGI_DEBUG") - - if PROXY: - depends_on("PROXY", ['ASYNC']) - cflags.append("-DUWSGI_PROXY") - gcc_list.append('proxy') - - if LDAP: - cflags.append("-DUWSGI_LDAP") - gcc_list.append('ldap') - libs.append('-lldap') + if self.get('ldap'): + self.cflags.append("-DUWSGI_LDAP") + self.gcc_list.append('ldap') + self.libs.append('-lldap') + """ if ROUTING: depends_on("ROUTING", ['WEB3', 'XML']) cflags.append("-DUWSGI_ROUTING") @@ -348,119 +226,114 @@ def parse_vars(): sys.exit(1) else: libs.append(pcreconf) + """ - if HTTP: - cflags.append("-DUWSGI_HTTP") - gcc_list.append('http') + if self.get('http'): + self.cflags.append("-DUWSGI_HTTP") + self.gcc_list.append('http') - if EVDIS: - cflags.append("-DUWSGI_EVDIS") - gcc_list.append('evdis') + if self.get('evdis'): + self.cflags.append("-DUWSGI_EVDIS") + self.gcc_list.append('evdis') - if UGREEN: - if uwsgi_os == 'Darwin': - cflags.append("-D_XOPEN_SOURCE") - depends_on("UGREEN", ['ASYNC']) - cflags.append("-DUWSGI_UGREEN") - gcc_list.append('ugreen') + if self.get('ugreen'): + if uwsgi_os == 'Darwin': + self.cflags.append("-D_XOPEN_SOURCE") + self.depends_on("ugreen", ['async']) + self.cflags.append("-DUWSGI_UGREEN") + self.gcc_list.append('ugreen') - if SNMP: - depends_on("SNMP", ['UDP']) - cflags.append("-DUWSGI_SNMP") - gcc_list.append('snmp') + if self.get('snmp'): + self.depends_on("snmp", ['udp']) + self.cflags.append("-DUWSGI_SNMP") + self.gcc_list.append('snmp') - if THREADING: - cflags.append("-DUWSGI_THREADING") + if self.get('threading'): + self.cflags.append("-DUWSGI_THREADING") - if PASTE: - cflags.append('-DUWSGI_PASTE') + if self.get('sendfile'): + self.cflags.append("-DUWSGI_SENDFILE") + self.gcc_list.append('sendfile') - if SENDFILE: - cflags.append("-DUWSGI_SENDFILE") - gcc_list.append('sendfile') - - if XML: - if XML_IMPLEMENTATION == 'libxml2': - xmlconf = spcall('xml2-config --libs') - if xmlconf is None: - print("*** libxml2 headers unavailable. uWSGI build is interrupted. You have to install libxml2 development package or use libexpat or disable XML") - sys.exit(1) - else: - libs.append(xmlconf) - xmlconf = spcall("xml2-config --cflags") + if self.get('xml'): + if self.get('xml_implementation') == 'libxml2': + xmlconf = spcall('xml2-config --libs') if xmlconf is None: print("*** libxml2 headers unavailable. uWSGI build is interrupted. You have to install libxml2 development package or use libexpat or disable XML") sys.exit(1) else: - cflags.append(xmlconf) - cflags.append("-DUWSGI_XML -DUWSGI_XML_LIBXML2") - gcc_list.append('xmlconf') - elif XML_IMPLEMENTATION == 'expat': - cflags.append("-DUWSGI_XML -DUWSGI_XML_EXPAT") - libs.append('-lexpat') - gcc_list.append('xmlconf') + self.libs.append(xmlconf) + xmlconf = spcall("xml2-config --cflags") + if xmlconf is None: + print("*** libxml2 headers unavailable. uWSGI build is interrupted. You have to install libxml2 development package or use libexpat or disable XML") + sys.exit(1) + else: + self.cflags.append(xmlconf) + self.cflags.append("-DUWSGI_XML -DUWSGI_XML_LIBXML2") + self.gcc_list.append('xmlconf') + elif self.get('xml_implementation') == 'expat': + self.cflags.append("-DUWSGI_XML -DUWSGI_XML_EXPAT") + self.libs.append('-lexpat') + self.gcc_list.append('xmlconf') - if ERLANG: - depends_on("ERLANG", ['EMBEDDED']) - cflags.append("-DUWSGI_ERLANG") - libs.append(ERLANG_LDFLAGS) - if str(ERLANG_CFLAGS) != '': - cflags.append(ERLANG_CFLAGS) - gcc_list.append('erlang') + if self.get('erlang'): + self.depends_on("ERLANG", ['EMBEDDED']) + self.cflags.append("-DUWSGI_ERLANG") + self.libs.append(ERLANG_LDFLAGS) + if str(ERLANG_CFLAGS) != '': + self.cflags.append(ERLANG_CFLAGS) + self.gcc_list.append('erlang') - if UWSGI_PLUGIN_DIR is not None: - cflags.append("-DUWSGI_PLUGIN_DIR=\\\"%s\\\"" % UWSGI_PLUGIN_DIR) + if self.get('plugin_dir'): + self.cflags.append('-DUWSGI_PLUGIN_DIR=\\"%s\\"' % self.get('plugin_dir')) - if len(PLUGINS) > 0 and EMBED_PLUGINS: - cflags.append("-DUWSGI_EMBED_PLUGINS") - for plugin in PLUGINS: - cflags.append("-DUWSGI_EMBED_PLUGIN_%s" % plugin.upper()) + if self.get('spooler'): + self.depends_on("spooler", ['embedded']) + self.cflags.append("-DUWSGI_SPOOLER") + self.gcc_list.append('spooler') - if SCTP: - libs.append("-lsctp") - cflags.append("-DUWSGI_SCTP") + if self.get('debug'): + self.cflags.append("-DUWSGI_DEBUG") + self.cflags.append("-g") - if SPOOLER: - depends_on("SPOOLER", ['EMBEDDED']) - cflags.append("-DUWSGI_SPOOLER") - gcc_list.append('spooler') + if self.get('unbit'): + self.cflags.append("-DUNBIT") - if WEB3: - cflags.append("-DUWSGI_WEB3") - gcc_list.append('web3_subhandler') + return self.gcc_list, self.cflags, self.ldflags, self.libs - if DEBUG: - cflags.append("-DUWSGI_DEBUG") - cflags.append("-g") - - if UNBIT: - cflags.append("-DUNBIT") - -def build_plugin(path): +def build_plugin(path, uc, cflags, ldflags, libs): path = path.rstrip('/') sys.path.insert(0, path) import uwsgiplugin as up + reload(up) p_cflags = cflags[:] p_ldflags = ldflags[:] + p_libs = libs[:] - p_cflags.append(up.CFLAGS) - p_ldflags.append(up.LDFLAGS) + p_cflags += up.CFLAGS + p_ldflags += up.LDFLAGS + p_libs += up.LIBS p_cflags.insert(0, '-I.') - plugin_base = path + '/' + up.NAME + '_plugin' - plugin_dest = UWSGI_PLUGIN_DIR + '/' + up.NAME + '_plugin' + plugin_dest = uc.get('plugin_dir') + '/' + up.NAME + '_plugin' shared_flag = '-shared' + gcc_list = [] + if uwsgi_os == 'Darwin': shared_flag = '-dynamiclib -undefined dynamic_lookup' - gccline = "%s -fPIC %s -o %s.so %s %s %s.c" % (GCC, shared_flag, plugin_dest, ' '.join(p_cflags), ' '.join(p_ldflags), plugin_base ) + for cfile in up.GCC_LIST: + gcc_list.append(path + '/' + cfile + '.c') + + + gccline = "%s -fPIC %s -o %s.so %s %s %s %s" % (GCC, shared_flag, plugin_dest, ' '.join(p_cflags), ' '.join(p_ldflags), ' '.join(gcc_list), ' '.join(p_libs) ) print(gccline) ret = os.system(gccline) @@ -491,15 +364,12 @@ if __name__ == "__main__": if cmd == '--libs': print(' '.join(libs)) elif cmd == '--build': - parse_vars() - build_uwsgi(UWSGI_BIN_NAME) + build_uwsgi(uConf('buildconf/default.ini')) elif cmd == '--unbit': - unbit_setup() - parse_vars() - build_uwsgi(UWSGI_BIN_NAME) + build_uwsgi(uConf('buildconf/unbit.ini')) elif cmd == '--plugin': parse_vars() - build_plugin(sys.argv[2]) + build_plugin(sys.argv[2], uConf('buildconf/unbit.ini')) else: print("unknown uwsgiconfig command") sys.exit(1) diff --git a/web3_subhandler.c b/web3_subhandler.c deleted file mode 100644 index 2767f44b..00000000 --- a/web3_subhandler.c +++ /dev/null @@ -1,165 +0,0 @@ -#include "uwsgi.h" - -extern struct uwsgi_server uwsgi; - -void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { - - PyObject *zero, *wsgi_socket; - - - wsgi_socket = PyFile_FromFile(wsgi_req->async_post, "web3_input", "r", NULL); - PyDict_SetItemString(wsgi_req->async_environ, "web3.input", wsgi_socket); - Py_DECREF(wsgi_socket); - - zero = PyTuple_New(2); - PyTuple_SetItem(zero, 0, PyInt_FromLong(1)); - PyTuple_SetItem(zero, 1, PyInt_FromLong(0)); - PyDict_SetItemString(wsgi_req->async_environ, "web3.version", zero); - Py_DECREF(zero); - - zero = PyFile_FromFile(stderr, "web3_input", "w", NULL); - PyDict_SetItemString(wsgi_req->async_environ, "web3.errors", zero); - Py_DECREF(zero); - - PyDict_SetItemString(wsgi_req->async_environ, "web3.run_once", Py_False); - - PyDict_SetItemString(wsgi_req->async_environ, "web3.multithread", Py_False); - if (uwsgi.numproc == 1) { - PyDict_SetItemString(wsgi_req->async_environ, "web3.multiprocess", Py_False); - } - else { - PyDict_SetItemString(wsgi_req->async_environ, "web3.multiprocess", Py_True); - } - - if (wsgi_req->scheme_len > 0) { - zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len); - } - else if (wsgi_req->https_len > 0) { - if (!strncasecmp(wsgi_req->https, "on", 2) || wsgi_req->https[0] == '1') { - zero = PyString_FromString("https"); - } - else { - zero = PyString_FromString("http"); - } - } - else { - zero = PyString_FromString("http"); - } - PyDict_SetItemString(wsgi_req->async_environ, "web3.url_scheme", zero); - Py_DECREF(zero); - - - wsgi_req->async_app = wi->wsgi_callable ; - - PyDict_SetItemString(uwsgi.embedded_dict, "env", wsgi_req->async_environ); - - // TODO: fix this - //PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.version", uwsgi_version); - - - // call - - 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_web3(struct wsgi_request *wsgi_req) { - - PyObject *pychunk ; - ssize_t wsize ; - - // return or yield ? (PyString on python2 PyBytes on python3) - if (PyString_Check((PyObject *)wsgi_req->async_result)) { - if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(wsgi_req->async_result), PyString_Size(wsgi_req->async_result))) < 0) { - uwsgi_error("write()"); - goto clear; - } - wsgi_req->response_size += wsize; - goto clear; - } - - - // ok its a yield - if (!wsgi_req->async_placeholder) { - if (PyTuple_Check((PyObject *)wsgi_req->async_result)) { - if (PyTuple_Size((PyObject *)wsgi_req->async_result) != 3) { - uwsgi_log("invalid Web3 response.\n"); - goto clear; - } - if (py_uwsgi_spit(NULL, (PyObject *)wsgi_req->async_result) == Py_None) { - goto clear; - } - - wsgi_req->async_result = PyTuple_GetItem((PyObject *)wsgi_req->async_result, 0); - - wsgi_req->async_placeholder = PyObject_GetIter( (PyObject *)wsgi_req->async_result ); - - if (!wsgi_req->async_placeholder) { - goto clear2; - } -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - return UWSGI_AGAIN; - } - } - else { - uwsgi_log("invalid Web3 response.\n"); - goto clear; - } -#endif - } - - - - pychunk = PyIter_Next(wsgi_req->async_placeholder) ; - - if (!pychunk) { - if (PyErr_Occurred()) PyErr_Print(); - goto clear; - } - - - - if (PyString_Check(pychunk)) { - if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(pychunk), PyString_Size(pychunk))) < 0) { - uwsgi_error("write()"); - Py_DECREF(pychunk); - goto clear; - } - wsgi_req->response_size += wsize; - } - - - Py_DECREF(pychunk); - return UWSGI_AGAIN ; - -clear: - if (wsgi_req->async_environ) { - PyDict_Clear(wsgi_req->async_environ); - } - if (wsgi_req->async_post && !wsgi_req->fd_closed) { - fclose(wsgi_req->async_post); - if (!uwsgi.post_buffering || wsgi_req->post_cl <= (size_t) uwsgi.post_buffering) { - wsgi_req->fd_closed = 1 ; - } - } - Py_XDECREF((PyObject *)wsgi_req->async_placeholder); -clear2: - Py_DECREF((PyObject *)wsgi_req->async_result); - PyErr_Clear(); - -#ifdef UWSGI_DEBUG - if (wsgi_req->async_placeholder) { - uwsgi_debug("wsgi_req->async_placeholder: %d\n", ((PyObject *)wsgi_req->async_placeholder)->ob_refcnt); - } - if (wsgi_req->async_result) { - uwsgi_debug("wsgi_req->async_result: %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt); - } - if (wsgi_req->async_app) { - uwsgi_debug("wsgi_req->async_app: %d\n", ((PyObject *)wsgi_req->async_app)->ob_refcnt); - } -#endif - return UWSGI_OK; -} - diff --git a/wsgi_handlers.c b/wsgi_handlers.c deleted file mode 100644 index ed15bd20..00000000 --- a/wsgi_handlers.c +++ /dev/null @@ -1,413 +0,0 @@ -#include "uwsgi.h" - -extern struct uwsgi_server uwsgi; - -PyObject *py_uwsgi_write(PyObject * self, PyObject * args) { - PyObject *data; - char *content; - int len; - - struct wsgi_request *wsgi_req = current_wsgi_req(); - - data = PyTuple_GetItem(args, 0); - if (PyString_Check(data)) { - content = PyString_AsString(data); - len = PyString_Size(data); - UWSGI_RELEASE_GIL - wsgi_req->response_size = write(wsgi_req->poll.fd, content, len); - UWSGI_GET_GIL - } - - Py_INCREF(Py_None); - return Py_None; -} - -#ifdef UWSGI_ASYNC - -PyObject *py_eventfd_read(PyObject * self, PyObject * args) { - int fd, timeout; - - struct wsgi_request *wsgi_req = current_wsgi_req(); - - if (!PyArg_ParseTuple(args, "i|i", &fd, &timeout)) { - return NULL; - } - - if (fd >= 0) { - wsgi_req->async_waiting_fd = fd ; - wsgi_req->async_waiting_fd_type = ASYNC_IN ; - wsgi_req->async_waiting_fd_monitored = 0 ; - } - - return PyString_FromString("") ; -} - - -PyObject *py_eventfd_write(PyObject * self, PyObject * args) { - int fd, timeout; - - struct wsgi_request *wsgi_req = current_wsgi_req(); - - if (!PyArg_ParseTuple(args, "i|i", &fd, &timeout)) { - return NULL; - } - - if (fd >= 0) { - wsgi_req->async_waiting_fd = fd ; - wsgi_req->async_waiting_fd_type = ASYNC_OUT ; - wsgi_req->async_waiting_fd_monitored = 0 ; - } - - return PyString_FromString("") ; -} -#endif - -int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { - - int i; - - size_t post_remains = wsgi_req->post_cl; - ssize_t post_chunk; - - PyObject *pydictkey, *pydictvalue; - - char *path_info; - struct uwsgi_app *wi ; - - int tmp_stderr; - char *what; - int what_len; - -#ifdef UWSGI_ASYNC - if (wsgi_req->async_status == UWSGI_AGAIN) { - // get rid of timeout - if (wsgi_req->async_timeout_expired) { - PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.timeout", Py_True); - wsgi_req->async_timeout_expired = 0 ; - } - else { - PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.timeout", Py_None); - } - return manage_python_response(wsgi_req); - } -#endif - - - /* Standard WSGI request */ - if (!wsgi_req->uh.pktsize) { - uwsgi_log( "Invalid WSGI request. skip.\n"); - return -1; - } - - if (uwsgi_parse_vars(wsgi_req)) { - uwsgi_log("Invalid WSGI request. skip.\n"); - return -1; - } - - if (uwsgi.limit_post) { - if (wsgi_req->post_cl > uwsgi.limit_post) { - uwsgi_log("Invalid (too big) CONTENT_LENGTH. skip.\n"); - return -1; - } - } - - - if (!uwsgi.ignore_script_name) { - - if (!wsgi_req->script_name) - wsgi_req->script_name = ""; - - if (uwsgi.vhost) { - what = uwsgi_concat3n(wsgi_req->host, wsgi_req->host_len, "|",1, wsgi_req->script_name, wsgi_req->script_name_len); - what_len = wsgi_req->host_len + 1 + wsgi_req->script_name_len ; -#ifdef UWSGI_DEBUG - uwsgi_debug("VirtualHost SCRIPT_NAME=%s\n", what); -#endif - } - else { - what = wsgi_req->script_name; - what_len = wsgi_req->script_name_len ; - } - - - if ( (wsgi_req->app_id = uwsgi_get_app_id(what, what_len)) == -1) { - if (wsgi_req->script_name_len > 1 || uwsgi.default_app < 0 || uwsgi.vhost) { - /* unavailable app for this SCRIPT_NAME */ - wsgi_req->app_id = -1; - if (wsgi_req->wsgi_script_len > 0 - || wsgi_req->wsgi_module_len > 0 - || wsgi_req->wsgi_file_len > 0 -#ifdef UWSGI_PASTE - || wsgi_req->wsgi_paste_len > 0 -#endif - ) { - // a bit of magic: 1-1 = 0 / 0-1 = -1 - // this part must be heavy locked in threaded modes - if (uwsgi.threads > 1) { - pthread_mutex_lock(&uwsgi.lock_pyloaders); - } - UWSGI_GET_GIL - wsgi_req->app_id = init_uwsgi_app(LOADER_DYN, (void *) wsgi_req, wsgi_req, uwsgi.single_interpreter-1); - UWSGI_RELEASE_GIL - if (uwsgi.threads > 1) { - pthread_mutex_unlock(&uwsgi.lock_pyloaders); - } - } - } - } - - if (uwsgi.vhost) { - free(what); - } - - } - else { - wsgi_req->app_id = 0; - } - - - if (wsgi_req->app_id == -1) { - // use default app ? - if (!uwsgi.no_default_app && uwsgi.default_app >= 0) { - wsgi_req->app_id = uwsgi.default_app ; - } - else { - internal_server_error(wsgi_req->poll.fd, "wsgi application not found"); - goto clear2; - } - - } - - wi = &uwsgi.apps[wsgi_req->app_id]; - - if (uwsgi.single_interpreter == 0 && wsgi_req->app_id > 0) { - if (!wi->interpreter) { - internal_server_error(wsgi_req->poll.fd, "wsgi application's %d interpreter not found"); - goto clear2; - } - - // set the interpreter - UWSGI_GET_GIL - if (uwsgi.threads > 1) { - PyThreadState_Swap(uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id]->ts[wsgi_req->app_id]); - } - else { - PyThreadState_Swap(wi->interpreter); - } - UWSGI_RELEASE_GIL - if (wi->chdir) { -#ifdef UWSGI_DEBUG - uwsgi_debug("chdir to %s\n", wi->chdir); -#endif - if (chdir(wi->chdir)) { - uwsgi_error("chdir()"); - } - } - } - - - - - if (wsgi_req->protocol_len < 5) { - uwsgi_log( "INVALID PROTOCOL: %.*s\n", wsgi_req->protocol_len, wsgi_req->protocol); - internal_server_error(wsgi_req->poll.fd, "invalid HTTP protocol !!!"); - goto clear; - - } - if (strncmp(wsgi_req->protocol, "HTTP/", 5)) { - uwsgi_log( "INVALID PROTOCOL: %.*s\n", wsgi_req->protocol_len, wsgi_req->protocol); - internal_server_error(wsgi_req->poll.fd, "invalid HTTP protocol !!!"); - goto clear; - } - - - -#ifdef UWSGI_ASYNC - wsgi_req->async_environ = wi->wsgi_environ[wsgi_req->async_id]; - wsgi_req->async_args = wi->wsgi_args[wsgi_req->async_id]; -#else - wsgi_req->async_environ = wi->wsgi_environ; - wsgi_req->async_args = wi->wsgi_args; -#endif - - UWSGI_GET_GIL - - // no fear of race conditions for this counter as it is already protected by the GIL - wi->requests++; - - Py_INCREF((PyObject *)wsgi_req->async_environ); - - for (i = 0; i < wsgi_req->var_cnt; i += 2) { -#ifdef UWSGI_DEBUG - uwsgi_debug("%.*s: %.*s\n", wsgi_req->hvec[i].iov_len, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i+1].iov_len, wsgi_req->hvec[i+1].iov_base); -#endif -#ifdef PYTHREE - pydictkey = PyUnicode_DecodeLatin1(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len, "ignore"); - pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, "ignore"); -#else - pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len); - pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len); -#endif - PyDict_SetItem(wsgi_req->async_environ, pydictkey, pydictvalue); - Py_DECREF(pydictkey); - Py_DECREF(pydictvalue); - } - - if (wsgi_req->uh.modifier1 == UWSGI_MODIFIER_MANAGE_PATH_INFO) { - pydictkey = PyDict_GetItemString(wsgi_req->async_environ, "SCRIPT_NAME"); - if (pydictkey) { - if (PyString_Check(pydictkey)) { - pydictvalue = PyDict_GetItemString(wsgi_req->async_environ, "PATH_INFO"); - if (pydictvalue) { - if (PyString_Check(pydictvalue)) { - path_info = PyString_AsString(pydictvalue); - PyDict_SetItemString(wsgi_req->async_environ, "PATH_INFO", PyString_FromString(path_info + PyString_Size(pydictkey))); - } - } - } - } - } - - - - - - if (uwsgi.post_buffering > 0 && wsgi_req->post_cl > (size_t) uwsgi.post_buffering) { - UWSGI_RELEASE_GIL - wsgi_req->async_post = tmpfile(); - if (!wsgi_req->async_post) { - uwsgi_error("tmpfile()"); - goto clear; - } - - - while(post_remains > 0) { - if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) { - inc_harakiri(uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); - } - if (post_remains > (size_t) uwsgi.post_buffering_bufsize) { - post_chunk = read(wsgi_req->poll.fd, wsgi_req->post_buffering_buf, uwsgi.post_buffering_bufsize); - } - else { - post_chunk = read(wsgi_req->poll.fd, wsgi_req->post_buffering_buf, post_remains); - } - if (post_chunk < 0) { - uwsgi_error("read()"); - goto clear; - } - if (!fwrite(wsgi_req->post_buffering_buf, post_chunk, 1, wsgi_req->async_post)) { - uwsgi_error("fwrite()"); - goto clear; - } - post_remains -= post_chunk; - } - rewind(wsgi_req->async_post); - UWSGI_GET_GIL - } - else { - wsgi_req->async_post = fdopen(wsgi_req->poll.fd, "r"); - } - - - wsgi_req->async_result = (*wi->request_subhandler)(wsgi_req, wi); - - - if (wsgi_req->async_result) { - - - UWSGI_RELEASE_GIL - while ( (*wi->response_subhandler)(wsgi_req) != UWSGI_OK) { - wsgi_req->switches++; -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - return UWSGI_AGAIN; - } -#endif - } - - - } - - else if (uwsgi.catch_exceptions) { - - // LOCK THIS PART - - wsgi_req->response_size += write(wsgi_req->poll.fd, wsgi_req->protocol, wsgi_req->protocol_len); - wsgi_req->response_size += write(wsgi_req->poll.fd, " 500 Internal Server Error\r\n", 28 ); - wsgi_req->response_size += write(wsgi_req->poll.fd, "Content-type: text/plain\r\n\r\n", 28 ); - wsgi_req->header_cnt = 1 ; - - /* - sorry that is a hack to avoid the rewrite of PyErr_Print - temporarily map (using dup2) stderr to wsgi_req->poll.fd - */ - tmp_stderr = dup(2); - if (tmp_stderr < 0) { - uwsgi_error("dup()"); - goto clear; - } - // map 2 to wsgi_req - if (dup2(wsgi_req->poll.fd, 2) < 0) { - close(tmp_stderr); - uwsgi_error("dup2()"); - goto clear; - } - // print the error - PyErr_Print(); - // ...resume the original stderr, in case of error we are damaged forever !!! - if (dup2(tmp_stderr, 2) < 0) { - uwsgi_error("dup2()"); - } - close(tmp_stderr); - } - - -clear: - - UWSGI_GET_GIL - - if (uwsgi.single_interpreter == 0 && wsgi_req->app_id > 0) { - // restoring main interpreter - if (uwsgi.threads > 1) { - PyThreadState_Swap((PyThreadState *) pthread_getspecific(uwsgi.ut_save_key)); - } - else { - PyThreadState_Swap(uwsgi.main_thread); - } - } - - UWSGI_RELEASE_GIL - -clear2: - - - return UWSGI_OK; - -} - -void uwsgi_after_request_wsgi(struct wsgi_request *wsgi_req) { - - if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) { - log_request(wsgi_req); - } - else { - if (uwsgi.shared->options[UWSGI_OPTION_LOG_ZERO]) { - if (wsgi_req->response_size == 0) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) { - if ((uint32_t) wsgi_req_time >= uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_4xx]) { - if (wsgi_req->status >= 400 && wsgi_req->status <= 499) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_5xx]) { - if (wsgi_req->status >= 500 && wsgi_req->status <= 599) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_BIG]) { - if (wsgi_req->response_size >= uwsgi.shared->options[UWSGI_OPTION_LOG_BIG]) { log_request(wsgi_req); return; } - } - if (uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE]) { - if (wsgi_req->sendfile_fd > -1 && wsgi_req->sendfile_obj == wsgi_req->async_result) { log_request(wsgi_req); return; } - } - } -} diff --git a/wsgi_headers.c b/wsgi_headers.c deleted file mode 100644 index 786c9c49..00000000 --- a/wsgi_headers.c +++ /dev/null @@ -1,160 +0,0 @@ -#include "uwsgi.h" - -extern struct uwsgi_server uwsgi; - -static char *nl = "\r\n"; -static char *h_sep = ": "; -static const char *http_protocol = "HTTP/1.1"; - -// check here - -PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { - PyObject *headers, *head; - PyObject *h_key, *h_value; - int i, j; - - struct wsgi_request *wsgi_req = current_wsgi_req(); - - int base = 0; - int shift = 0; - - // use writev() - - // is a Web3 response ? - /* - if (PyTuple_Size(args) == 3) { - shift = 0; - } - */ - - head = PyTuple_GetItem(args, 0+shift); - if (!head) { - goto clear; - } - - if (!PyString_Check(head)) { - uwsgi_log( "http status must be a string !\n"); - goto clear; - } - - - if (uwsgi.shared->options[UWSGI_OPTION_CGI_MODE] == 0) { - base = 4; - - if (wsgi_req->protocol_len == 0) { - wsgi_req->hvec[0].iov_base = (char *) http_protocol; - wsgi_req->protocol_len = 8; - } - else { - wsgi_req->hvec[0].iov_base = wsgi_req->protocol; - } - - wsgi_req->hvec[0].iov_len = wsgi_req->protocol_len; - wsgi_req->hvec[1].iov_base = " "; - wsgi_req->hvec[1].iov_len = 1; -#ifdef PYTHREE - wsgi_req->hvec[2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); - wsgi_req->hvec[2].iov_len = strlen(wsgi_req->hvec[2].iov_base); -#else - wsgi_req->hvec[2].iov_base = PyString_AsString(head); - wsgi_req->hvec[2].iov_len = PyString_Size(head); -#endif - wsgi_req->status = atoi(wsgi_req->hvec[2].iov_base); - wsgi_req->hvec[3].iov_base = nl; - wsgi_req->hvec[3].iov_len = NL_SIZE; - } - else { - // drop http status on cgi mode - base = 3; - wsgi_req->hvec[0].iov_base = "Status: "; - wsgi_req->hvec[0].iov_len = 8; -#ifdef PYTHREE - wsgi_req->hvec[1].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); - wsgi_req->hvec[1].iov_len = strlen(wsgi_req->hvec[1].iov_base); -#else - wsgi_req->hvec[1].iov_base = PyString_AsString(head); - wsgi_req->hvec[1].iov_len = PyString_Size(head); -#endif - wsgi_req->status = atoi(wsgi_req->hvec[1].iov_base); - wsgi_req->hvec[2].iov_base = nl; - wsgi_req->hvec[2].iov_len = NL_SIZE; - } - - - headers = PyTuple_GetItem(args, 1+shift); - if (!headers) { - goto clear; - } - if (!PyList_Check(headers)) { - uwsgi_log( "http headers must be in a python list\n"); - goto clear; - } - wsgi_req->header_cnt = PyList_Size(headers); - - - - if (wsgi_req->header_cnt > uwsgi.max_vars) { - wsgi_req->header_cnt = uwsgi.max_vars; - } - for (i = 0; i < wsgi_req->header_cnt; i++) { - j = (i * 4) + base; - head = PyList_GetItem(headers, i); - if (!head) { - goto clear; - } - if (!PyTuple_Check(head)) { - uwsgi_log( "http header must be defined in a tuple !\n"); - goto clear; - } - h_key = PyTuple_GetItem(head, 0); - if (!h_key) { - goto clear; - } - h_value = PyTuple_GetItem(head, 1); - if (!h_value) { - goto clear; - } -#ifdef PYTHREE - wsgi_req->hvec[j].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_key)); - wsgi_req->hvec[j].iov_len = strlen(wsgi_req->hvec[j].iov_base); -#else - wsgi_req->hvec[j].iov_base = PyString_AsString(h_key); - wsgi_req->hvec[j].iov_len = PyString_Size(h_key); -#endif - wsgi_req->hvec[j + 1].iov_base = h_sep; - wsgi_req->hvec[j + 1].iov_len = H_SEP_SIZE; -#ifdef PYTHREE - wsgi_req->hvec[j + 2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_value)); - wsgi_req->hvec[j + 2].iov_len = strlen(wsgi_req->hvec[j + 2].iov_base); -#else - wsgi_req->hvec[j + 2].iov_base = PyString_AsString(h_value); - wsgi_req->hvec[j + 2].iov_len = PyString_Size(h_value); -#endif - wsgi_req->hvec[j + 3].iov_base = nl; - wsgi_req->hvec[j + 3].iov_len = NL_SIZE; - //uwsgi_log( "%.*s: %.*s\n", wsgi_req->hvec[j].iov_len, (char *)wsgi_req->hvec[j].iov_base, wsgi_req->hvec[j+2].iov_len, (char *) wsgi_req->hvec[j+2].iov_base); - } - - - // \r\n - j = (i * 4) + base; - wsgi_req->hvec[j].iov_base = nl; - wsgi_req->hvec[j].iov_len = NL_SIZE; - - UWSGI_RELEASE_GIL - wsgi_req->headers_size = writev(wsgi_req->poll.fd, wsgi_req->hvec, j + 1); - UWSGI_GET_GIL - if (wsgi_req->headers_size < 0) { - uwsgi_error("writev()"); - } - - Py_INCREF(uwsgi.wsgi_writeout); - - - return uwsgi.wsgi_writeout; - - clear: - - Py_INCREF(Py_None); - return Py_None; -} diff --git a/wsgi_subhandler.c b/wsgi_subhandler.c deleted file mode 100644 index f08d79ce..00000000 --- a/wsgi_subhandler.c +++ /dev/null @@ -1,215 +0,0 @@ -#include "uwsgi.h" - -extern struct uwsgi_server uwsgi; - -void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { - - PyObject *wsgi_socket, *zero; - - static PyObject *uwsgi_version = NULL; - - if (uwsgi_version == NULL) { - uwsgi_version = PyString_FromString(UWSGI_VERSION); - } - - - wsgi_socket = PyFile_FromFile(wsgi_req->async_post, "wsgi_input", "r", NULL); - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.input", wsgi_socket); - Py_DECREF(wsgi_socket); - -#ifdef UWSGI_SENDFILE - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.file_wrapper", wi->wsgi_sendfile); -#endif - -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.readable", wi->wsgi_eventfd_read); - PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.writable", wi->wsgi_eventfd_write); - PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.fdevent.timeout", Py_None); - } -#endif - - zero = PyTuple_New(2); - PyTuple_SetItem(zero, 0, PyInt_FromLong(1)); - PyTuple_SetItem(zero, 1, PyInt_FromLong(0)); - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.version", zero); - Py_DECREF(zero); - - zero = PyFile_FromFile(stderr, "wsgi_input", "w", NULL); - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.errors", zero); - Py_DECREF(zero); - - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.run_once", Py_False); - - if (uwsgi.threads > 1) { - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.multithread", Py_True); - } - else { - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.multithread", Py_False); - } - if (uwsgi.numproc == 1) { - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.multiprocess", Py_False); - } - else { - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.multiprocess", Py_True); - } - - if (wsgi_req->scheme_len > 0) { - zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len); - } - else if (wsgi_req->https_len > 0) { - if (!strncasecmp(wsgi_req->https, "on", 2) || wsgi_req->https[0] == '1') { - zero = PyString_FromString("https"); - } - else { - zero = PyString_FromString("http"); - } - } - else { - zero = PyString_FromString("http"); - } - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.url_scheme", zero); - Py_DECREF(zero); - - - wsgi_req->async_app = wi->wsgi_callable ; - - //PyDict_SetItemString(uwsgi.embedded_dict, "env", wsgi_req->async_environ); - - PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.version", uwsgi_version); - - -#ifdef UWSGI_ROUTING - uwsgi_log("routing %d routes %d\n", uwsgi.routing, uwsgi.nroutes); - if (uwsgi.routing && uwsgi.nroutes > 0) { - check_route(uwsgi, wsgi_req); - } -#endif - - - // call - - - 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 wsgi_request *wsgi_req) { - - PyObject *pychunk ; - ssize_t wsize ; -#ifdef UWSGI_SENDFILE - ssize_t sf_len = 0 ; -#endif - - UWSGI_GET_GIL - - // return or yield ? - if (PyString_Check((PyObject *)wsgi_req->async_result)) { - if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(wsgi_req->async_result), PyString_Size(wsgi_req->async_result))) < 0) { - uwsgi_error("write()"); - goto clear; - } - wsgi_req->response_size += wsize; - goto clear; - } - -#ifdef UWSGI_SENDFILE - if (wsgi_req->sendfile_obj == wsgi_req->async_result && wsgi_req->sendfile_fd != -1) { - sf_len = uwsgi_sendfile(wsgi_req); - if (sf_len < 1) goto clear; - wsgi_req->response_size += sf_len ; -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - if (wsgi_req->response_size < wsgi_req->sendfile_fd_size) { - UWSGI_RELEASE_GIL - return UWSGI_AGAIN; - } - } -#endif - goto clear; - } -#endif - - // ok its a yield - if (!wsgi_req->async_placeholder) { - wsgi_req->async_placeholder = PyObject_GetIter(wsgi_req->async_result); - if (!wsgi_req->async_placeholder) { - goto clear2; - } -#ifdef UWSGI_ASYNC - if (uwsgi.async > 1) { - UWSGI_RELEASE_GIL - return UWSGI_AGAIN; - } -#endif - } - - - - pychunk = PyIter_Next(wsgi_req->async_placeholder) ; - - if (!pychunk) { - if (PyErr_Occurred()) PyErr_Print(); - goto clear; - } - - - - if (PyString_Check(pychunk)) { - if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(pychunk), PyString_Size(pychunk))) < 0) { - uwsgi_error("write()"); - Py_DECREF(pychunk); - goto clear; - } - wsgi_req->response_size += wsize; - } - -#ifdef UWSGI_SENDFILE - else if (wsgi_req->sendfile_obj == pychunk && wsgi_req->sendfile_fd != -1) { - sf_len = uwsgi_sendfile(wsgi_req); - if (sf_len < 1) goto clear; - wsgi_req->response_size += sf_len ; - } -#endif - - Py_DECREF(pychunk); - UWSGI_RELEASE_GIL - return UWSGI_AGAIN ; - -clear: - if (wsgi_req->sendfile_fd != -1) { - Py_DECREF((PyObject *)wsgi_req->async_sendfile); - } - if (wsgi_req->async_environ) { - PyDict_Clear(wsgi_req->async_environ); - } - if (wsgi_req->async_post && !wsgi_req->fd_closed) { - fclose(wsgi_req->async_post); - if (!uwsgi.post_buffering || wsgi_req->post_cl <= (size_t) uwsgi.post_buffering) { - wsgi_req->fd_closed = 1 ; - - } - } - Py_XDECREF((PyObject *)wsgi_req->async_placeholder); -clear2: - Py_DECREF((PyObject *)wsgi_req->async_result); - PyErr_Clear(); - -#ifdef UWSGI_DEBUG - if (wsgi_req->async_placeholder) { - uwsgi_debug("wsgi_req->async_placeholder: %d\n", ((PyObject *)wsgi_req->async_placeholder)->ob_refcnt); - } - if (wsgi_req->async_result) { - uwsgi_debug("wsgi_req->async_result: %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt); - } - if (wsgi_req->async_app) { - uwsgi_debug("wsgi_req->async_app: %d\n", ((PyObject *)wsgi_req->async_app)->ob_refcnt); - } -#endif - - UWSGI_RELEASE_GIL - return UWSGI_OK; -} - - diff --git a/xmlconf.c b/xmlconf.c index 634c4888..ec1a62cc 100644 --- a/xmlconf.c +++ b/xmlconf.c @@ -181,10 +181,12 @@ next: } else if (!strcmp((char *) node2->name, "callable")) { + /* if (!uwsgi.pyloader_dict) { uwsgi_log( "no module loaded in memory. skip.\n"); continue; } + */ if (!node2->children) { uwsgi_log( "no callable defined. skip.\n"); continue; @@ -194,7 +196,7 @@ next: continue; } - init_uwsgi_app(LOADER_STRING_CALLABLE, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); + //init_uwsgi_app(LOADER_STRING_CALLABLE, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); } else if (!strcmp((char *) node2->name, "eval")) { if (!node2->children) { @@ -206,7 +208,7 @@ next: continue; } - init_uwsgi_app(LOADER_EVAL, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); + //init_uwsgi_app(LOADER_EVAL, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); } else if (!strcmp((char *) node2->name, "file")) { @@ -219,7 +221,7 @@ next: continue; } - init_uwsgi_app(LOADER_FILE, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); + //init_uwsgi_app(LOADER_FILE, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); } else if (!strcmp((char *) node2->name, "module")) { @@ -232,7 +234,7 @@ next: continue; } - init_uwsgi_app(LOADER_UWSGI, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); + //init_uwsgi_app(LOADER_UWSGI, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); } else if (!strcmp((char *) node2->name, "script")) { if (!node2->children) { @@ -243,7 +245,7 @@ next: uwsgi_log( "no script defined. skip.\n"); continue; } - init_uwsgi_app(LOADER_UWSGI, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); + //init_uwsgi_app(LOADER_UWSGI, (void *) node2->children->content, wsgi_req, uwsgi.single_interpreter-1); } } } @@ -378,7 +380,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(&uwsgi, NULL); + //init_uwsgi_app(&uwsgi, NULL); current_xmlnode = 0; } };