Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
HTTP final fixes, better reloading system, better cwd detection, powe…
Browse files Browse the repository at this point in the history
…red up vacuum
  • Loading branch information
roberto@freebsd64 committed Aug 28, 2010
1 parent 38cfa72 commit 68aff37
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
37 changes: 35 additions & 2 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

Expand Down Expand Up @@ -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(;;) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];

Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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];
}

Expand All @@ -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);
}
Expand All @@ -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<uwsgi.http_vars_cnt;j++) {
char *separator;

separator = strchr(uwsgi.http_vars[j], '=');
if (separator) {
up = add_uwsgi_var(up, uwsgi.http_vars[j], separator - uwsgi.http_vars[j], separator + 1, strlen(separator + 1), 0, watermark);
}
else {
up = add_uwsgi_var(up, uwsgi.http_vars[j], strlen(uwsgi.http_vars[j]), NULL, 0, 0, watermark);
}
}

uwsgi_fd = uwsgi_connect(uwsgi.socket_name, 10);
if (uwsgi_fd >= 0) {
ulen = (up - uwsgipkt) - 4;
Expand Down Expand Up @@ -322,11 +353,13 @@ static void *http_request(void *u_h_r)
state = uwsgi_http_header_key_colon;
} else {
//check for overflow
if (ptr+1 > watermark2) { close(uwsgi_fd); goto clear;}
*ptr++ = buf[i];
}
} else {

//check for overflow
if (ptr+1 > watermark2) { close(uwsgi_fd); goto clear;}
*ptr++ = buf[i];
}

Expand Down
6 changes: 6 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ void sanitize_args(struct uwsgi_server *uwsgi) {
uwsgi_log(" *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** \n");
}
}

#ifdef UWSGI_HTTP
if (uwsgi->http && !uwsgi->http_only) {
uwsgi->vacuum = 1;
}
#endif
}

void env_to_arg(char *src, char *dst) {
Expand Down
50 changes: 32 additions & 18 deletions uwsgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static struct option long_options[] = {
#ifdef UWSGI_HTTP
{"http", required_argument, 0, LONG_ARGS_HTTP},
{"http-only", no_argument, &uwsgi.http_only, 1},
{"http-var", required_argument, 0, LONG_ARGS_HTTP_VAR},
#endif
{"catch-exceptions", no_argument, &uwsgi.catch_exceptions, 1},
{"mode", required_argument, 0, LONG_ARGS_MODE},
Expand Down Expand Up @@ -271,6 +272,7 @@ void grace_them_all() {

void reap_them_all() {
int i;
uwsgi.to_heaven = 1;
uwsgi_log("...brutally killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
kill(uwsgi.workers[i].pid, SIGTERM);
Expand Down Expand Up @@ -351,8 +353,11 @@ static void unconfigured_after_hook(struct uwsgi_server *uwsgi, struct wsgi_requ

static void vacuum(void) {

if (uwsgi.vacuum) {
if (uwsgi.vacuum && getpid() == masterpid) {
if (getpid() == masterpid) {
if (chdir(uwsgi.cwd)) {
uwsgi_error("chdir()");
}
if (uwsgi.socket_name && uwsgi.bind_to_unix) {
if (unlink(uwsgi.socket_name)) {
uwsgi_error("unlink()");
Expand Down Expand Up @@ -403,7 +408,6 @@ int main(int argc, char *argv[], char *envp[]) {
int working_workers = 0;
int blocking_workers = 0;

char *cwd = NULL;
int ready_to_reload = 0;
int ready_to_die = 0;

Expand All @@ -423,9 +427,13 @@ int main(int argc, char *argv[], char *envp[]) {
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, SIG_IGN);

atexit(vacuum);
// initialize masterpid with a default value
masterpid = getpid();

memset(&uwsgi, 0, sizeof(struct uwsgi_server));
uwsgi.cwd = uwsgi_get_cwd();

atexit(vacuum);


#ifdef UWSGI_DEBUG
Expand Down Expand Up @@ -561,7 +569,6 @@ int main(int argc, char *argv[], char *envp[]) {
}

if (uwsgi.binary_path == argv[0]) {
cwd = uwsgi_get_cwd();
uwsgi.binary_path = malloc(strlen(argv[0]) + 1);
if (uwsgi.binary_path == NULL) {
uwsgi_error("malloc()");
Expand Down Expand Up @@ -653,7 +660,7 @@ int main(int argc, char *argv[], char *envp[]) {
sanitize_args(&uwsgi);

#ifdef UWSGI_HTTP
if (uwsgi.http) {
if (uwsgi.http && !uwsgi.is_a_reload) {
char *tcp_port = strchr(uwsgi.http, ':');
if (tcp_port) {
uwsgi.http_server_port = tcp_port+1;
Expand Down Expand Up @@ -686,7 +693,6 @@ int main(int argc, char *argv[], char *envp[]) {


if (uwsgi.http_only) {
signal(SIGINT, (void *) &end_me);
http_loop(&uwsgi);
// never here
exit(1);
Expand Down Expand Up @@ -1268,11 +1274,9 @@ int main(int argc, char *argv[], char *envp[]) {
}
#endif
uwsgi_log( "binary reloading uWSGI...\n");
if (cwd) {
if (chdir(cwd)) {
uwsgi_error("chdir()");
exit(1);
}
if (chdir(uwsgi.cwd)) {
uwsgi_error("chdir()");
exit(1);
}
/* check fd table (a module can obviosly open some fd on initialization...) */
uwsgi_log( "closing all fds > 2 (_SC_OPEN_MAX = %ld)...\n", sysconf(_SC_OPEN_MAX));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -3173,6 +3186,7 @@ void manage_opt(int i, char *optarg) {
\t--routing\t\t\tenable uWSGI advanced routing\n\
\t--http <addr>\t\t\tstart embedded HTTP server on <addr>\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\
Expand Down
8 changes: 8 additions & 0 deletions uwsgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -680,6 +686,8 @@ struct uwsgi_server {
char *callable;

int xml_round2;

char *cwd;
};

struct uwsgi_cluster_node {
Expand Down

0 comments on commit 68aff37

Please sign in to comment.