annotate src/c/http.c @ 1934:420d129c4174

HTTP: avoid duplicate Content-length
author Adam Chlipala <adam@chlipala.net>
date Thu, 12 Dec 2013 10:24:38 -0500
parents 1a04b1edded2
children 6745eafff617
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@1917 236 if (keepalive) {
adam@1917 237 char *connection = uw_Basis_requestHeader(ctx, "Connection");
adam@1917 238
adam@1917 239 should_keepalive = !(connection && !strcmp(connection, "close"));
adam@1917 240 }
adam@1917 241
adam@1917 242 if (!should_keepalive)
adam@1917 243 uw_write_header(ctx, "Connection: close\r\n");
adam@1917 244
adam@1934 245 if (!uw_has_contentLength(ctx)) {
adam@1934 246 char clen[100];
adam@1934 247
adam@1934 248 sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
adam@1934 249 uw_write_header(ctx, clen);
adam@1934 250 }
adam@1934 251
adam@1915 252 uw_send(ctx, sock);
adam@1915 253 }
adamc@138 254
adam@1917 255 if (rr == SERVED || rr == FAILED) {
adam@1917 256 if (should_keepalive) {
adam@1917 257 // In case any other requests are queued up, shift
adam@1917 258 // unprocessed part of buffer to front.
adam@1917 259 int kept = back - after;
adam@1917 260 memmove(buf, after, kept);
adam@1917 261 back = buf + kept;
adam@1917 262 } else {
adam@1917 263 close(sock);
adam@1917 264 sock = 0;
adam@1917 265 }
adam@1931 266 } else if (rr == KEEP_OPEN)
adam@1931 267 sock = 0;
adam@1931 268 else
adamc@853 269 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 270
adamc@138 271 break;
adamc@138 272 }
adamc@116 273 }
adamc@116 274
adamc@854 275 done:
adamc@311 276 uw_reset(ctx);
adamc@116 277 }
adamc@1138 278
adamc@1138 279 return NULL;
adamc@116 280 }
adamc@102 281
adamc@477 282 static void help(char *cmd) {
adam@1930 283 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 284 }
adamc@477 285
adamc@502 286 static void sigint(int signum) {
adamc@502 287 printf("Exiting....\n");
adamc@502 288 exit(0);
adamc@502 289 }
adamc@502 290
adamc@1094 291 static loggers ls = {&uw_application, NULL, log_error, log_debug};
adamc@856 292
adamc@138 293 int main(int argc, char *argv[]) {
adamc@116 294 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 295 int sockfd; // listen on sock_fd
adamc@116 296 struct sockaddr_in my_addr;
adamc@116 297 struct sockaddr_in their_addr; // connector's address information
adamc@1134 298 socklen_t sin_size;
adamc@1134 299 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 300
adamc@502 301 signal(SIGINT, sigint);
adamc@505 302 signal(SIGPIPE, SIG_IGN);
adamc@505 303
adam@1779 304 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 305 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 306
adam@1930 307 while ((opt = getopt(argc, argv, "hp:a:t:kq")) != -1) {
adamc@472 308 switch (opt) {
adamc@472 309 case '?':
adam@1930 310 fprintf(stderr, "Unknown command-line option\n");
adamc@477 311 help(argv[0]);
adamc@472 312 return 1;
adamc@138 313
adamc@477 314 case 'h':
adamc@477 315 help(argv[0]);
adamc@477 316 return 0;
adamc@477 317
adamc@472 318 case 'p':
adamc@472 319 uw_port = atoi(optarg);
adamc@472 320 if (uw_port <= 0) {
adamc@472 321 fprintf(stderr, "Invalid port number\n");
adamc@477 322 help(argv[0]);
adamc@472 323 return 1;
adamc@472 324 }
adamc@472 325 break;
adamc@472 326
adam@1779 327 case 'a':
adam@1779 328 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 329 fprintf(stderr, "Invalid IP address\n");
adam@1779 330 help(argv[0]);
adam@1779 331 return 1;
adam@1779 332 }
adam@1779 333 break;
adam@1779 334
adamc@472 335 case 't':
adamc@472 336 nthreads = atoi(optarg);
adamc@472 337 if (nthreads <= 0) {
adamc@472 338 fprintf(stderr, "Invalid thread count\n");
adamc@477 339 help(argv[0]);
adamc@472 340 return 1;
adamc@472 341 }
adamc@472 342 break;
adamc@472 343
adam@1917 344 case 'k':
adam@1917 345 keepalive = 1;
adam@1917 346 break;
adam@1917 347
adam@1930 348 case 'q':
adam@1930 349 quiet = 1;
adam@1930 350 break;
adam@1930 351
adamc@472 352 default:
adamc@472 353 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 354 return 1;
adamc@472 355 }
adamc@138 356 }
adamc@138 357
adamc@1094 358 uw_request_init(&uw_application, NULL, log_error, log_debug);
adamc@687 359
adamc@138 360 names = calloc(nthreads, sizeof(int));
adamc@116 361
adamc@116 362 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 363
adamc@116 364 if (sockfd < 0) {
adamc@116 365 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 366 return 1;
adamc@116 367 }
adamc@116 368
adamc@116 369 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 370 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 371 return 1;
adamc@116 372 }
adamc@116 373
adamc@116 374 my_addr.sin_family = AF_INET; // host byte order
adamc@311 375 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 376
adamc@116 377 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 378 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 379 return 1;
adamc@116 380 }
adamc@116 381
adamc@311 382 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 383 fprintf(stderr, "Socket listen failed\n");
adamc@116 384 return 1;
adamc@116 385 }
adamc@116 386
adamc@116 387 sin_size = sizeof their_addr;
adamc@116 388
adam@1930 389 if (!quiet)
adam@1930 390 printf("Listening on port %d....\n", uw_port);
adamc@116 391
adamc@667 392 {
adamc@667 393 pthread_t thread;
adamc@667 394
adam@1522 395 if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
adamc@667 396 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 397 return 1;
adamc@667 398 }
adamc@667 399 }
adamc@667 400
adamc@138 401 for (i = 0; i < nthreads; ++i) {
adamc@138 402 pthread_t thread;
adamc@138 403 names[i] = i;
adam@1522 404 if (pthread_create_big(&thread, NULL, worker, &names[i])) {
adamc@138 405 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 406 return 1;
adamc@138 407 }
adamc@138 408 }
adamc@138 409
adamc@116 410 while (1) {
adamc@138 411 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 412
adamc@116 413 if (new_fd < 0) {
adamc@116 414 fprintf(stderr, "Socket accept failed\n");
adamc@116 415 return 1;
adamc@116 416 }
adamc@116 417
adam@1930 418 if (!quiet)
adam@1930 419 printf("Accepted connection.\n");
adamc@138 420
adam@1917 421 if (keepalive) {
adam@1917 422 int flag = 1;
adam@1917 423 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1917 424 }
adam@1917 425
adamc@859 426 uw_enqueue(new_fd);
adamc@116 427 }
adamc@102 428 }
adamc@1121 429
adamc@1121 430 void *uw_init_client_data() {
adamc@1121 431 return NULL;
adamc@1121 432 }
adamc@1121 433
adamc@1121 434 void uw_free_client_data(void *data) {
adamc@1121 435 }
adamc@1121 436
adamc@1121 437 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 438 }
adamc@1121 439
adamc@1121 440 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adamc@1121 441 if (uw_get_app(ctx)->db_begin(ctx))
adamc@1121 442 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1121 443 uw_get_app(ctx)->expunger(ctx, cli);
adam@1672 444 uw_commit(ctx);
adamc@1121 445 }
adamc@1121 446
adamc@1121 447 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 448 }
adam@1320 449
adam@1320 450 int uw_supports_direct_status = 1;