From 79c30fed239d807debfaba44b11cf5e531fee258 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Sun, 21 Nov 2010 15:21:06 +0100 Subject: [PATCH] added --manage-script-name --- http.c | 4 ++- plugins/python/python_plugin.c | 65 ++++++++++++++++++++-------------- protocol.c | 43 ++++++++++++++++++++++ utils.c | 11 ++++++ uwsgi.c | 1 + uwsgi.h | 3 ++ 6 files changed, 99 insertions(+), 28 deletions(-) diff --git a/http.c b/http.c index 1f8379a4..592ed040 100644 --- a/http.c +++ b/http.c @@ -236,8 +236,11 @@ static void *http_request(void *u_h_r) { if (!qs) { up = add_uwsgi_var(up, "QUERY_STRING", 12, NULL, 0, 0, watermark); } + + up = add_uwsgi_var(up, "SCRIPT_NAME", 11, "", 0, 0, watermark); up = add_uwsgi_var(up, "PATH_INFO", 9, tmp_buf, path_info_len, 0, watermark); + ptr = tmp_buf; state = uwsgi_http_protocol; @@ -290,7 +293,6 @@ static void *http_request(void *u_h_r) { up = add_uwsgi_var(up, "SERVER_NAME", 11, uwsgi.http_server_name, strlen(uwsgi.http_server_name), 0, watermark); up = add_uwsgi_var(up, "SERVER_PORT", 11, uwsgi.http_server_port, strlen(uwsgi.http_server_port), 0, watermark); - up = add_uwsgi_var(up, "SCRIPT_NAME", 11, "", 0, 0, watermark); ip = inet_ntoa(ur->c_addr.sin_addr); up = add_uwsgi_var(up, "REMOTE_ADDR", 11, ip, strlen(ip), 0, watermark); diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 56268d18..fa97c54f 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -772,35 +772,46 @@ int uwsgi_python_magic(char *mountpoint, char *lazy) { } - int uwsgi_python_xml(char *node, char *content) { +int uwsgi_python_xml(char *node, char *content) { - if (!strcmp("script", node)) { - return init_uwsgi_app(LOADER_UWSGI, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); - } - else if (!strcmp("file", node)) { - return init_uwsgi_app(LOADER_FILE, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); - } - else if (!strcmp("eval", node)) { - return init_uwsgi_app(LOADER_EVAL, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); - } - else if (!strcmp("module", node)) { - uwsgi.wsgi_req->module = content; - uwsgi.wsgi_req->module_len = strlen(content); - return 1; - } - else if (!strcmp("pyhome", node)) { - uwsgi.wsgi_req->pyhome = content; - uwsgi.wsgi_req->pyhome_len = strlen(content); - return 1; - } - else if (!strcmp("callable", node)) { - uwsgi.wsgi_req->callable = content; - uwsgi.wsgi_req->callable_len = strlen(content); - return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, uwsgi.single_interpreter-1); - } - - return 0; + if (!strcmp("script", node)) { + return init_uwsgi_app(LOADER_UWSGI, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); + } + else if (!strcmp("file", node)) { + return init_uwsgi_app(LOADER_FILE, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); + } + else if (!strcmp("eval", node)) { + return init_uwsgi_app(LOADER_EVAL, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); + } + else if (!strcmp("wsgi", node)) { + return init_uwsgi_app(LOADER_EVAL, content, uwsgi.wsgi_req, uwsgi.single_interpreter-1); + } + else if (!strcmp("module", node)) { + uwsgi.wsgi_req->module = content; + uwsgi.wsgi_req->module_len = strlen(content); + uwsgi.wsgi_req->callable = strchr(uwsgi.wsgi_req->module, ':'); + if (uwsgi.wsgi_req->callable) { + uwsgi.wsgi_req->callable[0] = 0; + uwsgi.wsgi_req->callable++; + uwsgi.wsgi_req->callable_len = strlen(uwsgi.wsgi_req->callable); + uwsgi.wsgi_req->module_len = strlen(uwsgi.wsgi_req->module); + return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, uwsgi.single_interpreter-1); } + return 1; + } + else if (!strcmp("pyhome", node)) { + uwsgi.wsgi_req->pyhome = content; + uwsgi.wsgi_req->pyhome_len = strlen(content); + return 1; + } + else if (!strcmp("callable", node)) { + uwsgi.wsgi_req->callable = content; + uwsgi.wsgi_req->callable_len = strlen(content); + return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, uwsgi.single_interpreter-1); + } + + return 0; +} void uwsgi_python_suspend(struct wsgi_request *wsgi_req) { diff --git a/protocol.c b/protocol.c index 202e1324..2a4527e5 100644 --- a/protocol.c +++ b/protocol.c @@ -321,6 +321,7 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { ptrbuf = buffer; bufferend = ptrbuf + wsgi_req->uh.pktsize; + int i, script_name= -1, path_info= -1; /* set an HTTP 500 status as default */ wsgi_req->status = 500; @@ -355,6 +356,7 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { if (!uwsgi_strncmp("SCRIPT_NAME", 11, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->script_name = ptrbuf; wsgi_req->script_name_len = strsize; + script_name = wsgi_req->var_cnt + 1; #ifdef UWSGI_DEBUG uwsgi_debug("SCRIPT_NAME=%.*s\n", wsgi_req->script_name_len, wsgi_req->script_name); #endif @@ -362,6 +364,7 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { else if (!uwsgi_strncmp("PATH_INFO", 9, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) { wsgi_req->path_info = ptrbuf; wsgi_req->path_info_len = strsize; + path_info = wsgi_req->var_cnt + 1; #ifdef UWSGI_DEBUG uwsgi_debug("PATH_INFO=%.*s\n", wsgi_req->path_info_len, wsgi_req->path_info); #endif @@ -471,6 +474,46 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { } + if (uwsgi.manage_script_name) { + if (uwsgi.apps_cnt > 0 && wsgi_req->path_info_len > 1) { + // starts with 1 as the 0 app is the default (/) one + int best_found = 0; + char *orig_path_info = wsgi_req->path_info; + int orig_path_info_len = wsgi_req->path_info_len; + // if SCRIPT_NAME is not allocated, add a slot for it + if (script_name == -1) { + if (wsgi_req->var_cnt >= uwsgi.vec_size - (4 + 2)) { + uwsgi_log( "max vec size reached. skip this header.\n"); + return -1; + } + wsgi_req->var_cnt++; + wsgi_req->hvec[wsgi_req->var_cnt].iov_base = "SCRIPT_NAME"; + wsgi_req->hvec[wsgi_req->var_cnt].iov_len = 11; + wsgi_req->var_cnt++; + script_name = wsgi_req->var_cnt; + } + for(i=1;i= uwsgi.apps[i].mountpoint_len) { + if (!uwsgi_startswith(orig_path_info, uwsgi.apps[i].mountpoint, uwsgi.apps[i].mountpoint_len) && uwsgi.apps[i].mountpoint_len > best_found) { + best_found = uwsgi.apps[i].mountpoint_len; + wsgi_req->script_name = uwsgi.apps[i].mountpoint; + wsgi_req->script_name_len = uwsgi.apps[i].mountpoint_len; + wsgi_req->path_info = orig_path_info+wsgi_req->script_name_len; + wsgi_req->path_info_len = orig_path_info_len-wsgi_req->script_name_len; + + wsgi_req->hvec[script_name].iov_base = wsgi_req->script_name; + wsgi_req->hvec[script_name].iov_len = wsgi_req->script_name_len; + + wsgi_req->hvec[path_info].iov_base = wsgi_req->path_info; + wsgi_req->hvec[path_info].iov_len = wsgi_req->path_info_len; + uwsgi_log("managed SCRIPT_NAME = %.*s PATH_INFO = %.*s\n", wsgi_req->script_name_len, wsgi_req->script_name, wsgi_req->path_info_len, wsgi_req->path_info); + } + } + } + } + } + return 0; } diff --git a/utils.c b/utils.c index c38e9dc7..1d73090c 100644 --- a/utils.c +++ b/utils.c @@ -640,6 +640,17 @@ inline int uwsgi_strncmp(char *src, int slen, char *dst, int dlen) { } +inline int uwsgi_startswith(char *src, char *what, int wlen) { + + int i; + + for(i=0;i