From 5d993fbfc08f2edb7af424d65ee3a5fcf6992ea6 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Tue, 9 Aug 2011 08:20:12 +0200 Subject: [PATCH] implemented UWSGI_APPID var --- plugins/python/pyloader.c | 35 +++-------- plugins/python/wsgi_handlers.c | 107 ++++++++++++--------------------- protocol.c | 8 +++ tests/multiapp.txt | 23 +++++++ utils.c | 8 ++- uwsgi.h | 4 ++ 6 files changed, 87 insertions(+), 98 deletions(-) create mode 100644 tests/multiapp.txt diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index e93315f4..d3054d99 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -37,23 +37,8 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre struct uwsgi_app *wi; - if (wsgi_req->script_name_len == 0) { - wsgi_req->script_name = ""; - } - else if (wsgi_req->script_name_len == 1) { - if (wsgi_req->script_name[0] == '/') { - wsgi_req->script_name = ""; - wsgi_req->script_name_len = 0; - } - } - - if (uwsgi.vhost) { - 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); - } + mountpoint = uwsgi_strncopy(wsgi_req->appid, wsgi_req->appid_len); if (uwsgi_get_app_id(mountpoint, strlen(mountpoint), -1) != -1) { uwsgi_log( "mountpoint %.*s already configured. skip.\n", strlen(mountpoint), mountpoint); @@ -159,7 +144,7 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre wi->callable = up.loaders[loader](arg1); if (!wi->callable) { - uwsgi_log("unable to load app SCRIPT_NAME=%s\n", mountpoint); + uwsgi_log("unable to load app mountpoint=%s\n", mountpoint); goto doh; } @@ -183,8 +168,8 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre } wi->mountpoint = PyString_AsString(app_mnt); wi->mountpoint_len = strlen(wi->mountpoint); - wsgi_req->script_name = wi->mountpoint; - wsgi_req->script_name_len = wi->mountpoint_len; + wsgi_req->appid = wi->mountpoint; + wsgi_req->appid_len = wi->mountpoint_len; uwsgi_log("main mountpoint = %s\n", wi->mountpoint); wi->callable = PyDict_GetItem(applications, app_mnt); } @@ -311,16 +296,16 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre } if (app_type == PYTHON_APP_TYPE_WSGI) { - uwsgi_log( "WSGI application %d (SCRIPT_NAME=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); + uwsgi_log( "WSGI application %d (mountpoint=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); } else if (app_type == PYTHON_APP_TYPE_WEB3) { - uwsgi_log( "Web3 application %d (SCRIPT_NAME=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); + uwsgi_log( "Web3 application %d (mountpoint=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); } else if (app_type == PYTHON_APP_TYPE_PUMP) { - uwsgi_log( "Pump application %d (SCRIPT_NAME=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); + uwsgi_log( "Pump application %d (mountpoint=%.*s) ready on interpreter %p pid: %d", id, wi->mountpoint_len, wi->mountpoint, wi->interpreter, (int) getpid()); } - if (!wsgi_req->script_name_len) { + if (!wsgi_req->appid_len) { uwsgi_rawlog(" (default app)"); uwsgi.default_app = id; } @@ -337,8 +322,8 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre continue; } - wsgi_req->script_name = PyString_AsString(app_mnt); - wsgi_req->script_name_len = strlen(wsgi_req->script_name); + wsgi_req->appid = PyString_AsString(app_mnt); + wsgi_req->appid_len = strlen(wsgi_req->appid); init_uwsgi_app(LOADER_CALLABLE, PyDict_GetItem(applications, app_mnt), wsgi_req, wi->interpreter, app_type); } } diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index 7af28c4b..bdea5b36 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -326,8 +326,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { struct uwsgi_app *wi; int tmp_stderr; - char *what; - int what_len; + int free_appid = 0; #ifdef UWSGI_ASYNC if (wsgi_req->async_status == UWSGI_AGAIN) { @@ -354,7 +353,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { /* Standard WSGI request */ if (!wsgi_req->uh.pktsize) { - uwsgi_log( "Invalid WSGI request. skip.\n"); + uwsgi_log( "Empty python request. skip.\n"); return -1; } @@ -363,73 +362,56 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { } - if (!up.ignore_script_name) { - - if (!wsgi_req->script_name) - wsgi_req->script_name = ""; + if (wsgi_req->appid_len == 0) { + if (!up.ignore_script_name) { + wsgi_req->appid = wsgi_req->script_name; + wsgi_req->appid_len = wsgi_req->script_name_len; + } 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; + wsgi_req->appid = uwsgi_concat3n(wsgi_req->host, wsgi_req->host_len, "|",1, wsgi_req->script_name, wsgi_req->script_name_len); + wsgi_req->appid_len = wsgi_req->host_len + 1 + wsgi_req->script_name_len; #ifdef UWSGI_DEBUG uwsgi_debug("VirtualHost SCRIPT_NAME=%s\n", what); #endif + free_appid = 1; } - 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, 0)) == -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->script_len > 0 - || wsgi_req->module_len > 0 - || wsgi_req->file_len > 0 - || wsgi_req->paste_len > 0 - ) { - // this part must be heavy locked in threaded modes - if (uwsgi.threads > 1) { - pthread_mutex_lock(&up.lock_pyloaders); - } - UWSGI_GET_GIL - if (uwsgi.single_interpreter) { - wsgi_req->app_id = init_uwsgi_app(LOADER_DYN, (void *) wsgi_req, wsgi_req, up.main_thread, PYTHON_APP_TYPE_WSGI); - } - else { - wsgi_req->app_id = init_uwsgi_app(LOADER_DYN, (void *) wsgi_req, wsgi_req, NULL, PYTHON_APP_TYPE_WSGI); - } - UWSGI_RELEASE_GIL - if (uwsgi.threads > 1) { - pthread_mutex_unlock(&up.lock_pyloaders); - } - } + if ( (wsgi_req->app_id = uwsgi_get_app_id(wsgi_req->appid, wsgi_req->appid_len, 0)) == -1) { + wsgi_req->app_id = uwsgi.default_app; + if (uwsgi.no_default_app) { + wsgi_req->app_id = -1; + } + if (wsgi_req->dynamic) { + // this part must be heavy locked in threaded modes + if (uwsgi.threads > 1) { + pthread_mutex_lock(&up.lock_pyloaders); + } + + UWSGI_GET_GIL + if (uwsgi.single_interpreter) { + wsgi_req->app_id = init_uwsgi_app(LOADER_DYN, (void *) wsgi_req, wsgi_req, up.main_thread, PYTHON_APP_TYPE_WSGI); + } + else { + wsgi_req->app_id = init_uwsgi_app(LOADER_DYN, (void *) wsgi_req, wsgi_req, NULL, PYTHON_APP_TYPE_WSGI); + } + UWSGI_RELEASE_GIL + if (uwsgi.threads > 1) { + pthread_mutex_unlock(&up.lock_pyloaders); } } - - if (uwsgi.vhost) { - free(what); - } - - } - else { - wsgi_req->app_id = 0; } + if (free_appid) { + free(wsgi_req->appid); + } 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, "wsgi application not found"); - goto clear2; - } - + internal_server_error(wsgi_req, "Python application not found"); + goto clear2; } wi = &uwsgi.apps[wsgi_req->app_id]; @@ -446,21 +428,6 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { } - if (wsgi_req->protocol_len < 5) { - uwsgi_log( "INVALID PROTOCOL: %.*s\n", wsgi_req->protocol_len, wsgi_req->protocol); - internal_server_error(wsgi_req, "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, "invalid HTTP protocol !!!"); - goto clear; - } - - - #ifdef UWSGI_ASYNC wsgi_req->async_environ = wi->environ[wsgi_req->async_id]; wsgi_req->async_args = wi->args[wsgi_req->async_id]; diff --git a/protocol.c b/protocol.c index 7460068b..47114615 100644 --- a/protocol.c +++ b/protocol.c @@ -582,14 +582,17 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { else if (!uwsgi_strncmp("UWSGI_SCRIPT", 12, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len )) { wsgi_req->script = ptrbuf; wsgi_req->script_len = strsize; + wsgi_req->dynamic = 1; } else if (!uwsgi_strncmp("UWSGI_MODULE", 12, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->module = ptrbuf; wsgi_req->module_len = strsize; + wsgi_req->dynamic = 1; } else if (!uwsgi_strncmp("UWSGI_CALLABLE", 14, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->callable = ptrbuf; wsgi_req->callable_len = strsize; + wsgi_req->dynamic = 1; } else if (!uwsgi_strncmp("UWSGI_PYHOME", 12, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->pyhome = ptrbuf; @@ -599,9 +602,14 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { wsgi_req->chdir = ptrbuf; wsgi_req->chdir_len = strsize; } + else if (!uwsgi_strncmp("UWSGI_APPID", 11, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { + wsgi_req->appid = ptrbuf; + wsgi_req->appid_len = strsize; + } else if (!uwsgi_strncmp("UWSGI_FILE", 10, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->file = ptrbuf; wsgi_req->file_len = strsize; + wsgi_req->dynamic = 1; } else if (!uwsgi_strncmp("UWSGI_TOUCH_RELOAD", 18, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->touch_reload = ptrbuf; diff --git a/tests/multiapp.txt b/tests/multiapp.txt new file mode 100644 index 00000000..d826dfe7 --- /dev/null +++ b/tests/multiapp.txt @@ -0,0 +1,23 @@ +- case 1 + +single static app + +- case 2 + +single static multiapp + +- case 3 + +multi app static mountpoint + +- case 4 + +dynamic apps with UWSGI_APPID + +- case 5 + +dynamic apps with SCRIPT_NAME + +- case 6 + +dynamic apps with vhost mode diff --git a/utils.c b/utils.c index ecb53c5e..7bec7ab3 100644 --- a/utils.c +++ b/utils.c @@ -727,7 +727,9 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) { void wsgi_req_setup(struct wsgi_request *wsgi_req, int async_id, struct uwsgi_socket *uwsgi_sock) { wsgi_req->poll.events = POLLIN; + wsgi_req->app_id = uwsgi.default_app; + wsgi_req->async_id = async_id; #ifdef UWSGI_SENDFILE wsgi_req->sendfile_fd = -1; @@ -1326,19 +1328,19 @@ char *uwsgi_strncopy(char *s, int len) { } -int uwsgi_get_app_id(char *script_name, int script_name_len, int modifier1) { +int uwsgi_get_app_id(char *app_name, int app_name_len, int modifier1) { int i; struct stat st; for (i = 0; i < uwsgi.apps_cnt; i++) { #ifdef UWSGI_DEBUG - uwsgi_log("searching for %.*s in %.*s %p\n", script_name_len, script_name, uwsgi.apps[i].mountpoint_len, uwsgi.apps[i].mountpoint, uwsgi.apps[i].callable); + uwsgi_log("searching for %.*s in %.*s %p\n", app_name_len, app_name, uwsgi.apps[i].mountpoint_len, uwsgi.apps[i].mountpoint, uwsgi.apps[i].callable); #endif if (!uwsgi.apps[i].callable) { continue; } - if (!uwsgi_strncmp(uwsgi.apps[i].mountpoint, uwsgi.apps[i].mountpoint_len, script_name, script_name_len)) { + if (!uwsgi_strncmp(uwsgi.apps[i].mountpoint, uwsgi.apps[i].mountpoint_len, app_name, app_name_len)) { if (uwsgi.apps[i].touch_reload) { if (!stat(uwsgi.apps[i].touch_reload, &st)) { if (st.st_mtime != uwsgi.apps[i].touch_reload_mtime) { diff --git a/uwsgi.h b/uwsgi.h index ad435600..d11cb41f 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -706,6 +706,10 @@ struct wsgi_request { //temporary attr int app_id; + int dynamic; + + char *appid; + uint16_t appid_len; struct pollfd poll;