annotate 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
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adamc@116 3 #include <stdio.h>
adamc@116 4 #include <string.h>
adamc@502 5 #include <stdlib.h>
adamc@116 6 #include <sys/types.h>
adamc@116 7 #include <sys/socket.h>
adamc@116 8 #include <netinet/in.h>
adam@1917 9 #include <netinet/tcp.h>
adam@1779 10 #include <arpa/inet.h>
adamc@472 11 #include <unistd.h>
adamc@502 12 #include <signal.h>
adamc@856 13 #include <stdarg.h>
adamc@116 14
adamc@138 15 #include <pthread.h>
adamc@138 16
adamc@244 17 #include "urweb.h"
adamc@853 18 #include "request.h"
adamc@859 19 #include "queue.h"
adamc@117 20
adamc@1094 21 extern uw_app uw_application;
adamc@1094 22
adam@1914 23 int uw_backlog = SOMAXCONN;
adam@1930 24 static int keepalive = 0, quiet = 0;
adamc@116 25
grrwlf@2069 26 #define qfprintf(f, fmt, args...) do { if(!quiet) fprintf(f, fmt, ##args); } while(0)
grrwlf@2069 27 #define qprintf(fmt, args...) do { if(!quiet) printf(fmt, ##args); } while(0)
grrwlf@2069 28
adamc@854 29 static char *get_header(void *data, const char *h) {
adamc@854 30 char *s = data;
adamc@854 31 int len = strlen(h);
adamc@854 32 char *p;
adamc@854 33
adamc@1134 34 while ((p = strchr(s, ':'))) {
adamc@854 35 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@854 36 return p + 2;
adamc@854 37 } else {
adamc@854 38 if ((s = strchr(p, 0)) && s[1] != 0)
adamc@854 39 s += 2;
adamc@854 40 else
adamc@854 41 return NULL;
adamc@854 42 }
adamc@854 43 }
adamc@854 44
adamc@854 45 return NULL;
adamc@854 46 }
adamc@854 47
adam@1799 48 static char *get_env(void *data, const char *name) {
adam@1799 49 return getenv(name);
adam@1799 50 }
adam@1799 51
adamc@856 52 static void on_success(uw_context ctx) {
adamc@856 53 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@856 54 }
adamc@856 55
adamc@856 56 static void on_failure(uw_context ctx) {
adamc@856 57 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@856 58 }
adamc@856 59
adamc@856 60 static void log_error(void *data, const char *fmt, ...) {
adamc@856 61 va_list ap;
adamc@856 62 va_start(ap, fmt);
adamc@856 63
adamc@856 64 vfprintf(stderr, fmt, ap);
adamc@856 65 }
adamc@856 66
adamc@856 67 static void log_debug(void *data, const char *fmt, ...) {
adam@1930 68 if (!quiet) {
adam@1930 69 va_list ap;
adam@1930 70 va_start(ap, fmt);
adamc@856 71
adam@1930 72 vprintf(fmt, ap);
adam@1930 73 }
adamc@856 74 }
adamc@856 75
grrwlf@1997 76 static uw_loggers ls = {NULL, log_error, log_debug};
grrwlf@1997 77
adamc@853 78 static void *worker(void *data) {
adamc@853 79 int me = *(int *)data;
grrwlf@1997 80 uw_context ctx = uw_request_new_context(me, &uw_application, &ls);
adam@1937 81 size_t buf_size = 1024;
adam@1917 82 char *buf = malloc(buf_size), *back = buf;
adamc@853 83 uw_request_context rc = uw_new_request_context();
adam@1917 84 int sock = 0;
adamc@698 85
adamc@272 86 while (1) {
adam@1917 87 if (sock == 0) {
adam@1917 88 back = buf;
adam@1917 89 sock = uw_dequeue();
adam@1917 90 }
adamc@138 91
grrwlf@2069 92 qprintf("Handling connection with thread #%d.\n", me);
adamc@138 93
adamc@138 94 while (1) {
adamc@737 95 int r;
adam@1917 96 char *method, *path, *query_string, *headers, *body, *after, *s, *s2;
adamc@737 97
adamc@742 98 if (back - buf == buf_size - 1) {
adamc@737 99 char *new_buf;
adamc@737 100 buf_size *= 2;
adamc@737 101 new_buf = realloc(buf, buf_size);
adamc@737 102 back = new_buf + (back - buf);
adamc@737 103 buf = new_buf;
adamc@737 104 }
adamc@737 105
adam@1937 106 *back = 0;
adam@1937 107 body = strstr(buf, "\r\n\r\n");
adam@1937 108 if (body == NULL) {
adam@1937 109 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 110
adam@1937 111 if (r < 0) {
grrwlf@2069 112 qfprintf(stderr, "Recv failed while receiving header\n");
adam@1937 113 close(sock);
adam@1937 114 sock = 0;
adam@1937 115 break;
adam@1937 116 }
adam@1937 117
adam@1937 118 if (r == 0) {
grrwlf@2069 119 qprintf("Connection closed.\n");
adam@1937 120 close(sock);
adam@1937 121 sock = 0;
adam@1937 122 break;
adam@1937 123 }
adam@1937 124
adam@1937 125 back += r;
adam@1937 126 *back = 0;
adamc@138 127 }
adamc@138 128
adam@1937 129 if (body != NULL || (body = strstr(buf, "\r\n\r\n"))) {
adamc@853 130 request_result rr;
adam@1917 131 int should_keepalive = 0;
adamc@138 132
adamc@854 133 body[0] = body[1] = 0;
adamc@854 134 body += 4;
adamc@854 135
adamc@854 136 if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) {
adamc@853 137 int clen;
adamc@737 138
adamc@854 139 if (sscanf(s + 18, "%d\r\n", &clen) != 1) {
adamc@853 140 fprintf(stderr, "Malformed Content-Length header\n");
adam@1917 141 close(sock);
adam@1917 142 sock = 0;
adamc@167 143 break;
adamc@167 144 }
adamc@167 145
adamc@854 146 while (back - body < clen) {
adamc@854 147 if (back - buf == buf_size - 1) {
adamc@854 148 char *new_buf;
adamc@854 149 buf_size *= 2;
adamc@854 150 new_buf = realloc(buf, buf_size);
adamc@854 151
adamc@854 152 back = new_buf + (back - buf);
adamc@854 153 body = new_buf + (body - buf);
adamc@854 154 s = new_buf + (s - buf);
adamc@854 155
adamc@854 156 buf = new_buf;
adamc@854 157 }
adamc@854 158
adamc@854 159 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@854 160
adamc@854 161 if (r < 0) {
grrwlf@2069 162 qfprintf(stderr, "Recv failed\n");
adamc@854 163 close(sock);
adam@1917 164 sock = 0;
adamc@854 165 goto done;
adamc@854 166 }
adamc@854 167
adamc@854 168 if (r == 0) {
grrwlf@2069 169 qfprintf(stderr, "Connection closed.\n");
adamc@854 170 close(sock);
adam@1917 171 sock = 0;
adamc@854 172 goto done;
adamc@854 173 }
adamc@854 174
adamc@854 175 back += r;
adamc@854 176 *back = 0;
adamc@854 177 }
adam@1917 178
adam@1917 179 after = body + clen;
adam@1917 180 } else
adam@1917 181 after = body;
adamc@138 182
adamc@1119 183 body[-4] = '\r';
adamc@1119 184 body[-3] = '\n';
adamc@1119 185
adamc@854 186 if (!(s = strstr(buf, "\r\n"))) {
adamc@854 187 fprintf(stderr, "No newline in request\n");
adamc@854 188 close(sock);
adam@1917 189 sock = 0;
adamc@854 190 goto done;
adamc@854 191 }
adamc@854 192
adamc@1119 193 body[-4] = body[-3] = 0;
adamc@1119 194
adamc@854 195 *s = 0;
adamc@854 196 headers = s + 2;
adamc@854 197 method = s = buf;
adamc@854 198
adam@1426 199 strsep(&s, " ");
adam@1426 200 if (!s) {
adamc@854 201 fprintf(stderr, "No first space in HTTP command\n");
adamc@854 202 close(sock);
adam@1917 203 sock = 0;
adamc@854 204 goto done;
adamc@854 205 }
adamc@854 206 path = s;
adamc@854 207
adamc@1134 208 if ((s = strchr(path, ' ')))
adamc@854 209 *s = 0;
adamc@854 210
adamc@1134 211 if ((s = strchr(path, '?'))) {
adamc@854 212 *s = 0;
adamc@854 213 query_string = s+1;
adamc@854 214 }
adamc@854 215 else
adamc@854 216 query_string = NULL;
adamc@854 217
adamc@854 218 s = headers;
adamc@1134 219 while ((s2 = strchr(s, '\r'))) {
adam@1937 220 if (s2 == s) {
adam@1937 221 *s = 0;
adam@1937 222 break;
adam@1937 223 }
adam@1937 224
adamc@854 225 s = s2;
adamc@854 226
adamc@854 227 if (s[1] == 0)
adamc@854 228 break;
adamc@854 229
adamc@854 230 *s = 0;
adamc@854 231 s += 2;
adamc@854 232 }
adamc@854 233
adamc@854 234 uw_set_headers(ctx, get_header, headers);
adam@1799 235 uw_set_env(ctx, get_env, NULL);
adamc@854 236
grrwlf@2069 237 qprintf("Serving URI %s....\n", path);
adamc@856 238 rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
adamc@856 239 on_success, on_failure,
adamc@856 240 NULL, log_error, log_debug,
adamc@863 241 sock, uw_really_send, close);
adam@1917 242
adam@1915 243 if (rr != KEEP_OPEN) {
adam@1917 244 if (keepalive) {
adam@1917 245 char *connection = uw_Basis_requestHeader(ctx, "Connection");
adam@1917 246
adam@1917 247 should_keepalive = !(connection && !strcmp(connection, "close"));
adam@1917 248 }
adam@1917 249
adam@1917 250 if (!should_keepalive)
adam@1917 251 uw_write_header(ctx, "Connection: close\r\n");
adam@1917 252
adam@1934 253 if (!uw_has_contentLength(ctx)) {
adam@1934 254 char clen[100];
adam@1934 255
adam@1934 256 sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
adam@1934 257 uw_write_header(ctx, clen);
adam@1934 258 }
adam@1934 259
adam@1915 260 uw_send(ctx, sock);
adam@1915 261 }
adamc@138 262
adam@1917 263 if (rr == SERVED || rr == FAILED) {
adam@1917 264 if (should_keepalive) {
adam@1917 265 // In case any other requests are queued up, shift
adam@1917 266 // unprocessed part of buffer to front.
adam@1917 267 int kept = back - after;
adam@1944 268
adam@1945 269 if (kept == 0) {
adam@1944 270 // No pipelining going on here.
adam@1944 271 // We'd might as well try to switch to a different connection,
adam@1944 272 // while we wait for more input on this one.
adam@1944 273 uw_enqueue(sock);
adam@1944 274 sock = 0;
adam@1944 275 } else {
adam@1944 276 // More input! Move it to the front and continue in this loop.
adam@1944 277 memmove(buf, after, kept);
adam@1944 278 back = buf + kept;
adam@1944 279 }
adam@1917 280 } else {
adam@1917 281 close(sock);
adam@1917 282 sock = 0;
adam@1917 283 }
adam@1931 284 } else if (rr == KEEP_OPEN)
adam@1931 285 sock = 0;
adam@1931 286 else
adamc@853 287 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 288
adamc@138 289 break;
adamc@138 290 }
adamc@116 291 }
adamc@116 292
adamc@854 293 done:
adamc@311 294 uw_reset(ctx);
adamc@116 295 }
adamc@1138 296
adamc@1138 297 return NULL;
adamc@116 298 }
adamc@102 299
adamc@477 300 static void help(char *cmd) {
adam@1930 301 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);
adamc@477 302 }
adamc@477 303
adamc@502 304 static void sigint(int signum) {
adamc@502 305 printf("Exiting....\n");
adamc@502 306 exit(0);
adamc@502 307 }
adamc@502 308
adamc@138 309 int main(int argc, char *argv[]) {
adamc@116 310 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 311 int sockfd; // listen on sock_fd
adamc@116 312 struct sockaddr_in my_addr;
adamc@116 313 struct sockaddr_in their_addr; // connector's address information
adamc@1134 314 socklen_t sin_size;
adamc@1134 315 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 316
adamc@502 317 signal(SIGINT, sigint);
adamc@505 318 signal(SIGPIPE, SIG_IGN);
adamc@505 319
adam@1779 320 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 321 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 322
adam@1930 323 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
adamc@472 324 switch (opt) {
adamc@472 325 case '?':
adam@1930 326 fprintf(stderr, "Unknown command-line option\n");
adamc@477 327 help(argv[0]);
adamc@472 328 return 1;
adamc@138 329
adamc@477 330 case 'h':
adamc@477 331 help(argv[0]);
adamc@477 332 return 0;
adamc@477 333
adamc@472 334 case 'p':
adamc@472 335 uw_port = atoi(optarg);
adamc@472 336 if (uw_port <= 0) {
adamc@472 337 fprintf(stderr, "Invalid port number\n");
adamc@477 338 help(argv[0]);
adamc@472 339 return 1;
adamc@472 340 }
adamc@472 341 break;
adamc@472 342
adam@1779 343 case 'a':
adam@1779 344 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 345 fprintf(stderr, "Invalid IP address\n");
adam@1779 346 help(argv[0]);
adam@1779 347 return 1;
adam@1779 348 }
adam@1779 349 break;
adam@1779 350
adamc@472 351 case 't':
adamc@472 352 nthreads = atoi(optarg);
adamc@472 353 if (nthreads <= 0) {
adamc@472 354 fprintf(stderr, "Invalid thread count\n");
adamc@477 355 help(argv[0]);
adamc@472 356 return 1;
adamc@472 357 }
adamc@472 358 break;
adamc@472 359
adam@1917 360 case 'k':
adam@1917 361 keepalive = 1;
adam@1917 362 break;
adam@1917 363
adam@1930 364 case 'q':
adam@1930 365 quiet = 1;
adam@1930 366 break;
adam@1930 367
adamc@472 368 default:
adamc@472 369 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 370 return 1;
adamc@472 371 }
adamc@138 372 }
adamc@138 373
grrwlf@1997 374 uw_request_init(&uw_application, &ls);
adamc@687 375
adamc@138 376 names = calloc(nthreads, sizeof(int));
adamc@116 377
adamc@116 378 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 379
adamc@116 380 if (sockfd < 0) {
adamc@116 381 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 382 return 1;
adamc@116 383 }
adamc@116 384
adamc@116 385 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 386 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 387 return 1;
adamc@116 388 }
adamc@116 389
adamc@116 390 my_addr.sin_family = AF_INET; // host byte order
adamc@311 391 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 392
adamc@116 393 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 394 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 395 return 1;
adamc@116 396 }
adamc@116 397
adamc@311 398 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 399 fprintf(stderr, "Socket listen failed\n");
adamc@116 400 return 1;
adamc@116 401 }
adamc@116 402
adamc@116 403 sin_size = sizeof their_addr;
adamc@116 404
grrwlf@2069 405 qprintf("Listening on port %d....\n", uw_port);
adamc@116 406
adamc@667 407 {
adamc@667 408 pthread_t thread;
adamc@667 409
grrwlf@1997 410 pruner_data *pd = (pruner_data *)malloc(sizeof(pruner_data));
grrwlf@1997 411 pd->app = &uw_application;
grrwlf@1997 412 pd->loggers = &ls;
grrwlf@1997 413
grrwlf@1997 414 if (pthread_create_big(&thread, NULL, client_pruner, pd)) {
adamc@667 415 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 416 return 1;
adamc@667 417 }
adamc@667 418 }
adamc@667 419
adamc@138 420 for (i = 0; i < nthreads; ++i) {
adamc@138 421 pthread_t thread;
adamc@138 422 names[i] = i;
adam@1522 423 if (pthread_create_big(&thread, NULL, worker, &names[i])) {
adamc@138 424 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 425 return 1;
adamc@138 426 }
adamc@138 427 }
adamc@138 428
adamc@116 429 while (1) {
adamc@138 430 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 431
adamc@116 432 if (new_fd < 0) {
grrwlf@2069 433 qfprintf(stderr, "Socket accept failed\n");
adam@1948 434 } else {
grrwlf@2069 435 qprintf("Accepted connection.\n");
adam@1948 436
adam@1948 437 if (keepalive) {
adam@1948 438 int flag = 1;
adam@1948 439 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1948 440 }
adam@1948 441
adam@1948 442 uw_enqueue(new_fd);
adamc@116 443 }
adamc@116 444 }
adamc@102 445 }
adamc@1121 446
adamc@1121 447 void *uw_init_client_data() {
adamc@1121 448 return NULL;
adamc@1121 449 }
adamc@1121 450
adamc@1121 451 void uw_free_client_data(void *data) {
adamc@1121 452 }
adamc@1121 453
adamc@1121 454 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 455 }
adamc@1121 456
adamc@1121 457 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1941 458 uw_ensure_transaction(ctx);
adam@1941 459 uw_get_app(ctx)->expunger(ctx, cli);
adam@1941 460
adam@1941 461 if (uw_commit(ctx))
adam@1941 462 uw_error(ctx, UNLIMITED_RETRY, "Rerunning expunge transaction");
adamc@1121 463 }
adamc@1121 464
adamc@1121 465 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 466 }
adam@1320 467
adam@1320 468 int uw_supports_direct_status = 1;