annotate src/c/http.c @ 1941:9f3597979e98

Tweaking handling of database transactions
author Adam Chlipala <adam@chlipala.net>
date Wed, 25 Dec 2013 13:11:43 -0500
parents d02c1a0d8082
children 68c8ffc4ae69
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
adamc@854 26 static char *get_header(void *data, const char *h) {
adamc@854 27 char *s = data;
adamc@854 28 int len = strlen(h);
adamc@854 29 char *p;
adamc@854 30
adamc@1134 31 while ((p = strchr(s, ':'))) {
adamc@854 32 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@854 33 return p + 2;
adamc@854 34 } else {
adamc@854 35 if ((s = strchr(p, 0)) && s[1] != 0)
adamc@854 36 s += 2;
adamc@854 37 else
adamc@854 38 return NULL;
adamc@854 39 }
adamc@854 40 }
adamc@854 41
adamc@854 42 return NULL;
adamc@854 43 }
adamc@854 44
adam@1799 45 static char *get_env(void *data, const char *name) {
adam@1799 46 return getenv(name);
adam@1799 47 }
adam@1799 48
adamc@856 49 static void on_success(uw_context ctx) {
adamc@856 50 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@856 51 }
adamc@856 52
adamc@856 53 static void on_failure(uw_context ctx) {
adamc@856 54 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@856 55 }
adamc@856 56
adamc@856 57 static void log_error(void *data, const char *fmt, ...) {
adamc@856 58 va_list ap;
adamc@856 59 va_start(ap, fmt);
adamc@856 60
adamc@856 61 vfprintf(stderr, fmt, ap);
adamc@856 62 }
adamc@856 63
adamc@856 64 static void log_debug(void *data, const char *fmt, ...) {
adam@1930 65 if (!quiet) {
adam@1930 66 va_list ap;
adam@1930 67 va_start(ap, fmt);
adamc@856 68
adam@1930 69 vprintf(fmt, ap);
adam@1930 70 }
adamc@856 71 }
adamc@856 72
adamc@853 73 static void *worker(void *data) {
adamc@853 74 int me = *(int *)data;
adam@1446 75 uw_context ctx = uw_request_new_context(me, &uw_application, NULL, log_error, log_debug);
adam@1937 76 size_t buf_size = 1024;
adam@1917 77 char *buf = malloc(buf_size), *back = buf;
adamc@853 78 uw_request_context rc = uw_new_request_context();
adam@1917 79 int sock = 0;
adamc@698 80
adamc@272 81 while (1) {
adam@1917 82 if (sock == 0) {
adam@1917 83 back = buf;
adam@1917 84 sock = uw_dequeue();
adam@1917 85 }
adamc@138 86
adam@1930 87 if (!quiet)
adam@1930 88 printf("Handling connection with thread #%d.\n", me);
adamc@138 89
adamc@138 90 while (1) {
adamc@737 91 int r;
adam@1917 92 char *method, *path, *query_string, *headers, *body, *after, *s, *s2;
adamc@737 93
adamc@742 94 if (back - buf == buf_size - 1) {
adamc@737 95 char *new_buf;
adamc@737 96 buf_size *= 2;
adamc@737 97 new_buf = realloc(buf, buf_size);
adamc@737 98 back = new_buf + (back - buf);
adamc@737 99 buf = new_buf;
adamc@737 100 }
adamc@737 101
adam@1937 102 *back = 0;
adam@1937 103 body = strstr(buf, "\r\n\r\n");
adam@1937 104 if (body == NULL) {
adam@1937 105 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 106
adam@1937 107 if (r < 0) {
adam@1937 108 if (!quiet)
adam@1937 109 fprintf(stderr, "Recv failed\n");
adam@1937 110 close(sock);
adam@1937 111 sock = 0;
adam@1937 112 break;
adam@1937 113 }
adam@1937 114
adam@1937 115 if (r == 0) {
adam@1937 116 if (!quiet)
adam@1937 117 printf("Connection closed.\n");
adam@1937 118 close(sock);
adam@1937 119 sock = 0;
adam@1937 120 break;
adam@1937 121 }
adam@1937 122
adam@1937 123 back += r;
adam@1937 124 *back = 0;
adamc@138 125 }
adamc@138 126
adam@1937 127 if (body != NULL || (body = strstr(buf, "\r\n\r\n"))) {
adamc@853 128 request_result rr;
adam@1917 129 int should_keepalive = 0;
adamc@138 130
adamc@854 131 body[0] = body[1] = 0;
adamc@854 132 body += 4;
adamc@854 133
adamc@854 134 if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) {
adamc@853 135 int clen;
adamc@737 136
adamc@854 137 if (sscanf(s + 18, "%d\r\n", &clen) != 1) {
adamc@853 138 fprintf(stderr, "Malformed Content-Length header\n");
adam@1917 139 close(sock);
adam@1917 140 sock = 0;
adamc@167 141 break;
adamc@167 142 }
adamc@167 143
adamc@854 144 while (back - body < clen) {
adamc@854 145 if (back - buf == buf_size - 1) {
adamc@854 146 char *new_buf;
adamc@854 147 buf_size *= 2;
adamc@854 148 new_buf = realloc(buf, buf_size);
adamc@854 149
adamc@854 150 back = new_buf + (back - buf);
adamc@854 151 body = new_buf + (body - buf);
adamc@854 152 s = new_buf + (s - buf);
adamc@854 153
adamc@854 154 buf = new_buf;
adamc@854 155 }
adamc@854 156
adamc@854 157 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@854 158
adamc@854 159 if (r < 0) {
adam@1930 160 if (!quiet)
adam@1930 161 fprintf(stderr, "Recv failed\n");
adamc@854 162 close(sock);
adam@1917 163 sock = 0;
adamc@854 164 goto done;
adamc@854 165 }
adamc@854 166
adamc@854 167 if (r == 0) {
adam@1930 168 if (!quiet)
adam@1930 169 fprintf(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
adam@1930 237 if (!quiet)
adam@1930 238 printf("Serving URI %s....\n", path);
adamc@856 239 rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
adamc@856 240 on_success, on_failure,
adamc@856 241 NULL, log_error, log_debug,
adamc@863 242 sock, uw_really_send, close);
adam@1917 243
adam@1915 244 if (rr != KEEP_OPEN) {
adam@1917 245 if (keepalive) {
adam@1917 246 char *connection = uw_Basis_requestHeader(ctx, "Connection");
adam@1917 247
adam@1917 248 should_keepalive = !(connection && !strcmp(connection, "close"));
adam@1917 249 }
adam@1917 250
adam@1917 251 if (!should_keepalive)
adam@1917 252 uw_write_header(ctx, "Connection: close\r\n");
adam@1917 253
adam@1934 254 if (!uw_has_contentLength(ctx)) {
adam@1934 255 char clen[100];
adam@1934 256
adam@1934 257 sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
adam@1934 258 uw_write_header(ctx, clen);
adam@1934 259 }
adam@1934 260
adam@1915 261 uw_send(ctx, sock);
adam@1915 262 }
adamc@138 263
adam@1917 264 if (rr == SERVED || rr == FAILED) {
adam@1917 265 if (should_keepalive) {
adam@1917 266 // In case any other requests are queued up, shift
adam@1917 267 // unprocessed part of buffer to front.
adam@1917 268 int kept = back - after;
adam@1917 269 memmove(buf, after, kept);
adam@1917 270 back = buf + kept;
adam@1917 271 } else {
adam@1917 272 close(sock);
adam@1917 273 sock = 0;
adam@1917 274 }
adam@1931 275 } else if (rr == KEEP_OPEN)
adam@1931 276 sock = 0;
adam@1931 277 else
adamc@853 278 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 279
adamc@138 280 break;
adamc@138 281 }
adamc@116 282 }
adamc@116 283
adamc@854 284 done:
adamc@311 285 uw_reset(ctx);
adamc@116 286 }
adamc@1138 287
adamc@1138 288 return NULL;
adamc@116 289 }
adamc@102 290
adamc@477 291 static void help(char *cmd) {
adam@1930 292 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 293 }
adamc@477 294
adamc@502 295 static void sigint(int signum) {
adamc@502 296 printf("Exiting....\n");
adamc@502 297 exit(0);
adamc@502 298 }
adamc@502 299
adamc@1094 300 static loggers ls = {&uw_application, NULL, log_error, log_debug};
adamc@856 301
adamc@138 302 int main(int argc, char *argv[]) {
adamc@116 303 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 304 int sockfd; // listen on sock_fd
adamc@116 305 struct sockaddr_in my_addr;
adamc@116 306 struct sockaddr_in their_addr; // connector's address information
adamc@1134 307 socklen_t sin_size;
adamc@1134 308 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 309
adamc@502 310 signal(SIGINT, sigint);
adamc@505 311 signal(SIGPIPE, SIG_IGN);
adamc@505 312
adam@1779 313 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 314 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 315
adam@1930 316 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
adamc@472 317 switch (opt) {
adamc@472 318 case '?':
adam@1930 319 fprintf(stderr, "Unknown command-line option\n");
adamc@477 320 help(argv[0]);
adamc@472 321 return 1;
adamc@138 322
adamc@477 323 case 'h':
adamc@477 324 help(argv[0]);
adamc@477 325 return 0;
adamc@477 326
adamc@472 327 case 'p':
adamc@472 328 uw_port = atoi(optarg);
adamc@472 329 if (uw_port <= 0) {
adamc@472 330 fprintf(stderr, "Invalid port number\n");
adamc@477 331 help(argv[0]);
adamc@472 332 return 1;
adamc@472 333 }
adamc@472 334 break;
adamc@472 335
adam@1779 336 case 'a':
adam@1779 337 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 338 fprintf(stderr, "Invalid IP address\n");
adam@1779 339 help(argv[0]);
adam@1779 340 return 1;
adam@1779 341 }
adam@1779 342 break;
adam@1779 343
adamc@472 344 case 't':
adamc@472 345 nthreads = atoi(optarg);
adamc@472 346 if (nthreads <= 0) {
adamc@472 347 fprintf(stderr, "Invalid thread count\n");
adamc@477 348 help(argv[0]);
adamc@472 349 return 1;
adamc@472 350 }
adamc@472 351 break;
adamc@472 352
adam@1917 353 case 'k':
adam@1917 354 keepalive = 1;
adam@1917 355 break;
adam@1917 356
adam@1930 357 case 'q':
adam@1930 358 quiet = 1;
adam@1930 359 break;
adam@1930 360
adamc@472 361 default:
adamc@472 362 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 363 return 1;
adamc@472 364 }
adamc@138 365 }
adamc@138 366
adamc@1094 367 uw_request_init(&uw_application, NULL, log_error, log_debug);
adamc@687 368
adamc@138 369 names = calloc(nthreads, sizeof(int));
adamc@116 370
adamc@116 371 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 372
adamc@116 373 if (sockfd < 0) {
adamc@116 374 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 375 return 1;
adamc@116 376 }
adamc@116 377
adamc@116 378 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 379 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 380 return 1;
adamc@116 381 }
adamc@116 382
adamc@116 383 my_addr.sin_family = AF_INET; // host byte order
adamc@311 384 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 385
adamc@116 386 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 387 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 388 return 1;
adamc@116 389 }
adamc@116 390
adamc@311 391 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 392 fprintf(stderr, "Socket listen failed\n");
adamc@116 393 return 1;
adamc@116 394 }
adamc@116 395
adamc@116 396 sin_size = sizeof their_addr;
adamc@116 397
adam@1930 398 if (!quiet)
adam@1930 399 printf("Listening on port %d....\n", uw_port);
adamc@116 400
adamc@667 401 {
adamc@667 402 pthread_t thread;
adamc@667 403
adam@1522 404 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
adamc@667 405 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 406 return 1;
adamc@667 407 }
adamc@667 408 }
adamc@667 409
adamc@138 410 for (i = 0; i < nthreads; ++i) {
adamc@138 411 pthread_t thread;
adamc@138 412 names[i] = i;
adam@1522 413 if (pthread_create_big(&thread, NULL, worker, &names[i])) {
adamc@138 414 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 415 return 1;
adamc@138 416 }
adamc@138 417 }
adamc@138 418
adamc@116 419 while (1) {
adamc@138 420 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 421
adamc@116 422 if (new_fd < 0) {
adamc@116 423 fprintf(stderr, "Socket accept failed\n");
adamc@116 424 return 1;
adamc@116 425 }
adamc@116 426
adam@1930 427 if (!quiet)
adam@1930 428 printf("Accepted connection.\n");
adamc@138 429
adam@1917 430 if (keepalive) {
adam@1917 431 int flag = 1;
adam@1917 432 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1917 433 }
adam@1917 434
adamc@859 435 uw_enqueue(new_fd);
adamc@116 436 }
adamc@102 437 }
adamc@1121 438
adamc@1121 439 void *uw_init_client_data() {
adamc@1121 440 return NULL;
adamc@1121 441 }
adamc@1121 442
adamc@1121 443 void uw_free_client_data(void *data) {
adamc@1121 444 }
adamc@1121 445
adamc@1121 446 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 447 }
adamc@1121 448
adamc@1121 449 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1941 450 uw_ensure_transaction(ctx);
adam@1941 451 uw_get_app(ctx)->expunger(ctx, cli);
adam@1941 452
adam@1941 453 if (uw_commit(ctx))
adam@1941 454 uw_error(ctx, UNLIMITED_RETRY, "Rerunning expunge transaction");
adamc@1121 455 }
adamc@1121 456
adamc@1121 457 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 458 }
adam@1320 459
adam@1320 460 int uw_supports_direct_status = 1;