From 42d4ba09d78efacde803ff82466b8b9a8b404204 Mon Sep 17 00:00:00 2001 From: Unbit Date: Mon, 22 Apr 2013 09:43:20 +0200 Subject: [PATCH] refactored uwsgi_get_app_id --- buildconf/all.ini | 2 +- core/utils.c | 36 ++++++++++++-- plugins/mono/mono_plugin.c | 4 +- plugins/psgi/psgi_plugin.c | 2 +- plugins/python/pyloader.c | 2 +- plugins/python/wsgi_handlers.c | 29 +---------- plugins/xslt/xslt.c | 90 ++++++++++++++++++++++++++++++++++ uwsgi.h | 2 +- 8 files changed, 130 insertions(+), 37 deletions(-) diff --git a/buildconf/all.ini b/buildconf/all.ini index 5e8cebaa..8fd474a5 100644 --- a/buildconf/all.ini +++ b/buildconf/all.ini @@ -1,3 +1,3 @@ [uwsgi] -main_plugin = python,gevent,psgi,lua,php,rack,jvm,jwsgi,ring,mono,transformation_gzip,transformation_toupper,coroae,v8,cgi +main_plugin = python,gevent,psgi,lua,php,rack,jvm,jwsgi,ring,mono,transformation_toupper,coroae,v8,cgi inherit = base diff --git a/core/utils.c b/core/utils.c index 10e3b2c9..9b2db5a9 100644 --- a/core/utils.c +++ b/core/utils.c @@ -1136,11 +1136,36 @@ void parse_sys_envs(char **envs) { } // get the application id -int uwsgi_get_app_id(char *app_name, int app_name_len, int modifier1) { +int uwsgi_get_app_id(struct wsgi_request *wsgi_req, char *key, uint16_t key_len, int modifier1) { int i; struct stat st; int found; + int free_appname = 0; + + char *app_name = key; + uint16_t app_name_len = key_len; + + if (!app_name && wsgi_req) { + app_name = wsgi_req->appid; + app_name_len = wsgi_req->appid_len; + if (app_name_len == 0) { + if (!uwsgi.ignore_script_name) { + app_name = wsgi_req->script_name; + app_name_len = wsgi_req->script_name_len; + } + + if (uwsgi.vhost) { + app_name = uwsgi_concat3n(wsgi_req->host, wsgi_req->host_len, "|",1, wsgi_req->script_name, wsgi_req->script_name_len); + app_name_len = wsgi_req->host_len + 1 + wsgi_req->script_name_len; +#ifdef UWSGI_DEBUG + uwsgi_debug("VirtualHost KEY=%.*s\n", wsgi_req->appid_len, wsgi_req->appid); +#endif + free_appname = 1; + } + } + } + for (i = 0; i < uwsgi_apps_cnt; i++) { // reset check @@ -1172,14 +1197,19 @@ int uwsgi_get_app_id(char *app_name, int app_name_len, int modifier1) { } } } - if (modifier1 == -1) + if (modifier1 == -1) { + if (free_appname) free(app_name); return i; - if (modifier1 == uwsgi_apps[i].modifier1) + } + if (modifier1 == uwsgi_apps[i].modifier1) { + if (free_appname) free(app_name); return i; + } } } if (!uwsgi.no_default_app) { + if (free_appname) free(app_name); return uwsgi.default_app; } diff --git a/plugins/mono/mono_plugin.c b/plugins/mono/mono_plugin.c index fb8e7bc2..26ff42fb 100644 --- a/plugins/mono/mono_plugin.c +++ b/plugins/mono/mono_plugin.c @@ -540,7 +540,7 @@ static int uwsgi_mono_request(struct wsgi_request *wsgi_req) { key_len = 0; } - wsgi_req->app_id = uwsgi_get_app_id(key, key_len, mono_plugin.modifier1); + wsgi_req->app_id = uwsgi_get_app_id(NULL, key, key_len, mono_plugin.modifier1); // if it is -1, try to load a dynamic app if (wsgi_req->app_id == -1) { if (uwsgi.threads > 1) { @@ -548,7 +548,7 @@ static int uwsgi_mono_request(struct wsgi_request *wsgi_req) { } // check if the mean time, something changed - wsgi_req->app_id = uwsgi_get_app_id(key, key_len, mono_plugin.modifier1); + wsgi_req->app_id = uwsgi_get_app_id(NULL, key, key_len, mono_plugin.modifier1); if (wsgi_req->app_id == -1) { wsgi_req->app_id = uwsgi_mono_create_app(key, key_len, key, key_len, 0); diff --git a/plugins/psgi/psgi_plugin.c b/plugins/psgi/psgi_plugin.c index 6412c203..a64a3ebf 100644 --- a/plugins/psgi/psgi_plugin.c +++ b/plugins/psgi/psgi_plugin.c @@ -394,7 +394,7 @@ int uwsgi_perl_request(struct wsgi_request *wsgi_req) { return -1; } - wsgi_req->app_id = uwsgi_get_app_id(wsgi_req->appid, wsgi_req->appid_len, psgi_plugin.modifier1); + wsgi_req->app_id = uwsgi_get_app_id(wsgi_req, wsgi_req->appid, wsgi_req->appid_len, psgi_plugin.modifier1); // if it is -1, try to load a dynamic app if (wsgi_req->app_id == -1) { if (wsgi_req->dynamic) { diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index 32eedea4..72f6806d 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -86,7 +86,7 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre time_t now = uwsgi_now(); - if (uwsgi_get_app_id(wsgi_req->appid, wsgi_req->appid_len, -1) != -1) { + 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; } diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index b819f64d..1fc870ba 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -293,8 +293,6 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { struct uwsgi_app *wi; - int free_appid = 0; - if (wsgi_req->async_status == UWSGI_AGAIN) { wi = &uwsgi_apps[wsgi_req->app_id]; UWSGI_GET_GIL @@ -330,28 +328,7 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { return -1; } - - if (wsgi_req->appid_len == 0) { - if (!uwsgi.ignore_script_name) { - wsgi_req->appid = wsgi_req->script_name; - wsgi_req->appid_len = wsgi_req->script_name_len; - } - - if (uwsgi.vhost) { - 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 KEY=%.*s\n", wsgi_req->appid_len, wsgi_req->appid); -#endif - free_appid = 1; - } - } - - 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->app_id = uwsgi_get_app_id(wsgi_req, wsgi_req->appid, wsgi_req->appid_len, 0)) == -1) { if (wsgi_req->dynamic) { // this part must be heavy locked in threaded modes if (uwsgi.threads > 1) { @@ -372,10 +349,6 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { } } - if (free_appid) { - free(wsgi_req->appid); - } - if (wsgi_req->app_id == -1) { uwsgi_500(wsgi_req); uwsgi_log("--- no python application found, check your startup logs for errors ---\n"); diff --git a/plugins/xslt/xslt.c b/plugins/xslt/xslt.c index b0efc2c4..33681ca7 100644 --- a/plugins/xslt/xslt.c +++ b/plugins/xslt/xslt.c @@ -15,6 +15,10 @@ xslt:doc=,stylesheet=,params= + As transformation + + toxslt:stylesheet=,params= + */ struct uwsgi_xslt_config { @@ -37,6 +41,12 @@ struct uwsgi_router_xslt_conf { uint16_t content_type_len; }; +struct uwsgi_transformation_xslt_conf { + struct uwsgi_buffer *stylesheet; + struct uwsgi_buffer *params; + struct uwsgi_buffer *content_type; +}; + struct uwsgi_option uwsgi_xslt_options[] = { {"xslt-docroot", required_argument, 0, "add a document_root for xslt processing", uwsgi_opt_add_string_list, &uxslt.docroot, 0}, {"xslt-ext", required_argument, 0, "search for xslt stylesheets with the specified extension", uwsgi_opt_add_string_list, &uxslt.ext, 0}, @@ -313,6 +323,55 @@ static void uwsgi_xslt_log(struct wsgi_request *wsgi_req) { log_request(wsgi_req); } +static int transform_tofile(struct wsgi_request *wsgi_req, struct uwsgi_buffer *ub, struct uwsgi_buffer **new, void *data) { + struct uwsgi_transformation_xslt_conf *utxc = (struct uwsgi_transformation_xslt_conf *) data; + + if + xmlDoc *doc = xmlReadMemory(ub->buf, ub->pos, NULL, NULL, 0); + + int rlen; + char *output = uwsgi_xslt_apply( ub_doc->buf, ub_stylesheet->buf, ub_params ? ub_params->buf : NULL, &rlen); + if (!output) goto end; + + if (uwsgi_response_prepare_headers(wsgi_req, "200 OK", 6)) goto end; + if (uwsgi_response_add_content_length(wsgi_req, rlen)) goto end; + if (uwsgi_response_add_content_type(wsgi_req, urxc->content_type, urxc->content_type_len)) goto end; + + return 0; +} + +static int uwsgi_routing_func_toxslt(struct wsgi_request *wsgi_req, struct uwsgi_route *ur){ + + struct uwsgi_router_xslt_conf *urxc = (struct uwsgi_router_xslt_conf *) ur->data2; + struct uwsgi_transformation_xslt_conf *utxc = uwsgi_calloc(sizeof(struct uwsgi_transformation_xslt_conf)); + + char **subject = (char **) (((char *)(wsgi_req))+ur->subject); + uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len); + + utxc->stylesheet = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, urxc->stylesheet, urxc->stylesheet_len); + if (!utxc->stylesheet) goto end; + + if (urxc->params) { + utxc->params = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, urxc->params, urxc->params_len); + if (!utxc->params) goto end; + } + + if (urxc->content_type) { + utxc->content_type = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, urxc->content_type, urxc->content_type_len); + if (!utxc->content_type) goto end; + } + + uwsgi_transformation_add(wsgi_req, transformation_xslt, utxc); + return UWSGI_ROUTE_NEXT; +end: + if (utxc->stylesheet) uwsgi_buffer_destroy(utxc->stylesheet); + if (utxc->params) uwsgi_buffer_destroy(utxc->params); + if (utxc->content_type) uwsgi_buffer_destroy(utxc->content_type); + free(utxc); + return UWSGI_ROUTE_BREAK; +} + + static int uwsgi_routing_func_xslt(struct wsgi_request *wsgi_req, struct uwsgi_route *ur){ struct uwsgi_router_xslt_conf *urxc = (struct uwsgi_router_xslt_conf *) ur->data2; @@ -390,9 +449,40 @@ static int uwsgi_router_xslt(struct uwsgi_route *ur, char *args) { return 0; } +static int uwsgi_router_toxslt(struct uwsgi_route *ur, char *args) { + ur->func = uwsgi_routing_func_toxslt; + ur->data = args; + ur->data_len = strlen(args); + struct uwsgi_router_xslt_conf *urxc = uwsgi_calloc(sizeof(struct uwsgi_router_xslt_conf)); + if (uwsgi_kvlist_parse(ur->data, ur->data_len, ',', '=', + "stylesheet", &urxc->stylesheet, + "content_type", &urxc->content_type, + "params", &urxc->params, + NULL)) { + uwsgi_log("invalid route syntax: %s\n", args); + exit(1); + } + + if (!urxc->stylesheet) { + uwsgi_log("invalid route/transformation syntax: you need to specify a stylesheet\n"); + exit(1); + } + + urxc->stylesheet_len = strlen(urxc->stylesheet); + + if (urxc->params) urxc->params_len = strlen(urxc->params); + if (!urxc->content_type) urxc->content_type = "text/html"; + urxc->content_type_len = strlen(urxc->content_type); + ur->data2 = urxc; + return 0; +} + + + static void router_xslt_register() { uwsgi_register_router("xslt", uwsgi_router_xslt); + uwsgi_register_router("toxslt", uwsgi_router_toxslt); } diff --git a/uwsgi.h b/uwsgi.h index 2b006188..8b467192 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2680,7 +2680,7 @@ char *uwsgi_concat4(char *, char *, char *, char *); char *uwsgi_concat4n(char *, int, char *, int, char *, int, char *, int); -int uwsgi_get_app_id(char *, int, int); +int uwsgi_get_app_id(struct wsgi_request *, char *, uint16_t, int); char *uwsgi_strncopy(char *, int); int master_loop(char **, char **);