Mercurial > urweb
comparison src/c/http.c @ 856:86ec89baee01
cgi protocol
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Tue, 23 Jun 2009 17:59:23 -0400 |
parents | 28e42b22424d |
children | 60240acd15b9 |
comparison
equal
deleted
inserted
replaced
855:28e42b22424d | 856:86ec89baee01 |
---|---|
6 #include <sys/types.h> | 6 #include <sys/types.h> |
7 #include <sys/socket.h> | 7 #include <sys/socket.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #include <signal.h> | 10 #include <signal.h> |
11 #include <stdarg.h> | |
11 | 12 |
12 #include <pthread.h> | 13 #include <pthread.h> |
13 | |
14 #include <mhash.h> | |
15 | 14 |
16 #include "urweb.h" | 15 #include "urweb.h" |
17 #include "request.h" | 16 #include "request.h" |
18 | 17 |
19 int uw_backlog = 10; | 18 int uw_backlog = 10; |
71 } | 70 } |
72 | 71 |
73 return NULL; | 72 return NULL; |
74 } | 73 } |
75 | 74 |
75 static void on_success(uw_context ctx) { | |
76 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n"); | |
77 } | |
78 | |
79 static void on_failure(uw_context ctx) { | |
80 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n"); | |
81 } | |
82 | |
83 static void log_error(void *data, const char *fmt, ...) { | |
84 va_list ap; | |
85 va_start(ap, fmt); | |
86 | |
87 vfprintf(stderr, fmt, ap); | |
88 } | |
89 | |
90 static void log_debug(void *data, const char *fmt, ...) { | |
91 va_list ap; | |
92 va_start(ap, fmt); | |
93 | |
94 vprintf(fmt, ap); | |
95 } | |
96 | |
76 static void *worker(void *data) { | 97 static void *worker(void *data) { |
77 int me = *(int *)data; | 98 int me = *(int *)data; |
78 uw_context ctx = uw_request_new_context(); | 99 uw_context ctx = uw_request_new_context(NULL, log_error, log_debug); |
79 size_t buf_size = 2; | 100 size_t buf_size = 2; |
80 char *buf = malloc(buf_size); | 101 char *buf = malloc(buf_size); |
81 uw_request_context rc = uw_new_request_context(); | 102 uw_request_context rc = uw_new_request_context(); |
82 | 103 |
83 while (1) { | 104 while (1) { |
203 s += 2; | 224 s += 2; |
204 } | 225 } |
205 | 226 |
206 uw_set_headers(ctx, get_header, headers); | 227 uw_set_headers(ctx, get_header, headers); |
207 | 228 |
208 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, sock); | 229 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, |
230 on_success, on_failure, | |
231 NULL, log_error, log_debug, | |
232 sock); | |
209 uw_send(ctx, sock); | 233 uw_send(ctx, sock); |
210 | 234 |
211 if (rr == SERVED || rr == FAILED) | 235 if (rr == SERVED || rr == FAILED) |
212 close(sock); | 236 close(sock); |
213 else if (rr != KEEP_OPEN) | 237 else if (rr != KEEP_OPEN) |
228 | 252 |
229 static void sigint(int signum) { | 253 static void sigint(int signum) { |
230 printf("Exiting....\n"); | 254 printf("Exiting....\n"); |
231 exit(0); | 255 exit(0); |
232 } | 256 } |
257 | |
258 static loggers ls = {NULL, log_error, log_debug}; | |
233 | 259 |
234 int main(int argc, char *argv[]) { | 260 int main(int argc, char *argv[]) { |
235 // The skeleton for this function comes from Beej's sockets tutorial. | 261 // The skeleton for this function comes from Beej's sockets tutorial. |
236 int sockfd; // listen on sock_fd | 262 int sockfd; // listen on sock_fd |
237 struct sockaddr_in my_addr; | 263 struct sockaddr_in my_addr; |
275 fprintf(stderr, "Unexpected getopt() behavior\n"); | 301 fprintf(stderr, "Unexpected getopt() behavior\n"); |
276 return 1; | 302 return 1; |
277 } | 303 } |
278 } | 304 } |
279 | 305 |
280 uw_request_init(); | 306 uw_request_init(NULL, log_error, log_debug); |
281 | 307 |
282 names = calloc(nthreads, sizeof(int)); | 308 names = calloc(nthreads, sizeof(int)); |
283 | 309 |
284 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! | 310 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! |
285 | 311 |
314 | 340 |
315 { | 341 { |
316 pthread_t thread; | 342 pthread_t thread; |
317 int name; | 343 int name; |
318 | 344 |
319 if (pthread_create(&thread, NULL, client_pruner, &name)) { | 345 if (pthread_create(&thread, NULL, client_pruner, &ls)) { |
320 fprintf(stderr, "Error creating pruner thread\n"); | 346 fprintf(stderr, "Error creating pruner thread\n"); |
321 return 1; | 347 return 1; |
322 } | 348 } |
323 } | 349 } |
324 | 350 |