annotate src/c/http.c @ 1931:1a04b1edded2

Fix regression in http.c for long-polling connections; add lazy initialization of database connections, to avoid the overhead in handlers that don't use SQL
author Adam Chlipala <adam@chlipala.net>
date Wed, 11 Dec 2013 14:57:54 -0500
parents 5a7ae5acdcea
children 420d129c4174
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);
adamc@853 76 size_t buf_size = 2;
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
adamc@742 102 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 103
adamc@138 104 if (r < 0) {
adam@1930 105 if (!quiet)
adam@1930 106 fprintf(stderr, "Recv failed\n");
adam@1917 107 close(sock);
adam@1917 108 sock = 0;
adamc@138 109 break;
adamc@138 110 }
adamc@138 111
adamc@138 112 if (r == 0) {
adam@1930 113 if (!quiet)
adam@1930 114 printf("Connection closed.\n");
adam@1917 115 close(sock);
adam@1917 116 sock = 0;
adamc@138 117 break;
adamc@138 118 }
adamc@138 119
adamc@138 120 back += r;
adamc@138 121 *back = 0;
adamc@730 122
adamc@854 123 if ((body = strstr(buf, "\r\n\r\n"))) {
adamc@853 124 request_result rr;
adam@1917 125 int should_keepalive = 0;
adamc@138 126
adamc@854 127 body[0] = body[1] = 0;
adamc@854 128 body += 4;
adamc@854 129
adamc@854 130 if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) {
adamc@853 131 int clen;
adamc@737 132
adamc@854 133 if (sscanf(s + 18, "%d\r\n", &clen) != 1) {
adamc@853 134 fprintf(stderr, "Malformed Content-Length header\n");
adam@1917 135 close(sock);
adam@1917 136 sock = 0;
adamc@167 137 break;
adamc@167 138 }
adamc@167 139
adamc@854 140 while (back - body < clen) {
adamc@854 141 if (back - buf == buf_size - 1) {
adamc@854 142 char *new_buf;
adamc@854 143 buf_size *= 2;
adamc@854 144 new_buf = realloc(buf, buf_size);
adamc@854 145
adamc@854 146 back = new_buf + (back - buf);
adamc@854 147 body = new_buf + (body - buf);
adamc@854 148 s = new_buf + (s - buf);
adamc@854 149
adamc@854 150 buf = new_buf;
adamc@854 151 }
adamc@854 152
adamc@854 153 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@854 154
adamc@854 155 if (r < 0) {
adam@1930 156 if (!quiet)
adam@1930 157 fprintf(stderr, "Recv failed\n");
adamc@854 158 close(sock);
adam@1917 159 sock = 0;
adamc@854 160 goto done;
adamc@854 161 }
adamc@854 162
adamc@854 163 if (r == 0) {
adam@1930 164 if (!quiet)
adam@1930 165 fprintf(stderr, "Connection closed.\n");
adamc@854 166 close(sock);
adam@1917 167 sock = 0;
adamc@854 168 goto done;
adamc@854 169 }
adamc@854 170
adamc@854 171 back += r;
adamc@854 172 *back = 0;
adamc@854 173 }
adam@1917 174
adam@1917 175 after = body + clen;
adam@1917 176 } else
adam@1917 177 after = body;
adamc@138 178
adamc@1119 179 body[-4] = '\r';
adamc@1119 180 body[-3] = '\n';
adamc@1119 181
adamc@854 182 if (!(s = strstr(buf, "\r\n"))) {
adamc@854 183 fprintf(stderr, "No newline in request\n");
adamc@854 184 close(sock);
adam@1917 185 sock = 0;
adamc@854 186 goto done;
adamc@854 187 }
adamc@854 188
adamc@1119 189 body[-4] = body[-3] = 0;
adamc@1119 190
adamc@854 191 *s = 0;
adamc@854 192 headers = s + 2;
adamc@854 193 method = s = buf;
adamc@854 194
adam@1426 195 strsep(&s, " ");
adam@1426 196 if (!s) {
adamc@854 197 fprintf(stderr, "No first space in HTTP command\n");
adamc@854 198 close(sock);
adam@1917 199 sock = 0;
adamc@854 200 goto done;
adamc@854 201 }
adamc@854 202 path = s;
adamc@854 203
adamc@1134 204 if ((s = strchr(path, ' ')))
adamc@854 205 *s = 0;
adamc@854 206
adamc@1134 207 if ((s = strchr(path, '?'))) {
adamc@854 208 *s = 0;
adamc@854 209 query_string = s+1;
adamc@854 210 }
adamc@854 211 else
adamc@854 212 query_string = NULL;
adamc@854 213
adamc@854 214 s = headers;
adamc@1134 215 while ((s2 = strchr(s, '\r'))) {
adamc@854 216 s = s2;
adamc@854 217
adamc@854 218 if (s[1] == 0)
adamc@854 219 break;
adamc@854 220
adamc@854 221 *s = 0;
adamc@854 222 s += 2;
adamc@854 223 }
adamc@854 224
adamc@854 225 uw_set_headers(ctx, get_header, headers);
adam@1799 226 uw_set_env(ctx, get_env, NULL);
adamc@854 227
adam@1930 228 if (!quiet)
adam@1930 229 printf("Serving URI %s....\n", path);
adamc@856 230 rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
adamc@856 231 on_success, on_failure,
adamc@856 232 NULL, log_error, log_debug,
adamc@863 233 sock, uw_really_send, close);
adam@1917 234
adam@1915 235 if (rr != KEEP_OPEN) {
adam@1915 236 char clen[100];
adam@1915 237
adam@1917 238 if (keepalive) {
adam@1917 239 char *connection = uw_Basis_requestHeader(ctx, "Connection");
adam@1917 240
adam@1917 241 should_keepalive = !(connection && !strcmp(connection, "close"));
adam@1917 242 }
adam@1917 243
adam@1917 244 if (!should_keepalive)
adam@1917 245 uw_write_header(ctx, "Connection: close\r\n");
adam@1917 246
adam@1915 247 sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
adam@1915 248 uw_write_header(ctx, clen);
adam@1915 249 uw_send(ctx, sock);
adam@1915 250 }
adamc@138 251
adam@1917 252 if (rr == SERVED || rr == FAILED) {
adam@1917 253 if (should_keepalive) {
adam@1917 254 // In case any other requests are queued up, shift
adam@1917 255 // unprocessed part of buffer to front.
adam@1917 256 int kept = back - after;
adam@1917 257 memmove(buf, after, kept);
adam@1917 258 back = buf + kept;
adam@1917 259 } else {
adam@1917 260 close(sock);
adam@1917 261 sock = 0;
adam@1917 262 }
adam@1931 263 } else if (rr == KEEP_OPEN)
adam@1931 264 sock = 0;
adam@1931 265 else
adamc@853 266 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 267
adamc@138 268 break;
adamc@138 269 }
adamc@116 270 }
adamc@116 271
adamc@854 272 done:
adamc@311 273 uw_reset(ctx);
adamc@116 274 }
adamc@1138 275
adamc@1138 276 return NULL;
adamc@116 277 }
adamc@102 278
adamc@477 279 static void help(char *cmd) {
adam@1930 280 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 281 }
adamc@477 282
adamc@502 283 static void sigint(int signum) {
adamc@502 284 printf("Exiting....\n");
adamc@502 285 exit(0);
adamc@502 286 }
adamc@502 287
adamc@1094 288 static loggers ls = {&uw_application, NULL, log_error, log_debug};
adamc@856 289
adamc@138 290 int main(int argc, char *argv[]) {
adamc@116 291 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 292 int sockfd; // listen on sock_fd
adamc@116 293 struct sockaddr_in my_addr;
adamc@116 294 struct sockaddr_in their_addr; // connector's address information
adamc@1134 295 socklen_t sin_size;
adamc@1134 296 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 297
adamc@502 298 signal(SIGINT, sigint);
adamc@505 299 signal(SIGPIPE, SIG_IGN);
adamc@505 300
adam@1779 301 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 302 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 303
adam@1930 304 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
adamc@472 305 switch (opt) {
adamc@472 306 case '?':
adam@1930 307 fprintf(stderr, "Unknown command-line option\n");
adamc@477 308 help(argv[0]);
adamc@472 309 return 1;
adamc@138 310
adamc@477 311 case 'h':
adamc@477 312 help(argv[0]);
adamc@477 313 return 0;
adamc@477 314
adamc@472 315 case 'p':
adamc@472 316 uw_port = atoi(optarg);
adamc@472 317 if (uw_port <= 0) {
adamc@472 318 fprintf(stderr, "Invalid port number\n");
adamc@477 319 help(argv[0]);
adamc@472 320 return 1;
adamc@472 321 }
adamc@472 322 break;
adamc@472 323
adam@1779 324 case 'a':
adam@1779 325 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 326 fprintf(stderr, "Invalid IP address\n");
adam@1779 327 help(argv[0]);
adam@1779 328 return 1;
adam@1779 329 }
adam@1779 330 break;
adam@1779 331
adamc@472 332 case 't':
adamc@472 333 nthreads = atoi(optarg);
adamc@472 334 if (nthreads <= 0) {
adamc@472 335 fprintf(stderr, "Invalid thread count\n");
adamc@477 336 help(argv[0]);
adamc@472 337 return 1;
adamc@472 338 }
adamc@472 339 break;
adamc@472 340
adam@1917 341 case 'k':
adam@1917 342 keepalive = 1;
adam@1917 343 break;
adam@1917 344
adam@1930 345 case 'q':
adam@1930 346 quiet = 1;
adam@1930 347 break;
adam@1930 348
adamc@472 349 default:
adamc@472 350 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 351 return 1;
adamc@472 352 }
adamc@138 353 }
adamc@138 354
adamc@1094 355 uw_request_init(&uw_application, NULL, log_error, log_debug);
adamc@687 356
adamc@138 357 names = calloc(nthreads, sizeof(int));
adamc@116 358
adamc@116 359 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 360
adamc@116 361 if (sockfd < 0) {
adamc@116 362 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 363 return 1;
adamc@116 364 }
adamc@116 365
adamc@116 366 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 367 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 368 return 1;
adamc@116 369 }
adamc@116 370
adamc@116 371 my_addr.sin_family = AF_INET; // host byte order
adamc@311 372 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 373
adamc@116 374 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 375 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 376 return 1;
adamc@116 377 }
adamc@116 378
adamc@311 379 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 380 fprintf(stderr, "Socket listen failed\n");
adamc@116 381 return 1;
adamc@116 382 }
adamc@116 383
adamc@116 384 sin_size = sizeof their_addr;
adamc@116 385
adam@1930 386 if (!quiet)
adam@1930 387 printf("Listening on port %d....\n", uw_port);
adamc@116 388
adamc@667 389 {
adamc@667 390 pthread_t thread;
adamc@667 391
adam@1522 392 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
adamc@667 393 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 394 return 1;
adamc@667 395 }
adamc@667 396 }
adamc@667 397
adamc@138 398 for (i = 0; i < nthreads; ++i) {
adamc@138 399 pthread_t thread;
adamc@138 400 names[i] = i;
adam@1522 401 if (pthread_create_big(&thread, NULL, worker, &names[i])) {
adamc@138 402 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 403 return 1;
adamc@138 404 }
adamc@138 405 }
adamc@138 406
adamc@116 407 while (1) {
adamc@138 408 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 409
adamc@116 410 if (new_fd < 0) {
adamc@116 411 fprintf(stderr, "Socket accept failed\n");
adamc@116 412 return 1;
adamc@116 413 }
adamc@116 414
adam@1930 415 if (!quiet)
adam@1930 416 printf("Accepted connection.\n");
adamc@138 417
adam@1917 418 if (keepalive) {
adam@1917 419 int flag = 1;
adam@1917 420 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1917 421 }
adam@1917 422
adamc@859 423 uw_enqueue(new_fd);
adamc@116 424 }
adamc@102 425 }
adamc@1121 426
adamc@1121 427 void *uw_init_client_data() {
adamc@1121 428 return NULL;
adamc@1121 429 }
adamc@1121 430
adamc@1121 431 void uw_free_client_data(void *data) {
adamc@1121 432 }
adamc@1121 433
adamc@1121 434 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 435 }
adamc@1121 436
adamc@1121 437 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adamc@1121 438 if (uw_get_app(ctx)->db_begin(ctx))
adamc@1121 439 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1121 440 uw_get_app(ctx)->expunger(ctx, cli);
adam@1672 441 uw_commit(ctx);
adamc@1121 442 }
adamc@1121 443
adamc@1121 444 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 445 }
adam@1320 446
adam@1320 447 int uw_supports_direct_status = 1;