annotate src/c/http.c @ 1950:b3c8860508ea

New release
author Adam Chlipala <adam@chlipala.net>
date Tue, 31 Dec 2013 10:48:50 -0500
parents 6b80900ddc66
children c93fbd139732
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@1944 269
adam@1945 270 if (kept == 0) {
adam@1944 271 // No pipelining going on here.
adam@1944 272 // We'd might as well try to switch to a different connection,
adam@1944 273 // while we wait for more input on this one.
adam@1944 274 uw_enqueue(sock);
adam@1944 275 sock = 0;
adam@1944 276 } else {
adam@1944 277 // More input! Move it to the front and continue in this loop.
adam@1944 278 memmove(buf, after, kept);
adam@1944 279 back = buf + kept;
adam@1944 280 }
adam@1917 281 } else {
adam@1917 282 close(sock);
adam@1917 283 sock = 0;
adam@1917 284 }
adam@1931 285 } else if (rr == KEEP_OPEN)
adam@1931 286 sock = 0;
adam@1931 287 else
adamc@853 288 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 289
adamc@138 290 break;
adamc@138 291 }
adamc@116 292 }
adamc@116 293
adamc@854 294 done:
adamc@311 295 uw_reset(ctx);
adamc@116 296 }
adamc@1138 297
adamc@1138 298 return NULL;
adamc@116 299 }
adamc@102 300
adamc@477 301 static void help(char *cmd) {
adam@1930 302 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 303 }
adamc@477 304
adamc@502 305 static void sigint(int signum) {
adamc@502 306 printf("Exiting....\n");
adamc@502 307 exit(0);
adamc@502 308 }
adamc@502 309
adamc@1094 310 static loggers ls = {&uw_application, NULL, log_error, log_debug};
adamc@856 311
adamc@138 312 int main(int argc, char *argv[]) {
adamc@116 313 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 314 int sockfd; // listen on sock_fd
adamc@116 315 struct sockaddr_in my_addr;
adamc@116 316 struct sockaddr_in their_addr; // connector's address information
adamc@1134 317 socklen_t sin_size;
adamc@1134 318 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 319
adamc@502 320 signal(SIGINT, sigint);
adamc@505 321 signal(SIGPIPE, SIG_IGN);
adamc@505 322
adam@1779 323 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 324 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 325
adam@1930 326 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
adamc@472 327 switch (opt) {
adamc@472 328 case '?':
adam@1930 329 fprintf(stderr, "Unknown command-line option\n");
adamc@477 330 help(argv[0]);
adamc@472 331 return 1;
adamc@138 332
adamc@477 333 case 'h':
adamc@477 334 help(argv[0]);
adamc@477 335 return 0;
adamc@477 336
adamc@472 337 case 'p':
adamc@472 338 uw_port = atoi(optarg);
adamc@472 339 if (uw_port <= 0) {
adamc@472 340 fprintf(stderr, "Invalid port number\n");
adamc@477 341 help(argv[0]);
adamc@472 342 return 1;
adamc@472 343 }
adamc@472 344 break;
adamc@472 345
adam@1779 346 case 'a':
adam@1779 347 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 348 fprintf(stderr, "Invalid IP address\n");
adam@1779 349 help(argv[0]);
adam@1779 350 return 1;
adam@1779 351 }
adam@1779 352 break;
adam@1779 353
adamc@472 354 case 't':
adamc@472 355 nthreads = atoi(optarg);
adamc@472 356 if (nthreads <= 0) {
adamc@472 357 fprintf(stderr, "Invalid thread count\n");
adamc@477 358 help(argv[0]);
adamc@472 359 return 1;
adamc@472 360 }
adamc@472 361 break;
adamc@472 362
adam@1917 363 case 'k':
adam@1917 364 keepalive = 1;
adam@1917 365 break;
adam@1917 366
adam@1930 367 case 'q':
adam@1930 368 quiet = 1;
adam@1930 369 break;
adam@1930 370
adamc@472 371 default:
adamc@472 372 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 373 return 1;
adamc@472 374 }
adamc@138 375 }
adamc@138 376
adamc@1094 377 uw_request_init(&uw_application, NULL, log_error, log_debug);
adamc@687 378
adamc@138 379 names = calloc(nthreads, sizeof(int));
adamc@116 380
adamc@116 381 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 382
adamc@116 383 if (sockfd < 0) {
adamc@116 384 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 385 return 1;
adamc@116 386 }
adamc@116 387
adamc@116 388 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 389 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 390 return 1;
adamc@116 391 }
adamc@116 392
adamc@116 393 my_addr.sin_family = AF_INET; // host byte order
adamc@311 394 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 395
adamc@116 396 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 397 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 398 return 1;
adamc@116 399 }
adamc@116 400
adamc@311 401 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 402 fprintf(stderr, "Socket listen failed\n");
adamc@116 403 return 1;
adamc@116 404 }
adamc@116 405
adamc@116 406 sin_size = sizeof their_addr;
adamc@116 407
adam@1930 408 if (!quiet)
adam@1930 409 printf("Listening on port %d....\n", uw_port);
adamc@116 410
adamc@667 411 {
adamc@667 412 pthread_t thread;
adamc@667 413
adam@1522 414 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
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) {
adam@1948 433 if (!quiet)
adam@1948 434 fprintf(stderr, "Socket accept failed\n");
adam@1948 435 } else {
adam@1948 436 if (!quiet)
adam@1948 437 printf("Accepted connection.\n");
adam@1948 438
adam@1948 439 if (keepalive) {
adam@1948 440 int flag = 1;
adam@1948 441 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1948 442 }
adam@1948 443
adam@1948 444 uw_enqueue(new_fd);
adamc@116 445 }
adamc@116 446 }
adamc@102 447 }
adamc@1121 448
adamc@1121 449 void *uw_init_client_data() {
adamc@1121 450 return NULL;
adamc@1121 451 }
adamc@1121 452
adamc@1121 453 void uw_free_client_data(void *data) {
adamc@1121 454 }
adamc@1121 455
adamc@1121 456 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 457 }
adamc@1121 458
adamc@1121 459 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1941 460 uw_ensure_transaction(ctx);
adam@1941 461 uw_get_app(ctx)->expunger(ctx, cli);
adam@1941 462
adam@1941 463 if (uw_commit(ctx))
adam@1941 464 uw_error(ctx, UNLIMITED_RETRY, "Rerunning expunge transaction");
adamc@1121 465 }
adamc@1121 466
adamc@1121 467 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 468 }
adam@1320 469
adam@1320 470 int uw_supports_direct_status = 1;