From 68aff372ce528fc4e98df3c2bd13094252f687c4 Mon Sep 17 00:00:00 2001 From: "roberto@freebsd64" Date: Sat, 28 Aug 2010 13:00:21 +0200 Subject: [PATCH] HTTP final fixes, better reloading system, better cwd detection, powered up vacuum --- http.c | 37 +++++++++++++++++++++++++++++++++++-- utils.c | 6 ++++++ uwsgi.c | 50 ++++++++++++++++++++++++++++++++------------------ uwsgi.h | 8 ++++++++ 4 files changed, 81 insertions(+), 20 deletions(-) diff --git a/http.c b/http.c index 69657a94..ec736bd8 100644 --- a/http.c +++ b/http.c @@ -30,6 +30,14 @@ void http_end() { exit(0); } +void http_wait_end() { + pid_t wp_p; + int wp_c; + wp_p = waitpid(-1, &wp_c, 0); + uwsgi_log("closing uWSGI embedded HTTP server.\n"); + exit(0); +} + static char *add_uwsgi_var(char *up, char *key, uint16_t keylen, char *val, uint16_t vallen, int header, char *watermark) { @@ -103,8 +111,14 @@ void http_loop(struct uwsgi_server * uwsgi) exit(1); } + // ignore broken pipes; + signal(SIGPIPE, SIG_IGN); if (!uwsgi->http_only) { signal(SIGCHLD, &http_end); + signal(SIGINT, &http_wait_end); + } + else { + signal(SIGINT, &http_end); } for(;;) { @@ -133,6 +147,7 @@ void http_loop(struct uwsgi_server * uwsgi) if (ret) { uwsgi_log("pthread_create() = %d\n", ret); free(ur); + // sleep a bit to allow some resource gaining sleep(1); continue; } @@ -163,8 +178,7 @@ static void *http_request(void *u_h_r) size_t len; - int i, - j; + int i, j; char HTTP_header_key[1024]; @@ -177,6 +191,7 @@ static void *http_request(void *u_h_r) up = uwsgipkt; char *watermark = up + 4096 ; + char *watermark2 = tmp_buf + 4096 ; up[0] = 0; up[3] = 0; @@ -224,6 +239,7 @@ static void *http_request(void *u_h_r) } else if (state == uwsgi_http_header_key_colon) { + if (ptr+1 > watermark2) { close(uwsgi_fd); goto clear;} *ptr++ = 0; memset(HTTP_header_key, 0, sizeof(HTTP_header_key)); @@ -232,6 +248,7 @@ static void *http_request(void *u_h_r) state = uwsgi_http_header_val; } else { //check for overflow + if (ptr+1 > watermark2) { close(uwsgi_fd); goto clear;} *ptr++ = buf[i]; } @@ -251,6 +268,7 @@ static void *http_request(void *u_h_r) up = add_uwsgi_var(up, HTTP_header_key, strlen(HTTP_header_key), tmp_buf, ptr - tmp_buf, 1, watermark); if (!strcmp("CONTENT_LENGTH", HTTP_header_key)) { + if (ptr+1 > watermark2) { close(uwsgi_fd); goto clear;} *ptr++ = 0; http_body_len = atoi(tmp_buf); } @@ -277,8 +295,21 @@ static void *http_request(void *u_h_r) char *ip = inet_ntoa(ur->c_addr.sin_addr); up = add_uwsgi_var(up, "REMOTE_ADDR", 11, ip, strlen(ip), 0, watermark); + //up = add_uwsgi_var(up, "REMOTE_ADDR", 11, "127.0.0.1", 9, 0, watermark); //up = add_uwsgi_var(up, "REMOTE_USER", 11, "unknown", 7, 0); + for(j=0;j 2 (_SC_OPEN_MAX = %ld)...\n", sysconf(_SC_OPEN_MAX)); @@ -2905,6 +2909,17 @@ void manage_opt(int i, char *optarg) { case LONG_ARGS_ERLANG_COOKIE: uwsgi.erlang_cookie = optarg; break; +#endif +#ifdef UWSGI_HTTP + case LONG_ARGS_HTTP_VAR: + if (uwsgi.http_vars_cnt < 63) { + uwsgi.http_vars[uwsgi.http_vars_cnt] = optarg; + uwsgi.http_vars_cnt++; + } + else { + uwsgi_log( "you can specify at most 64 --http-var options\n"); + } + break; #endif case LONG_ARGS_PYTHONPATH: if (uwsgi.python_path_cnt < 63) { @@ -2938,18 +2953,16 @@ void manage_opt(int i, char *optarg) { case LONG_ARGS_INI_PASTE: uwsgi.ini = optarg; if (uwsgi.ini[0] != '/') { - char *paste_cwd = uwsgi_get_cwd(); - uwsgi.paste = malloc( 7 + strlen(paste_cwd) + 1 + strlen(uwsgi.ini) + 1); + uwsgi.paste = malloc( 7 + strlen(uwsgi.cwd) + 1 + strlen(uwsgi.ini) + 1); if (uwsgi.paste == NULL) { uwsgi_error("malloc()"); exit(1); } - memset(uwsgi.paste, 0, 7 + strlen(paste_cwd) + strlen(uwsgi.ini) + 1); + memset(uwsgi.paste, 0, 7 + strlen(uwsgi.cwd) + strlen(uwsgi.ini) + 1); memcpy(uwsgi.paste, "config:", 7); - memcpy(uwsgi.paste + 7, paste_cwd, strlen(paste_cwd)); - uwsgi.paste[7 + strlen(paste_cwd)] = '/'; - memcpy(uwsgi.paste + 7 + strlen(paste_cwd) + 1, uwsgi.ini, strlen(uwsgi.ini)); - free(paste_cwd); + memcpy(uwsgi.paste + 7, uwsgi.cwd, strlen(uwsgi.cwd)); + uwsgi.paste[7 + strlen(uwsgi.cwd)] = '/'; + memcpy(uwsgi.paste + 7 + strlen(uwsgi.cwd) + 1, uwsgi.ini, strlen(uwsgi.ini)); } else { uwsgi.paste = malloc( 7 + strlen(uwsgi.ini) + 1); @@ -3173,6 +3186,7 @@ void manage_opt(int i, char *optarg) { \t--routing\t\t\tenable uWSGI advanced routing\n\ \t--http \t\t\tstart embedded HTTP server on \n\ \t--http-only\t\t\tstart only the embedded HTTP server\n\ +\t--http-var KEY[=VALUE]\t\tadd var KEY to uwsgi requests made by the embedded HTTP server\n\ \t--catch-exceptions\t\tprint exceptions in the browser\n\ \t--mode\t\t\t\tset configuration mode\n\ \t--env KEY=VALUE\t\t\tset environment variable\n\ diff --git a/uwsgi.h b/uwsgi.h index 58c952b1..5b7d13f9 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -176,6 +176,7 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t); #define LONG_ARGS_PING_TIMEOUT 17040 #define LONG_ARGS_INI_PASTE 17041 #define LONG_ARGS_CALLABLE 17042 +#define LONG_ARGS_HTTP_VAR 17043 @@ -450,11 +451,16 @@ struct uwsgi_server { uid_t uid; char *mode; + +#ifdef UWSGI_HTTP char *http; char *http_server_name; char *http_server_port; int http_only; int http_fd; + char *http_vars[64]; + int http_vars_cnt; +#endif int ignore_script_name; int logdate; @@ -680,6 +686,8 @@ struct uwsgi_server { char *callable; int xml_round2; + + char *cwd; }; struct uwsgi_cluster_node {