diff --git a/ini.c b/ini.c index 63eccef5..6f6a4bd0 100644 --- a/ini.c +++ b/ini.c @@ -136,6 +136,11 @@ void uwsgi_ini_config(char *file, char *magic_table[]) { add_exported_option(0, (char *)key); } else { + if (aopt->has_arg == optional_argument) { + if (!strcmp("true", val)) { + val = NULL; + } + } manage_opt(aopt->val, val); } } diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index 11ec3452..a62ec161 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -9,6 +9,8 @@ extern struct uwsgi_server uwsgi; extern struct uwsgi_python up; +extern char **environ; + #ifdef UWSGI_SENDFILE PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}}; #endif @@ -79,6 +81,51 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre // Initialize a new environment for the new interpreter + // reload "os" environ to allow dynamic setenv() + if (up.reload_os_env) { + + char **e, *p; + PyObject *k, *env_value; + + PyObject *os_module = PyImport_ImportModule("os"); + if (os_module) { + PyObject *os_module_dict = PyModule_GetDict(os_module); + PyObject *py_environ = PyDict_GetItemString(os_module_dict, "environ"); + if (py_environ) { + for (e = environ; *e != NULL; e++) { + p = strchr(*e, '='); + if (p == NULL) continue; + + k = PyString_FromStringAndSize(*e, (int)(p-*e)); + if (k == NULL) { + PyErr_Print(); + continue; + } + + env_value = PyString_FromString(p+1); + if (env_value == NULL) { + PyErr_Print(); + Py_DECREF(k); + continue; + } + + uwsgi_log("%s = %s\n", PyString_AsString(k), PyString_AsString(env_value)); + + if (PyObject_SetItem(py_environ, k, env_value)) { + uwsgi_log("cazzo\n"); + PyErr_Print(); + } + + Py_DECREF(k); + Py_DECREF(env_value); + + } + + } + } + } + + if (interpreter == NULL && id) { wi->interpreter = Py_NewInterpreter(); if (!wi->interpreter) { diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 605adc52..8e4fddae 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -26,6 +26,7 @@ struct option uwsgi_python_options[] = { {"catch-exceptions", no_argument, &up.catch_exceptions, 1}, {"ignore-script-name", no_argument, &up.ignore_script_name, 1}, {"pep3333-input", no_argument, &up.pep3333_input, 1}, + {"reload-os-env", no_argument, &up.reload_os_env, 1}, {"no-site", no_argument, &Py_NoSiteFlag, 1}, {0, 0, 0, 0}, diff --git a/plugins/python/pyutils.c b/plugins/python/pyutils.c index 21a188a5..c1fb2e17 100644 --- a/plugins/python/pyutils.c +++ b/plugins/python/pyutils.c @@ -96,4 +96,6 @@ void init_pyargv() { } PySys_SetArgv(up.argc, up.py_argv); - } + + +} diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 5bf263d7..ac5d37ad 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -11,6 +11,7 @@ #define LONG_ARGS_PASTE LONG_ARGS_PYTHON_BASE + 2 #define LONG_ARGS_PYARGV LONG_ARGS_PYTHON_BASE + 3 #define LONG_ARGS_PYMODULE_ALIAS LONG_ARGS_PYTHON_BASE + 4 +#define LONG_ARGS_RELOAD_OS_ENV LONG_ARGS_PYTHON_BASE + 5 #ifdef UWSGI_STACKLESS #include @@ -134,6 +135,8 @@ struct uwsgi_python { int pep3333_input; void (*extension)(void); + + int reload_os_env; }; diff --git a/protocol.c b/protocol.c index 14afc9aa..628c1a22 100644 --- a/protocol.c +++ b/protocol.c @@ -512,6 +512,17 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { wsgi_req->chdir = ptrbuf; wsgi_req->chdir_len = strsize; } + else if (!uwsgi_strncmp("UWSGI_SETENV", 12, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { + char *env_value = memchr(ptrbuf, '=', strsize); + if (env_value) { + env_value[0] = 0; + env_value = uwsgi_concat2n(env_value+1, strsize-((env_value+1)-ptrbuf), "", 0); + if (setenv(ptrbuf, env_value, 1)) { + uwsgi_error("setenv()"); + } + free(env_value); + } + } else if (!uwsgi_strncmp("SERVER_NAME", 11, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len) && !uwsgi.vhost_host) { wsgi_req->host = ptrbuf; wsgi_req->host_len = strsize; diff --git a/yaml.c b/yaml.c index b94bcfc6..7459ea6c 100644 --- a/yaml.c +++ b/yaml.c @@ -181,6 +181,11 @@ void uwsgi_yaml_config(char *file, char *magic_table[]) { add_exported_option(0, (char *)key); } else { + if (aopt->has_arg == optional_argument) { + if (!strcmp("true", val)) { + val = NULL; + } + } manage_opt(aopt->val, val); } }