int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThreadState *interpreter, int app_type) { PyObject *app_list = NULL, *applications = NULL; if (uwsgi_apps_cnt >= uwsgi.max_apps) { uwsgi_log("ERROR: you cannot load more than %d apps in a worker\n", uwsgi.max_apps); return -1; } int id = uwsgi_apps_cnt; int multiapp = 0; int i; struct uwsgi_app *wi; time_t now = uwsgi_now(); if (uwsgi_get_app_id(NULL, wsgi_req->appid, wsgi_req->appid_len, -1) != -1) { uwsgi_log( "mountpoint %.*s already configured. skip.\n", wsgi_req->appid_len, wsgi_req->appid); return -1; } wi = &uwsgi_apps[id]; memset(wi, 0, sizeof(struct uwsgi_app)); wi->mountpoint_len = wsgi_req->appid_len < 0xff ? wsgi_req->appid_len : (0xff-1); strncpy(wi->mountpoint, wsgi_req->appid, wi->mountpoint_len); // dynamic chdir ? if (wsgi_req->chdir_len > 0) { strncpy(wi->chdir, wsgi_req->chdir, wsgi_req->chdir_len < 0xff ? wsgi_req->chdir_len : (0xff-1)); #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 // 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; } #ifdef UWSGI_DEBUG uwsgi_log("%s = %s\n", PyString_AsString(k), PyString_AsString(env_value)); #endif if (PyObject_SetItem(py_environ, k, env_value)) { PyErr_Print(); } Py_DECREF(k); Py_DECREF(env_value); } } } } if (interpreter == NULL && 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(); // we need to inizialize an embedded module for every interpreter init_uwsgi_embedded_module(); init_uwsgi_vars(); } else if (interpreter) { wi->interpreter = interpreter; } else { wi->interpreter = up.main_thread; } if (wsgi_req->home_len) { set_dyn_pyhome(wsgi_req->home, wsgi_req->home_len); } if (wsgi_req->touch_reload_len > 0 && wsgi_req->touch_reload_len < 0xff) { struct stat trst; strncpy(wi->touch_reload, wsgi_req->touch_reload, wsgi_req->touch_reload_len); if (!stat(wi->touch_reload, &trst)) { wi->touch_reload_mtime = trst.st_mtime; } } wi->callable = up.loaders[loader](arg1); if (!wi->callable) { uwsgi_log("unable to load app %d (mountpoint='%s') (callable not found or import error)\n", id, wi->mountpoint); goto doh; } // the module contains multiple apps if (PyDict_Check((PyObject *)wi->callable)) { applications = wi->callable; uwsgi_log("found a multiapp module...\n"); app_list = PyDict_Keys(applications); multiapp = PyList_Size(app_list); if (multiapp < 1) { uwsgi_log("you have to define at least one app in the apllications dictionary\n"); goto doh; } PyObject *app_mnt = PyList_GetItem(app_list, 0); if (!PyString_Check(app_mnt)) { uwsgi_log("the app mountpoint must be a string\n"); goto doh; } char *tmp_mountpoint = PyString_AsString(app_mnt); wi->mountpoint_len = strlen(wi->mountpoint) < 0xff ? strlen(wi->mountpoint) : (0xff-1); strncpy(wi->mountpoint, tmp_mountpoint, wi->mountpoint_len); wsgi_req->appid = wi->mountpoint; wsgi_req->appid_len = wi->mountpoint_len; #ifdef UWSGI_DEBUG uwsgi_log("main mountpoint = %s\n", wi->mountpoint); #endif wi->callable = PyDict_GetItem(applications, app_mnt); if (PyString_Check((PyObject *) wi->callable)) { PyObject *callables_dict = get_uwsgi_pydict((char *)arg1); if (callables_dict) { wi->callable = PyDict_GetItem(callables_dict, (PyObject *)wi->callable); } } } Py_INCREF((PyObject *)wi->callable); wi->environ = malloc(sizeof(PyObject*)*uwsgi.cores); if (!wi->environ) { uwsgi_error("malloc()"); exit(1); } for(i=0;i<uwsgi.cores;i++) { wi->environ[i] = PyDict_New(); if (!wi->environ[i]) { uwsgi_log("unable to allocate new env dictionary for app\n"); exit(1); } } wi->argc = 1; if (app_type == PYTHON_APP_TYPE_WSGI) { #ifdef UWSGI_DEBUG uwsgi_log("-- WSGI callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_wsgi; wi->response_subhandler = uwsgi_response_subhandler_wsgi; wi->argc = 2; } else if (app_type == PYTHON_APP_TYPE_WEB3) { #ifdef UWSGI_DEBUG uwsgi_log("-- Web3 callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_web3; wi->response_subhandler = uwsgi_response_subhandler_web3; } else if (app_type == PYTHON_APP_TYPE_PUMP) { #ifdef UWSGI_DEBUG uwsgi_log("-- Pump callable selected --\n"); #endif wi->request_subhandler = uwsgi_request_subhandler_pump; wi->response_subhandler = uwsgi_response_subhandler_pump; } wi->args = malloc(sizeof(PyObject*)*uwsgi.cores); if (!wi->args) { uwsgi_error("malloc()"); exit(1); } for(i=0;i<uwsgi.cores;i++) { wi->args[i] = PyTuple_New(wi->argc); if (!wi->args[i]) { uwsgi_log("unable to allocate new tuple for app args\n"); exit(1); } // add start_response on WSGI app Py_INCREF((PyObject *)up.wsgi_spitout); if (app_type == PYTHON_APP_TYPE_WSGI) { if (PyTuple_SetItem(wi->args[i], 1, up.wsgi_spitout)) { uwsgi_log("unable to set start_response in args tuple\n"); exit(1); } } } if (app_type == PYTHON_APP_TYPE_WSGI) { // prepare sendfile() for WSGI app wi->sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL); wi->eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL); wi->eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL); } // cache most used values wi->error = PyFile_FromFile(stderr, "wsgi_errors", "w", NULL); Py_INCREF((PyObject *)wi->error); wi->gateway_version = PyTuple_New(2); PyTuple_SetItem(wi->gateway_version, 0, PyInt_FromLong(1)); PyTuple_SetItem(wi->gateway_version, 1, PyInt_FromLong(0)); Py_INCREF((PyObject *)wi->gateway_version); wi->uwsgi_version = PyString_FromString(UWSGI_VERSION); Py_INCREF((PyObject *)wi->uwsgi_version); wi->uwsgi_node = PyString_FromString(uwsgi.hostname); Py_INCREF((PyObject *)wi->uwsgi_node); if (uwsgi.threads > 1 && id) { // if we have multiple threads we need to initialize a PyThreadState for each one for(i=0;i<uwsgi.threads;i++) { //uwsgi_log("%p\n", uwsgi.core[i]->ts[id]); uwsgi.workers[uwsgi.mywid].cores[i].ts[id] = PyThreadState_New( ((PyThreadState *)wi->interpreter)->interp); if (!uwsgi.workers[uwsgi.mywid].cores[i].ts[id]) { uwsgi_log("unable to allocate new PyThreadState structure for app %s", wi->mountpoint); goto doh; } } PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key) ); } else if (interpreter == NULL && id) { PyThreadState_Swap(up.main_thread); } const char *default_app = ""; if ((wsgi_req->appid_len == 0 || (wsgi_req->appid_len = 1 && wsgi_req->appid[0] == '/')) && uwsgi.default_app == -1) { default_app = " (default app)" ; uwsgi.default_app = id; } wi->started_at = now; wi->startup_time = uwsgi_now() - now; if (app_type == PYTHON_APP_TYPE_WSGI) { uwsgi_log( "WSGI app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } else if (app_type == PYTHON_APP_TYPE_WEB3) { uwsgi_log( "Web3 app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } else if (app_type == PYTHON_APP_TYPE_PUMP) { uwsgi_log( "Pump app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app); } uwsgi_apps_cnt++; if (multiapp > 1) { for(i=1;i<multiapp;i++) { PyObject *app_mnt = PyList_GetItem(app_list, i); if (!PyString_Check(app_mnt)) { uwsgi_log("applications dictionary key must be a string, skipping.\n"); continue; } wsgi_req->appid = PyString_AsString(app_mnt); wsgi_req->appid_len = strlen(wsgi_req->appid); PyObject *a_callable = PyDict_GetItem(applications, app_mnt); if (PyString_Check(a_callable)) { PyObject *callables_dict = get_uwsgi_pydict((char *)arg1); if (callables_dict) { a_callable = PyDict_GetItem(callables_dict, a_callable); } } if (!a_callable) { uwsgi_log("skipping broken app %s\n", wsgi_req->appid); continue; } init_uwsgi_app(LOADER_CALLABLE, a_callable, wsgi_req, wi->interpreter, app_type); } } // emulate COW uwsgi_emulate_cow_for_apps(id); return id; doh: if (PyErr_Occurred()) PyErr_Print(); if (interpreter == NULL && id) { Py_EndInterpreter(wi->interpreter); if (uwsgi.threads > 1) { PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key)); } else { PyThreadState_Swap(up.main_thread); } } return -1; }
void manage_snmp(int fd, uint8_t * buffer, int size, struct sockaddr_in *client_addr) { uint16_t asnlen; uint16_t oidlen; uint8_t oid_part[2]; int ptrdelta; uint8_t *ptr = buffer, *seq1, *seq2, *seq3; uint8_t community_len; uint64_t snmp_int = 0; uint64_t request_id = 0; uint64_t version = 0; // KISS for memory management if (size > SNMP_WATERMARK) return; ptr++; // check total sequence size if (*ptr > SNMP_WATERMARK || *ptr < 13) return; ptr++; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP packet size: %d\n", size); #endif // check snmp version if (*ptr != SNMP_INTEGER) return; ptr++; ptrdelta = get_snmp_integer(ptr, &version); if (version > 2) return; ptr += ptrdelta; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP version: %d\n", version); #endif // check for community string (this must be set from the python vm using uwsgi.snmp_community or with --snmp-community arg) if (*ptr != SNMP_STRING) return; ptr++; community_len = *ptr; if (community_len > 72 || community_len < 1) return; ptr++; // check for community string if (strlen(uwsgi.shared->snmp_community) != community_len) return; if (memcmp(ptr, uwsgi.shared->snmp_community, community_len)) return; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP community: %.*s\n", community_len, ptr); #endif ptr += community_len; // check for get request if (*ptr != SNMP_GET) return; *ptr = SNMP_RES; ptr++; seq1 = ptr; if (*ptr != ((size - community_len) - 9)) return; ptr++; // get request_id if (*ptr != SNMP_INTEGER) return; ptr++; ptrdelta = get_snmp_integer(ptr, &request_id); if (ptrdelta <= 0) return; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP request id: %d %p\n", request_id, ptr); #endif // check here if (ptr + ptrdelta >= buffer + size) return; ptr += ptrdelta; // get error if (*ptr != SNMP_INTEGER) return; ptr++; snmp_int = 0; ptrdelta = get_snmp_integer(ptr, &snmp_int); if (ptrdelta <= 0) return; if (ptr + ptrdelta >= buffer + size) return; if (snmp_int != 0) return; ptr += ptrdelta; // get index if (*ptr != SNMP_INTEGER) return; ptr++; snmp_int = 0; ptrdelta = get_snmp_integer(ptr, &snmp_int); if (ptrdelta <= 0) return; if (ptr + ptrdelta >= buffer + size) return; if (snmp_int != 0) return; ptr += ptrdelta; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP int [1]: %d\n", snmp_int); #endif // check for sequence if (*ptr != SNMP_SEQUENCE) return; ptr++; if (*ptr > SNMP_WATERMARK) return; seq2 = ptr; ptr++; // now the interesting stuff: OID management if (*ptr != SNMP_SEQUENCE) return; ptr++; // check for normal OID uWSGI size: |1.3|.6|.1|.4|.1.|35156|.17|.1/2|.x| + OID_NULL asnlen = *ptr; if (asnlen < 15) return; seq3 = ptr; ptr++; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP ASN len: %d\n", asnlen); #endif // is it an OID ? if (*ptr != SNMP_OID) return; ptr++; oidlen = *ptr; if (oidlen != 11) return; ptr++; // and now parse the OID !!! if (memcmp(ptr, SNMP_UWSGI_BASE, 9)) return; ptr += 9; oid_part[0] = *ptr; if (oid_part[0] != 1 && oid_part[0] != 2) return; ptr++; oid_part[1] = *ptr; if (oid_part[1] < 1 || oid_part[1] > 100) return; ptr++; // check for null if (memcmp((char *) ptr, "\x05\x00", 2)) return; ptr += 2; size = build_snmp_response(oid_part[0], oid_part[1], buffer, size, seq1, seq2, seq3); if (size > 0) { if (sendto(fd, buffer, size, 0, (struct sockaddr *) client_addr, sizeof(struct sockaddr_in)) < 0) { uwsgi_error("sendto()"); } } }
static int get_snmp_integer(uint8_t * ptr, uint64_t * val) { uint16_t tlen; int i, j; uint8_t *cval = (uint8_t *) val; tlen = *ptr; if (tlen > 4) return -1; #ifdef UWSGI_DEBUG uwsgi_debug("SNMP get integer TLEN %d %p\n", tlen, ptr); #endif j = 0; #ifdef __BIG_ENDIAN__ for (i = 0; i < tlen; i++) { #else for (i = tlen - 1; i >= 0; i--) { #endif cval[j] = ptr[1 + i]; j++; } return tlen + 1; } static uint8_t snmp_int_to_snmp(uint64_t snmp_val, uint8_t oid_type, uint8_t * buffer) { uint8_t tlen; int i, j; uint8_t *ptr = (uint8_t *) & snmp_val; // check for counter, counter64 or gauge if (oid_type == SNMP_COUNTER64) { tlen = 8; } else if (oid_type == SNMP_NULL || oid_type == 0) { tlen = 0; } else { tlen = 4; } buffer[0] = tlen; j = 1; #ifdef __BIG_ENDIAN__ for (i = 0; i < tlen; i++) { #else for (i = tlen - 1; i >= 0; i--) { #endif buffer[j] = ptr[i]; j++; } return tlen + 1; } static ssize_t build_snmp_response(uint8_t oid1, uint8_t oid2, uint8_t * buffer, int size, uint8_t * seq1, uint8_t * seq2, uint8_t * seq3) { uint64_t snmp_val; uint8_t oid_sz; uint8_t oid_type; if (oid1 == 1) { snmp_val = get_uwsgi_snmp_value(oid2, &oid_type); } else if (oid1 == 2) { snmp_val = get_uwsgi_custom_snmp_value(oid2, &oid_type); } else { return -1; } buffer[size - 2] = oid_type; oid_sz = snmp_int_to_snmp(snmp_val, oid_type, buffer + (size - 1)); if (oid_sz < 1) return -1; oid_sz--; buffer[1] += oid_sz; *seq1 += oid_sz; *seq2 += oid_sz; *seq3 += oid_sz; return size + oid_sz; }
void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { PyObject *zero; int i; PyObject *pydictkey, *pydictvalue; char *path_info; 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, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); #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) { wsgi_req->uh.modifier1 = 0; 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 async_post is mapped as a file, directly use it as wsgi.input if (wsgi_req->async_post) { #ifdef PYTHREE wsgi_req->async_input = PyFile_FromFd(fileno((FILE *)wsgi_req->async_post), "web3_input", "rb", 0, NULL, NULL, NULL, 0); #else wsgi_req->async_input = PyFile_FromFile(wsgi_req->async_post, "web3_input", "r", NULL); #endif } else { // create wsgi.input custom object wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType); ((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req; ((uwsgi_Input*)wsgi_req->async_input)->pos = 0; ((uwsgi_Input*)wsgi_req->async_input)->readline_pos = 0; ((uwsgi_Input*)wsgi_req->async_input)->readline_max_size = 0; } PyDict_SetItemString(wsgi_req->async_environ, "web3.input", wsgi_req->async_input); PyDict_SetItemString(wsgi_req->async_environ, "web3.version", wi->gateway_version); 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 = UWSGI_PYFROMSTRINGSIZE(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 = UWSGI_PYFROMSTRING("https"); } else { zero = UWSGI_PYFROMSTRING("http"); } } else { zero = UWSGI_PYFROMSTRING("http"); } PyDict_SetItemString(wsgi_req->async_environ, "web3.url_scheme", zero); Py_DECREF(zero); wsgi_req->async_app = wi->callable; // export .env only in non-threaded mode if (uwsgi.threads < 2) { PyDict_SetItemString(up.embedded_dict, "env", wsgi_req->async_environ); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.version", wi->uwsgi_version); if (uwsgi.cores > 1) { PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.core", PyInt_FromLong(wsgi_req->async_id)); } // cache this ? if (uwsgi.cluster_fd >= 0) { zero = PyString_FromString(uwsgi.cluster); PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster", zero); Py_DECREF(zero); zero = PyString_FromString(uwsgi.hostname); PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.cluster_node", zero); Py_DECREF(zero); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node); // call PyTuple_SetItem(wsgi_req->async_args, 0, wsgi_req->async_environ); return python_call(wsgi_req->async_app, wsgi_req->async_args, uwsgi.catch_exceptions, wsgi_req); }
void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { PyObject *zero; int i; PyObject *pydictkey, *pydictvalue; char *path_info; 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, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); #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) { wsgi_req->uh->modifier1 = python_plugin.modifier1; 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))); } } } } } // create wsgi.input custom object wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType); ((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req; PyDict_SetItemString(wsgi_req->async_environ, "web3.input", wsgi_req->async_input); PyDict_SetItemString(wsgi_req->async_environ, "web3.version", wi->gateway_version); 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 = UWSGI_PYFROMSTRINGSIZE(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 = UWSGI_PYFROMSTRING("https"); } else { zero = UWSGI_PYFROMSTRING("http"); } } else { zero = UWSGI_PYFROMSTRING("http"); } PyDict_SetItemString(wsgi_req->async_environ, "web3.url_scheme", zero); Py_DECREF(zero); wsgi_req->async_app = wi->callable; // export .env only in non-threaded mode if (uwsgi.threads < 2) { PyDict_SetItemString(up.embedded_dict, "env", wsgi_req->async_environ); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.version", wi->uwsgi_version); if (uwsgi.cores > 1) { PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.core", PyInt_FromLong(wsgi_req->async_id)); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node); // call if (PyTuple_GetItem(wsgi_req->async_args, 0) != wsgi_req->async_environ) { if (PyTuple_SetItem(wsgi_req->async_args, 0, wsgi_req->async_environ)) { uwsgi_log_verbose("unable to set environ to the python application callable, consider using the holy env allocator\n"); return NULL; } } return python_call(wsgi_req->async_app, wsgi_req->async_args, uwsgi.catch_exceptions, wsgi_req); }
void *uwsgi_request_subhandler_pump(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { PyObject *zero; int i; PyObject *pydictkey, *pydictvalue; char *port = memchr(wsgi_req->host, ':', wsgi_req->host_len); if (port) { zero = PyString_FromStringAndSize(wsgi_req->host, (port-wsgi_req->host)); PyDict_SetItemString(wsgi_req->async_environ, "server_name", zero); Py_DECREF(zero); zero = PyString_FromStringAndSize(port, wsgi_req->host_len-((port+1)-wsgi_req->host)); PyDict_SetItemString(wsgi_req->async_environ, "server_port", zero); Py_DECREF(zero); } else { zero = PyString_FromStringAndSize(wsgi_req->host, wsgi_req->host_len); PyDict_SetItemString(wsgi_req->async_environ, "server_name", zero); Py_DECREF(zero); zero = PyString_FromStringAndSize("80", 2); PyDict_SetItemString(wsgi_req->async_environ, "server_port", zero); Py_DECREF(zero); } zero = PyString_FromStringAndSize(wsgi_req->remote_addr, wsgi_req->remote_addr_len); PyDict_SetItemString(wsgi_req->async_environ, "remote_addr", zero); Py_DECREF(zero); zero = PyString_FromStringAndSize(wsgi_req->path_info, wsgi_req->path_info_len); PyDict_SetItemString(wsgi_req->async_environ, "uri", zero); Py_DECREF(zero); if (wsgi_req->query_string_len > 0) { zero = PyString_FromStringAndSize(wsgi_req->query_string, wsgi_req->query_string_len); PyDict_SetItemString(wsgi_req->async_environ, "query_string", zero); Py_DECREF(zero); } zero = PyString_FromStringAndSize(uwsgi_lower(wsgi_req->method, wsgi_req->method_len), wsgi_req->method_len); PyDict_SetItemString(wsgi_req->async_environ, "method", zero); Py_DECREF(zero); if (wsgi_req->post_cl > 0) { PyDict_SetItemString(wsgi_req->async_environ, "content_length", PyInt_FromLong(wsgi_req->post_cl)); if (wsgi_req->content_type_len > 0) { zero = PyString_FromStringAndSize(wsgi_req->content_type, wsgi_req->content_type_len); PyDict_SetItemString(wsgi_req->async_environ, "content_type", zero); Py_DECREF(zero); } } PyObject *headers = PyDict_New(); 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 if (wsgi_req->hvec[i].iov_len < 6) continue; if (!uwsgi_startswith(wsgi_req->hvec[i].iov_base, "HTTP_", 5)) { (void) uwsgi_lower(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5); #ifdef PYTHREE pydictkey = PyUnicode_DecodeLatin1(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); #else pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5); pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len); #endif PyObject *old_value = PyDict_GetItem(headers, pydictkey); if (old_value) { if (PyString_Check(old_value)) { PyObject *new_value = PyList_New(0); PyList_Append(new_value, old_value); old_value = new_value; PyDict_SetItem(headers, pydictkey, old_value); Py_DECREF(old_value); } PyList_Append(old_value, pydictvalue); } else { PyDict_SetItem(headers, pydictkey, pydictvalue); } Py_DECREF(pydictkey); Py_DECREF(pydictvalue); } } PyDict_SetItemString(wsgi_req->async_environ, "headers", headers); Py_DECREF(headers); // create wsgi.input custom object wsgi_req->async_input = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType); ((uwsgi_Input*)wsgi_req->async_input)->wsgi_req = wsgi_req; PyDict_SetItemString(wsgi_req->async_environ, "body", wsgi_req->async_input); if (wsgi_req->scheme_len > 0) { zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len); } 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, "scheme", zero); Py_DECREF(zero); wsgi_req->async_app = wi->callable; // export .env only in non-threaded mode if (uwsgi.threads < 2) { PyDict_SetItemString(up.embedded_dict, "env", wsgi_req->async_environ); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.version", wi->uwsgi_version); if (uwsgi.cores > 1) { PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.core", PyInt_FromLong(wsgi_req->async_id)); } PyDict_SetItemString(wsgi_req->async_environ, "uwsgi.node", wi->uwsgi_node); // 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, wsgi_req); }
void uwsgi_ldap_config(char *url) { LDAP *ldp; LDAPMessage *results, *entry; BerElement *ber; struct berval **bervalues; char *attr; char *uwsgi_attr; char *url_slash; int desired_version = LDAP_VERSION3; int ret; LDAPURLDesc *ldap_url; if (!ldap_is_ldap_url(url)) { uwsgi_log("invalid LDAP url.\n"); exit(1); } if (ldap_url_parse(url, &ldap_url) != LDAP_SUCCESS) { uwsgi_log("unable to parse LDAP url.\n"); exit(1); } uwsgi_log("[uWSGI] getting LDAP configuration from %s\n", url); url_slash = strchr(url, '/'); url_slash = strchr(url_slash + 1, '/'); url_slash = strchr(url_slash + 1, '/'); if (url_slash) { url_slash[0] = 0; } #ifdef UWSGI_DEBUG uwsgi_debug("LDAP URL: %s\n", url); uwsgi_debug("LDAP BASE DN: %s\n", ldap_url->lud_dn); #endif #if LDAP_API_VERSION >= 3000 if ((ret = ldap_initialize(&ldp, url)) != LDAP_SUCCESS) { uwsgi_log("LDAP: %s\n", ldap_err2string(ret)); exit(1); } #else if ((ldp = ldap_init(ldap_url->lud_host, ldap_url->lud_port)) == NULL) { uwsgi_error("ldap_init()"); exit(1); } #endif if ((ret = ldap_set_option(ldp, LDAP_OPT_PROTOCOL_VERSION, &desired_version)) != LDAP_OPT_SUCCESS) { uwsgi_log("LDAP: %s\n", ldap_err2string(ret)); exit(1); } if ((ret = ldap_search_ext_s(ldp, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter, NULL, 0, NULL, NULL, NULL, 1, &results)) != LDAP_SUCCESS) { uwsgi_log("LDAP: %s\n", ldap_err2string(ret)); exit(1); } #ifdef UWSGI_DEBUG uwsgi_debug("LDAP connection initialized %p\n", ldp); #endif free(ldap_url); if (ldap_count_entries(ldp, results) < 1) { uwsgi_log("no LDAP entry found\n"); exit(1); } entry = ldap_first_entry(ldp, results); int found = 0; for (attr = ldap_first_attribute(ldp, entry, &ber); attr != NULL; attr = ldap_next_attribute(ldp, entry, ber)) { if (!strncmp(attr, "uWSGI", 5)) { found = 1; uwsgi_attr = malloc(calc_ldap_name(attr) + 1); if (!uwsgi_attr) { uwsgi_error("malloc()"); exit(1); } ldap2uwsgi(attr + 5, uwsgi_attr); #ifdef UWSGI_DEBUG uwsgi_debug("LDAP attribute: %s = --%s\n", attr, uwsgi_attr); #endif bervalues = ldap_get_values_len(ldp, entry, attr); if (bervalues) { // do not free uwsgi_attr/uwsgi_val; char *uwsgi_val = malloc(bervalues[0]->bv_len + 1); if (!uwsgi_val) { uwsgi_error("malloc()"); exit(1); } memcpy(uwsgi_val, bervalues[0]->bv_val, bervalues[0]->bv_len); uwsgi_val[bervalues[0]->bv_len] = 0; add_exported_option((char *) uwsgi_attr, uwsgi_val, 0); free(bervalues); } else { free(uwsgi_attr); } } free(attr); } if (!found) { uwsgi_log("no uWSGI LDAP entry found\n"); exit(1); } free(ber); free(results); ldap_unbind_ext_s(ldp, NULL, NULL); }
int uwsgi_proto_uwsgi_parser(struct wsgi_request *wsgi_req) { uint8_t *hdr_buf = (uint8_t *) & wsgi_req->uh; ssize_t len; struct iovec iov[1]; struct cmsghdr *cmsg; if (wsgi_req->proto_parser_status == PROTO_STATUS_RECV_HDR) { if (wsgi_req->proto_parser_pos > 0) { len = read(wsgi_req->poll.fd, hdr_buf + wsgi_req->proto_parser_pos, 4 - wsgi_req->proto_parser_pos); } else { iov[0].iov_base = hdr_buf; iov[0].iov_len = 4; wsgi_req->msg.msg_name = NULL; wsgi_req->msg.msg_namelen = 0; wsgi_req->msg.msg_iov = iov; wsgi_req->msg.msg_iovlen = 1; wsgi_req->msg.msg_control = &wsgi_req->msg_control; wsgi_req->msg.msg_controllen = sizeof(wsgi_req->msg_control); wsgi_req->msg.msg_flags = 0; len = recvmsg(wsgi_req->poll.fd, &wsgi_req->msg, 0); } if (len <= 0) { uwsgi_error("read()"); return -1; } wsgi_req->proto_parser_pos += len; // header ready ? if (wsgi_req->proto_parser_pos == 4) { wsgi_req->proto_parser_status = PROTO_STATUS_RECV_VARS; wsgi_req->proto_parser_pos = 0; /* big endian ? */ #ifdef __BIG_ENDIAN__ wsgi_req->uh.pktsize = uwsgi_swap16(wsgi_req->uh.pktsize); #endif #ifdef UWSGI_DEBUG uwsgi_debug("uwsgi payload size: %d (0x%X) modifier1: %d modifier2: %d\n", wsgi_req->uh.pktsize, wsgi_req->uh.pktsize, wsgi_req->uh.modifier1, wsgi_req->uh.modifier2); #endif /* check for max buffer size */ if (wsgi_req->uh.pktsize > uwsgi.buffer_size) { uwsgi_log("invalid request block size: %d (max %d)...skip\n", wsgi_req->uh.pktsize, uwsgi.buffer_size); return -1; } if (!wsgi_req->uh.pktsize) return UWSGI_OK; } return UWSGI_AGAIN; } else if (wsgi_req->proto_parser_status == PROTO_STATUS_RECV_VARS) { len = read(wsgi_req->poll.fd, wsgi_req->buffer + wsgi_req->proto_parser_pos, wsgi_req->uh.pktsize - wsgi_req->proto_parser_pos); if (len <= 0) { uwsgi_error("read()"); return -1; } wsgi_req->proto_parser_pos += len; // body ready ? if (wsgi_req->proto_parser_pos >= wsgi_req->uh.pktsize) { // older OSX versions make mess with CMSG_FIRSTHDR #ifdef __APPLE__ if (!wsgi_req->msg.msg_controllen) return UWSGI_OK; #endif if (uwsgi.no_fd_passing) return UWSGI_OK; cmsg = CMSG_FIRSTHDR(&wsgi_req->msg); while (cmsg != NULL) { if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type && SCM_RIGHTS) { // upgrade connection to the new socket #ifdef UWSGI_DEBUG uwsgi_log("upgrading fd %d to ", wsgi_req->poll.fd); #endif close(wsgi_req->poll.fd); memcpy(&wsgi_req->poll.fd, CMSG_DATA(cmsg), sizeof(int)); #ifdef UWSGI_DEBUG uwsgi_log("%d\n", wsgi_req->poll.fd); #endif } cmsg = CMSG_NXTHDR(&wsgi_req->msg, cmsg); } return UWSGI_OK; } return UWSGI_AGAIN; } // never here return -1; }