annotate src/c/http.c @ 1930:5a7ae5acdcea

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