Mercurial > urweb
comparison src/c/fastcgi.c @ 1134:b08b73591d2c
Switch to gcc -Wall
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 28 Jan 2010 13:32:26 -0500 |
parents | 0cee0c8d8c37 |
children | b7118ffd32ae |
comparison
equal
deleted
inserted
replaced
1133:482815817e99 | 1134:b08b73591d2c |
---|---|
137 static void write_stderr(FCGI_Output *o, const char *fmt, ...) { | 137 static void write_stderr(FCGI_Output *o, const char *fmt, ...) { |
138 int len; | 138 int len; |
139 va_list ap; | 139 va_list ap; |
140 va_start(ap, fmt); | 140 va_start(ap, fmt); |
141 | 141 |
142 len = vsnprintf(o->r.contentData, 65535, fmt, ap); | 142 len = vsnprintf((char *)o->r.contentData, 65535, fmt, ap); |
143 if (len < 0) | 143 if (len < 0) |
144 fprintf(stderr, "vsnprintf() failed in write_stderr().\n"); | 144 fprintf(stderr, "vsnprintf() failed in write_stderr().\n"); |
145 else if (fastcgi_send(o, FCGI_STDERR, len)) | 145 else if (fastcgi_send(o, FCGI_STDERR, len)) |
146 fprintf(stderr, "fastcgi_send() failed in write_stderr().\n"); | 146 fprintf(stderr, "fastcgi_send() failed in write_stderr().\n"); |
147 } | 147 } |
155 FCGI_Output *o = (FCGI_Output *)data; | 155 FCGI_Output *o = (FCGI_Output *)data; |
156 va_list ap; | 156 va_list ap; |
157 va_start(ap, fmt); | 157 va_start(ap, fmt); |
158 | 158 |
159 if (o) { | 159 if (o) { |
160 int len = vsnprintf(o->r.contentData, 65535, fmt, ap); | 160 int len = vsnprintf((char *)o->r.contentData, 65535, fmt, ap); |
161 if (len < 0) | 161 if (len < 0) |
162 fprintf(stderr, "vsnprintf() failed in log_error().\n"); | 162 fprintf(stderr, "vsnprintf() failed in log_error().\n"); |
163 else if (fastcgi_send(o, FCGI_STDERR, len)) | 163 else if (fastcgi_send(o, FCGI_STDERR, len)) |
164 fprintf(stderr, "fastcgi_send() failed in log_error().\n"); | 164 fprintf(stderr, "fastcgi_send() failed in log_error().\n"); |
165 } else | 165 } else |
189 } headers; | 189 } headers; |
190 | 190 |
191 static char *get_header(void *data, const char *h) { | 191 static char *get_header(void *data, const char *h) { |
192 headers *hs = (headers *)data; | 192 headers *hs = (headers *)data; |
193 size_t len = strlen(h); | 193 size_t len = strlen(h); |
194 char *s, *r; | 194 char *s; |
195 const char *saved_h = h; | 195 const char *saved_h = h; |
196 | 196 |
197 if (len > hs->uppercased_len) { | 197 if (len > hs->uppercased_len) { |
198 hs->uppercased_len = len; | 198 hs->uppercased_len = len; |
199 hs->uppercased = realloc(hs->uppercased, len + 6); | 199 hs->uppercased = realloc(hs->uppercased, len + 6); |
204 *s++ = *h == '-' ? '_' : toupper(*h); | 204 *s++ = *h == '-' ? '_' : toupper(*h); |
205 *s = 0; | 205 *s = 0; |
206 | 206 |
207 if (!strcasecmp(saved_h, "Content-length") | 207 if (!strcasecmp(saved_h, "Content-length") |
208 || !strcasecmp(saved_h, "Content-type")) { | 208 || !strcasecmp(saved_h, "Content-type")) { |
209 if (s = search_nvps(hs->nvps, hs->uppercased + 5)) | 209 if ((s = search_nvps(hs->nvps, hs->uppercased + 5))) |
210 return s; | 210 return s; |
211 } | 211 } |
212 | 212 |
213 return search_nvps(hs->nvps, hs->uppercased); | 213 return search_nvps(hs->nvps, hs->uppercased); |
214 } | 214 } |
298 | 298 |
299 return write_stdout(&out, buf, len); | 299 return write_stdout(&out, buf, len); |
300 } | 300 } |
301 | 301 |
302 static void *worker(void *data) { | 302 static void *worker(void *data) { |
303 int me = *(int *)data; | |
304 FCGI_Input *in = fastcgi_input(); | 303 FCGI_Input *in = fastcgi_input(); |
305 FCGI_Output *out = fastcgi_output(); | 304 FCGI_Output *out = fastcgi_output(); |
306 uw_context ctx = uw_request_new_context(&uw_application, out, log_error, log_debug); | 305 uw_context ctx = uw_request_new_context(&uw_application, out, log_error, log_debug); |
307 uw_request_context rc = uw_new_request_context(); | 306 uw_request_context rc = uw_new_request_context(); |
308 headers hs; | 307 headers hs; |
309 size_t body_size = 0; | 308 size_t body_size = 0; |
310 char *body = malloc(0); | 309 char *body = malloc(0); |
311 size_t path_size = 0; | 310 size_t path_size = 0; |
312 char *path_buf = malloc(0); | 311 char *path_buf = malloc(0); |
313 int tries = 0; | |
314 | 312 |
315 hs.uppercased = malloc(0); | 313 hs.uppercased = malloc(0); |
316 hs.uppercased_len = 0; | 314 hs.uppercased_len = 0; |
317 hs.nvps = malloc(sizeof(nvp)); | 315 hs.nvps = malloc(sizeof(nvp)); |
318 hs.n_nvps = 1; | 316 hs.n_nvps = 1; |
383 } | 381 } |
384 } | 382 } |
385 | 383 |
386 hs.nvps[used_nvps].name[0] = 0; | 384 hs.nvps[used_nvps].name[0] = 0; |
387 | 385 |
388 if (s = get_header(&hs, "Content-Length")) { | 386 if ((s = get_header(&hs, "Content-Length"))) { |
389 body_len = atoi(s); | 387 body_len = atoi(s); |
390 if (body_len < 0) { | 388 if (body_len < 0) { |
391 write_stderr(out, "Invalid Content-Length\n"); | 389 write_stderr(out, "Invalid Content-Length\n"); |
392 goto done; | 390 goto done; |
393 } | 391 } |
398 body_size = body_len+1; | 396 body_size = body_len+1; |
399 body = realloc(body, body_size); | 397 body = realloc(body, body_size); |
400 } | 398 } |
401 | 399 |
402 for (body_read = 0; body_read < body_len; ) { | 400 for (body_read = 0; body_read < body_len; ) { |
403 char *buf; | |
404 int this_len; | 401 int this_len; |
405 | 402 |
406 if (!(r = fastcgi_recv(in))) { | 403 if (!(r = fastcgi_recv(in))) { |
407 write_stderr(out, "Error receiving STDIN\n"); | 404 write_stderr(out, "Error receiving STDIN\n"); |
408 goto done; | 405 goto done; |
439 if (!(path = search_nvps(hs.nvps, "SCRIPT_NAME"))) { | 436 if (!(path = search_nvps(hs.nvps, "SCRIPT_NAME"))) { |
440 write_stderr(out, "SCRIPT_NAME not set\n"); | 437 write_stderr(out, "SCRIPT_NAME not set\n"); |
441 goto done; | 438 goto done; |
442 } | 439 } |
443 | 440 |
444 if (path_info = search_nvps(hs.nvps, "PATH_INFO")) { | 441 if ((path_info = search_nvps(hs.nvps, "PATH_INFO"))) { |
445 int len1 = strlen(path), len2 = strlen(path_info); | 442 int len1 = strlen(path), len2 = strlen(path_info); |
446 int len = len1 + len2 + 1; | 443 int len = len1 + len2 + 1; |
447 | 444 |
448 if (len > path_size) { | 445 if (len > path_size) { |
449 path_size = len; | 446 path_size = len; |
495 static loggers ls = {&uw_application, NULL, log_error, log_debug}; | 492 static loggers ls = {&uw_application, NULL, log_error, log_debug}; |
496 | 493 |
497 int main(int argc, char *argv[]) { | 494 int main(int argc, char *argv[]) { |
498 // The skeleton for this function comes from Beej's sockets tutorial. | 495 // The skeleton for this function comes from Beej's sockets tutorial. |
499 struct sockaddr_in their_addr; // connector's address information | 496 struct sockaddr_in their_addr; // connector's address information |
500 int sin_size, yes = 1; | 497 socklen_t sin_size; |
501 int nthreads = 1, i, *names, opt; | 498 int nthreads = 1, i, *names, opt; |
502 char *fwsa = getenv("FCGI_WEB_SERVER_ADDRS"), *nthreads_s = getenv("URWEB_NUM_THREADS"); | 499 char *fwsa = getenv("FCGI_WEB_SERVER_ADDRS"), *nthreads_s = getenv("URWEB_NUM_THREADS"); |
503 | 500 |
504 if (nthreads_s) { | 501 if (nthreads_s) { |
505 nthreads = atoi(nthreads_s); | 502 nthreads = atoi(nthreads_s); |
547 | 544 |
548 sin_size = sizeof their_addr; | 545 sin_size = sizeof their_addr; |
549 | 546 |
550 { | 547 { |
551 pthread_t thread; | 548 pthread_t thread; |
552 int name; | |
553 | 549 |
554 if (pthread_create(&thread, NULL, client_pruner, &ls)) { | 550 if (pthread_create(&thread, NULL, client_pruner, &ls)) { |
555 fprintf(stderr, "Error creating pruner thread\n"); | 551 fprintf(stderr, "Error creating pruner thread\n"); |
556 return 1; | 552 return 1; |
557 } | 553 } |
581 if (getnameinfo((struct sockaddr *)&their_addr, sin_size, host, sizeof host, NULL, 0, NI_NUMERICHOST)) { | 577 if (getnameinfo((struct sockaddr *)&their_addr, sin_size, host, sizeof host, NULL, 0, NI_NUMERICHOST)) { |
582 fprintf(stderr, "Remote IP determination failed\n"); | 578 fprintf(stderr, "Remote IP determination failed\n"); |
583 return 1; | 579 return 1; |
584 } | 580 } |
585 | 581 |
586 for (ips = fwsa; sep = strchr(ips, ','); ips = sep+1) { | 582 for (ips = fwsa; (sep = strchr(ips, ',')); ips = sep+1) { |
587 if (!strncmp(ips, host, sep - ips)) { | 583 if (!strncmp(ips, host, sep - ips)) { |
588 matched = 1; | 584 matched = 1; |
589 break; | 585 break; |
590 } | 586 } |
591 } | 587 } |