annotate src/c/http.c @ 1917:398298ca82b9

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