comparison src/c/http.c @ 1930:5a7ae5acdcea

Add '-q' option to HTTP binaries
author Adam Chlipala <adam@chlipala.net>
date Wed, 11 Dec 2013 11:06:08 -0500
parents 398298ca82b9
children 1a04b1edded2
comparison
equal deleted inserted replaced
1929:f792a6544093 1930:5a7ae5acdcea
19 #include "queue.h" 19 #include "queue.h"
20 20
21 extern uw_app uw_application; 21 extern uw_app uw_application;
22 22
23 int uw_backlog = SOMAXCONN; 23 int uw_backlog = SOMAXCONN;
24 static int keepalive = 0; 24 static int keepalive = 0, quiet = 0;
25 25
26 static char *get_header(void *data, const char *h) { 26 static char *get_header(void *data, const char *h) {
27 char *s = data; 27 char *s = data;
28 int len = strlen(h); 28 int len = strlen(h);
29 char *p; 29 char *p;
60 60
61 vfprintf(stderr, fmt, ap); 61 vfprintf(stderr, fmt, ap);
62 } 62 }
63 63
64 static void log_debug(void *data, const char *fmt, ...) { 64 static void log_debug(void *data, const char *fmt, ...) {
65 va_list ap; 65 if (!quiet) {
66 va_start(ap, fmt); 66 va_list ap;
67 67 va_start(ap, fmt);
68 vprintf(fmt, ap); 68
69 vprintf(fmt, ap);
70 }
69 } 71 }
70 72
71 static void *worker(void *data) { 73 static void *worker(void *data) {
72 int me = *(int *)data; 74 int me = *(int *)data;
73 uw_context ctx = uw_request_new_context(me, &uw_application, NULL, log_error, log_debug); 75 uw_context ctx = uw_request_new_context(me, &uw_application, NULL, log_error, log_debug);
80 if (sock == 0) { 82 if (sock == 0) {
81 back = buf; 83 back = buf;
82 sock = uw_dequeue(); 84 sock = uw_dequeue();
83 } 85 }
84 86
85 printf("Handling connection with thread #%d.\n", me); 87 if (!quiet)
88 printf("Handling connection with thread #%d.\n", me);
86 89
87 while (1) { 90 while (1) {
88 int r; 91 int r;
89 char *method, *path, *query_string, *headers, *body, *after, *s, *s2; 92 char *method, *path, *query_string, *headers, *body, *after, *s, *s2;
90 93
97 } 100 }
98 101
99 r = recv(sock, back, buf_size - 1 - (back - buf), 0); 102 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
100 103
101 if (r < 0) { 104 if (r < 0) {
102 fprintf(stderr, "Recv failed\n"); 105 if (!quiet)
106 fprintf(stderr, "Recv failed\n");
103 close(sock); 107 close(sock);
104 sock = 0; 108 sock = 0;
105 break; 109 break;
106 } 110 }
107 111
108 if (r == 0) { 112 if (r == 0) {
109 printf("Connection closed.\n"); 113 if (!quiet)
114 printf("Connection closed.\n");
110 close(sock); 115 close(sock);
111 sock = 0; 116 sock = 0;
112 break; 117 break;
113 } 118 }
114 119
146 } 151 }
147 152
148 r = recv(sock, back, buf_size - 1 - (back - buf), 0); 153 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
149 154
150 if (r < 0) { 155 if (r < 0) {
151 fprintf(stderr, "Recv failed\n"); 156 if (!quiet)
157 fprintf(stderr, "Recv failed\n");
152 close(sock); 158 close(sock);
153 sock = 0; 159 sock = 0;
154 goto done; 160 goto done;
155 } 161 }
156 162
157 if (r == 0) { 163 if (r == 0) {
158 fprintf(stderr, "Connection closed.\n"); 164 if (!quiet)
165 fprintf(stderr, "Connection closed.\n");
159 close(sock); 166 close(sock);
160 sock = 0; 167 sock = 0;
161 goto done; 168 goto done;
162 } 169 }
163 170
216 } 223 }
217 224
218 uw_set_headers(ctx, get_header, headers); 225 uw_set_headers(ctx, get_header, headers);
219 uw_set_env(ctx, get_env, NULL); 226 uw_set_env(ctx, get_env, NULL);
220 227
221 printf("Serving URI %s....\n", path); 228 if (!quiet)
229 printf("Serving URI %s....\n", path);
222 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, 230 rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
223 on_success, on_failure, 231 on_success, on_failure,
224 NULL, log_error, log_debug, 232 NULL, log_error, log_debug,
225 sock, uw_really_send, close); 233 sock, uw_really_send, close);
226 234
265 273
266 return NULL; 274 return NULL;
267 } 275 }
268 276
269 static void help(char *cmd) { 277 static void help(char *cmd) {
270 printf("Usage: %s [-p <port>] [-a <IP address>] [-t <thread count>] [-k]\nThe '-k' option turns on HTTP keepalive.\n", cmd); 278 printf("Usage: %s [-p <port>] [-a <IP address>] [-t <thread count>] [-k] [-q]\nThe '-k' option turns on HTTP keepalive.\nThe '-q' option turns off some chatter on stdout.\n", cmd);
271 } 279 }
272 280
273 static void sigint(int signum) { 281 static void sigint(int signum) {
274 printf("Exiting....\n"); 282 printf("Exiting....\n");
275 exit(0); 283 exit(0);
289 signal(SIGPIPE, SIG_IGN); 297 signal(SIGPIPE, SIG_IGN);
290 298
291 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP 299 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
292 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); 300 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
293 301
294 while ((opt = getopt(argc, argv, "hp:a:t:k")) != -1) { 302 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
295 switch (opt) { 303 switch (opt) {
296 case '?': 304 case '?':
297 fprintf(stderr, "Unknown command-line option"); 305 fprintf(stderr, "Unknown command-line option\n");
298 help(argv[0]); 306 help(argv[0]);
299 return 1; 307 return 1;
300 308
301 case 'h': 309 case 'h':
302 help(argv[0]); 310 help(argv[0]);
330 338
331 case 'k': 339 case 'k':
332 keepalive = 1; 340 keepalive = 1;
333 break; 341 break;
334 342
343 case 'q':
344 quiet = 1;
345 break;
346
335 default: 347 default:
336 fprintf(stderr, "Unexpected getopt() behavior\n"); 348 fprintf(stderr, "Unexpected getopt() behavior\n");
337 return 1; 349 return 1;
338 } 350 }
339 } 351 }
367 return 1; 379 return 1;
368 } 380 }
369 381
370 sin_size = sizeof their_addr; 382 sin_size = sizeof their_addr;
371 383
372 printf("Listening on port %d....\n", uw_port); 384 if (!quiet)
385 printf("Listening on port %d....\n", uw_port);
373 386
374 { 387 {
375 pthread_t thread; 388 pthread_t thread;
376 389
377 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) { 390 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
395 if (new_fd < 0) { 408 if (new_fd < 0) {
396 fprintf(stderr, "Socket accept failed\n"); 409 fprintf(stderr, "Socket accept failed\n");
397 return 1; 410 return 1;
398 } 411 }
399 412
400 printf("Accepted connection.\n"); 413 if (!quiet)
414 printf("Accepted connection.\n");
401 415
402 if (keepalive) { 416 if (keepalive) {
403 int flag = 1; 417 int flag = 1;
404 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); 418 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
405 } 419 }