Mercurial > urweb
comparison src/c/http.c @ 2069:262dd74df39c
Replace common "if(!quiet) printf(...)" pattern with a macro
author | Sergey Mironov <grrwlf@gmail.com> |
---|---|
date | Tue, 02 Sep 2014 17:36:14 +0000 |
parents | c93fbd139732 |
children | 382911d72e63 |
comparison
equal
deleted
inserted
replaced
2068:b6adfe99bb08 | 2069:262dd74df39c |
---|---|
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, quiet = 0; | 24 static int keepalive = 0, quiet = 0; |
25 | 25 |
26 #define qfprintf(f, fmt, args...) do { if(!quiet) fprintf(f, fmt, ##args); } while(0) | |
27 #define qprintf(fmt, args...) do { if(!quiet) printf(fmt, ##args); } while(0) | |
28 | |
26 static char *get_header(void *data, const char *h) { | 29 static char *get_header(void *data, const char *h) { |
27 char *s = data; | 30 char *s = data; |
28 int len = strlen(h); | 31 int len = strlen(h); |
29 char *p; | 32 char *p; |
30 | 33 |
84 if (sock == 0) { | 87 if (sock == 0) { |
85 back = buf; | 88 back = buf; |
86 sock = uw_dequeue(); | 89 sock = uw_dequeue(); |
87 } | 90 } |
88 | 91 |
89 if (!quiet) | 92 qprintf("Handling connection with thread #%d.\n", me); |
90 printf("Handling connection with thread #%d.\n", me); | |
91 | 93 |
92 while (1) { | 94 while (1) { |
93 int r; | 95 int r; |
94 char *method, *path, *query_string, *headers, *body, *after, *s, *s2; | 96 char *method, *path, *query_string, *headers, *body, *after, *s, *s2; |
95 | 97 |
105 body = strstr(buf, "\r\n\r\n"); | 107 body = strstr(buf, "\r\n\r\n"); |
106 if (body == NULL) { | 108 if (body == NULL) { |
107 r = recv(sock, back, buf_size - 1 - (back - buf), 0); | 109 r = recv(sock, back, buf_size - 1 - (back - buf), 0); |
108 | 110 |
109 if (r < 0) { | 111 if (r < 0) { |
110 if (!quiet) | 112 qfprintf(stderr, "Recv failed while receiving header\n"); |
111 fprintf(stderr, "Recv failed\n"); | |
112 close(sock); | 113 close(sock); |
113 sock = 0; | 114 sock = 0; |
114 break; | 115 break; |
115 } | 116 } |
116 | 117 |
117 if (r == 0) { | 118 if (r == 0) { |
118 if (!quiet) | 119 qprintf("Connection closed.\n"); |
119 printf("Connection closed.\n"); | |
120 close(sock); | 120 close(sock); |
121 sock = 0; | 121 sock = 0; |
122 break; | 122 break; |
123 } | 123 } |
124 | 124 |
157 } | 157 } |
158 | 158 |
159 r = recv(sock, back, buf_size - 1 - (back - buf), 0); | 159 r = recv(sock, back, buf_size - 1 - (back - buf), 0); |
160 | 160 |
161 if (r < 0) { | 161 if (r < 0) { |
162 if (!quiet) | 162 qfprintf(stderr, "Recv failed\n"); |
163 fprintf(stderr, "Recv failed\n"); | |
164 close(sock); | 163 close(sock); |
165 sock = 0; | 164 sock = 0; |
166 goto done; | 165 goto done; |
167 } | 166 } |
168 | 167 |
169 if (r == 0) { | 168 if (r == 0) { |
170 if (!quiet) | 169 qfprintf(stderr, "Connection closed.\n"); |
171 fprintf(stderr, "Connection closed.\n"); | |
172 close(sock); | 170 close(sock); |
173 sock = 0; | 171 sock = 0; |
174 goto done; | 172 goto done; |
175 } | 173 } |
176 | 174 |
234 } | 232 } |
235 | 233 |
236 uw_set_headers(ctx, get_header, headers); | 234 uw_set_headers(ctx, get_header, headers); |
237 uw_set_env(ctx, get_env, NULL); | 235 uw_set_env(ctx, get_env, NULL); |
238 | 236 |
239 if (!quiet) | 237 qprintf("Serving URI %s....\n", path); |
240 printf("Serving URI %s....\n", path); | |
241 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, | 238 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, |
242 on_success, on_failure, | 239 on_success, on_failure, |
243 NULL, log_error, log_debug, | 240 NULL, log_error, log_debug, |
244 sock, uw_really_send, close); | 241 sock, uw_really_send, close); |
245 | 242 |
403 return 1; | 400 return 1; |
404 } | 401 } |
405 | 402 |
406 sin_size = sizeof their_addr; | 403 sin_size = sizeof their_addr; |
407 | 404 |
408 if (!quiet) | 405 qprintf("Listening on port %d....\n", uw_port); |
409 printf("Listening on port %d....\n", uw_port); | |
410 | 406 |
411 { | 407 { |
412 pthread_t thread; | 408 pthread_t thread; |
413 | 409 |
414 pruner_data *pd = (pruner_data *)malloc(sizeof(pruner_data)); | 410 pruner_data *pd = (pruner_data *)malloc(sizeof(pruner_data)); |
432 | 428 |
433 while (1) { | 429 while (1) { |
434 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); | 430 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); |
435 | 431 |
436 if (new_fd < 0) { | 432 if (new_fd < 0) { |
437 if (!quiet) | 433 qfprintf(stderr, "Socket accept failed\n"); |
438 fprintf(stderr, "Socket accept failed\n"); | |
439 } else { | 434 } else { |
440 if (!quiet) | 435 qprintf("Accepted connection.\n"); |
441 printf("Accepted connection.\n"); | |
442 | 436 |
443 if (keepalive) { | 437 if (keepalive) { |
444 int flag = 1; | 438 int flag = 1; |
445 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); | 439 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); |
446 } | 440 } |