annotate src/c/http.c @ 2071:739172204214

Introduce recv timeout controlled by '-T' option in http.c This should prevent a DDoS attack where attacker and keeps the connection open but send no data.
author Sergey Mironov <grrwlf@gmail.com>
date Tue, 02 Sep 2014 17:42:10 +0000
parents 382911d72e63
children 1839df6ed755
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
grrwlf@2069 26 #define qfprintf(f, fmt, args...) do { if(!quiet) fprintf(f, fmt, ##args); } while(0)
grrwlf@2069 27 #define qprintf(fmt, args...) do { if(!quiet) printf(fmt, ##args); } while(0)
grrwlf@2069 28
adamc@854 29 static char *get_header(void *data, const char *h) {
adamc@854 30 char *s = data;
adamc@854 31 int len = strlen(h);
adamc@854 32 char *p;
adamc@854 33
adamc@1134 34 while ((p = strchr(s, ':'))) {
adamc@854 35 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@854 36 return p + 2;
adamc@854 37 } else {
adamc@854 38 if ((s = strchr(p, 0)) && s[1] != 0)
adamc@854 39 s += 2;
adamc@854 40 else
adamc@854 41 return NULL;
adamc@854 42 }
adamc@854 43 }
adamc@854 44
adamc@854 45 return NULL;
adamc@854 46 }
adamc@854 47
adam@1799 48 static char *get_env(void *data, const char *name) {
adam@1799 49 return getenv(name);
adam@1799 50 }
adam@1799 51
adamc@856 52 static void on_success(uw_context ctx) {
adamc@856 53 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@856 54 }
adamc@856 55
adamc@856 56 static void on_failure(uw_context ctx) {
adamc@856 57 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@856 58 }
adamc@856 59
adamc@856 60 static void log_error(void *data, const char *fmt, ...) {
adamc@856 61 va_list ap;
adamc@856 62 va_start(ap, fmt);
adamc@856 63
adamc@856 64 vfprintf(stderr, fmt, ap);
adamc@856 65 }
adamc@856 66
adamc@856 67 static void log_debug(void *data, const char *fmt, ...) {
adam@1930 68 if (!quiet) {
adam@1930 69 va_list ap;
adam@1930 70 va_start(ap, fmt);
adamc@856 71
adam@1930 72 vprintf(fmt, ap);
adam@1930 73 }
adamc@856 74 }
adamc@856 75
grrwlf@1997 76 static uw_loggers ls = {NULL, log_error, log_debug};
grrwlf@1997 77
adamc@853 78 static void *worker(void *data) {
adamc@853 79 int me = *(int *)data;
grrwlf@1997 80 uw_context ctx = uw_request_new_context(me, &uw_application, &ls);
adam@1937 81 size_t buf_size = 1024;
adam@1917 82 char *buf = malloc(buf_size), *back = buf;
adamc@853 83 uw_request_context rc = uw_new_request_context();
adam@1917 84 int sock = 0;
adamc@698 85
adamc@272 86 while (1) {
adam@1917 87 if (sock == 0) {
adam@1917 88 back = buf;
adam@1917 89 sock = uw_dequeue();
adam@1917 90 }
adamc@138 91
grrwlf@2069 92 qprintf("Handling connection with thread #%d.\n", me);
adamc@138 93
adamc@138 94 while (1) {
adamc@737 95 int r;
adam@1917 96 char *method, *path, *query_string, *headers, *body, *after, *s, *s2;
adamc@737 97
adamc@742 98 if (back - buf == buf_size - 1) {
adamc@737 99 char *new_buf;
grrwlf@2070 100 size_t new_buf_size = buf_size*2;
grrwlf@2070 101 new_buf = realloc(buf, new_buf_size);
grrwlf@2070 102 if(!new_buf) {
grrwlf@2070 103 qfprintf(stderr, "Realloc failed while receiving header\n");
grrwlf@2070 104 close(sock);
grrwlf@2070 105 sock = 0;
grrwlf@2070 106 break;
grrwlf@2070 107 }
grrwlf@2070 108 buf_size = new_buf_size;
adamc@737 109 back = new_buf + (back - buf);
adamc@737 110 buf = new_buf;
adamc@737 111 }
adamc@737 112
adam@1937 113 *back = 0;
adam@1937 114 body = strstr(buf, "\r\n\r\n");
adam@1937 115 if (body == NULL) {
adam@1937 116 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 117
adam@1937 118 if (r < 0) {
grrwlf@2071 119 qfprintf(stderr, "Recv failed while receiving header, retcode %d errno %m\n", r);
adam@1937 120 close(sock);
adam@1937 121 sock = 0;
adam@1937 122 break;
adam@1937 123 }
adam@1937 124
adam@1937 125 if (r == 0) {
grrwlf@2069 126 qprintf("Connection closed.\n");
adam@1937 127 close(sock);
adam@1937 128 sock = 0;
adam@1937 129 break;
adam@1937 130 }
adam@1937 131
adam@1937 132 back += r;
adam@1937 133 *back = 0;
adamc@138 134 }
adamc@138 135
adam@1937 136 if (body != NULL || (body = strstr(buf, "\r\n\r\n"))) {
adamc@853 137 request_result rr;
adam@1917 138 int should_keepalive = 0;
adamc@138 139
adamc@854 140 body[0] = body[1] = 0;
adamc@854 141 body += 4;
adamc@854 142
adamc@854 143 if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) {
adamc@853 144 int clen;
adamc@737 145
adamc@854 146 if (sscanf(s + 18, "%d\r\n", &clen) != 1) {
adamc@853 147 fprintf(stderr, "Malformed Content-Length header\n");
adam@1917 148 close(sock);
adam@1917 149 sock = 0;
adamc@167 150 break;
adamc@167 151 }
adamc@167 152
adamc@854 153 while (back - body < clen) {
adamc@854 154 if (back - buf == buf_size - 1) {
adamc@854 155 char *new_buf;
grrwlf@2070 156 size_t new_buf_size = buf_size * 2;
grrwlf@2070 157 new_buf = realloc(buf, new_buf_size);
grrwlf@2070 158 if(!new_buf) {
grrwlf@2070 159 qfprintf(stderr, "Realloc failed while receiving content\n");
grrwlf@2070 160 close(sock);
grrwlf@2070 161 sock = 0;
grrwlf@2070 162 goto done;
grrwlf@2070 163 }
adamc@854 164
grrwlf@2070 165 buf_size = new_buf_size;
adamc@854 166 back = new_buf + (back - buf);
adamc@854 167 body = new_buf + (body - buf);
adamc@854 168 s = new_buf + (s - buf);
adamc@854 169
adamc@854 170 buf = new_buf;
adamc@854 171 }
adamc@854 172
adamc@854 173 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@854 174
adamc@854 175 if (r < 0) {
grrwlf@2071 176 qfprintf(stderr, "Recv failed while receiving content, retcode %d errno %m\n", r);
adamc@854 177 close(sock);
adam@1917 178 sock = 0;
adamc@854 179 goto done;
adamc@854 180 }
adamc@854 181
adamc@854 182 if (r == 0) {
grrwlf@2069 183 qfprintf(stderr, "Connection closed.\n");
adamc@854 184 close(sock);
adam@1917 185 sock = 0;
adamc@854 186 goto done;
adamc@854 187 }
adamc@854 188
adamc@854 189 back += r;
adamc@854 190 *back = 0;
adamc@854 191 }
adam@1917 192
adam@1917 193 after = body + clen;
adam@1917 194 } else
adam@1917 195 after = body;
adamc@138 196
adamc@1119 197 body[-4] = '\r';
adamc@1119 198 body[-3] = '\n';
adamc@1119 199
adamc@854 200 if (!(s = strstr(buf, "\r\n"))) {
adamc@854 201 fprintf(stderr, "No newline in request\n");
adamc@854 202 close(sock);
adam@1917 203 sock = 0;
adamc@854 204 goto done;
adamc@854 205 }
adamc@854 206
adamc@1119 207 body[-4] = body[-3] = 0;
adamc@1119 208
adamc@854 209 *s = 0;
adamc@854 210 headers = s + 2;
adamc@854 211 method = s = buf;
adamc@854 212
adam@1426 213 strsep(&s, " ");
adam@1426 214 if (!s) {
adamc@854 215 fprintf(stderr, "No first space in HTTP command\n");
adamc@854 216 close(sock);
adam@1917 217 sock = 0;
adamc@854 218 goto done;
adamc@854 219 }
adamc@854 220 path = s;
adamc@854 221
adamc@1134 222 if ((s = strchr(path, ' ')))
adamc@854 223 *s = 0;
adamc@854 224
adamc@1134 225 if ((s = strchr(path, '?'))) {
adamc@854 226 *s = 0;
adamc@854 227 query_string = s+1;
adamc@854 228 }
adamc@854 229 else
adamc@854 230 query_string = NULL;
adamc@854 231
adamc@854 232 s = headers;
adamc@1134 233 while ((s2 = strchr(s, '\r'))) {
adam@1937 234 if (s2 == s) {
adam@1937 235 *s = 0;
adam@1937 236 break;
adam@1937 237 }
adam@1937 238
adamc@854 239 s = s2;
adamc@854 240
adamc@854 241 if (s[1] == 0)
adamc@854 242 break;
adamc@854 243
adamc@854 244 *s = 0;
adamc@854 245 s += 2;
adamc@854 246 }
adamc@854 247
adamc@854 248 uw_set_headers(ctx, get_header, headers);
adam@1799 249 uw_set_env(ctx, get_env, NULL);
adamc@854 250
grrwlf@2069 251 qprintf("Serving URI %s....\n", path);
adamc@856 252 rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
adamc@856 253 on_success, on_failure,
adamc@856 254 NULL, log_error, log_debug,
adamc@863 255 sock, uw_really_send, close);
adam@1917 256
adam@1915 257 if (rr != KEEP_OPEN) {
adam@1917 258 if (keepalive) {
adam@1917 259 char *connection = uw_Basis_requestHeader(ctx, "Connection");
adam@1917 260
adam@1917 261 should_keepalive = !(connection && !strcmp(connection, "close"));
adam@1917 262 }
adam@1917 263
adam@1917 264 if (!should_keepalive)
adam@1917 265 uw_write_header(ctx, "Connection: close\r\n");
adam@1917 266
adam@1934 267 if (!uw_has_contentLength(ctx)) {
adam@1934 268 char clen[100];
adam@1934 269
adam@1934 270 sprintf(clen, "Content-length: %d\r\n", uw_pagelen(ctx));
adam@1934 271 uw_write_header(ctx, clen);
adam@1934 272 }
adam@1934 273
adam@1915 274 uw_send(ctx, sock);
adam@1915 275 }
adamc@138 276
adam@1917 277 if (rr == SERVED || rr == FAILED) {
adam@1917 278 if (should_keepalive) {
adam@1917 279 // In case any other requests are queued up, shift
adam@1917 280 // unprocessed part of buffer to front.
adam@1917 281 int kept = back - after;
adam@1944 282
adam@1945 283 if (kept == 0) {
adam@1944 284 // No pipelining going on here.
adam@1944 285 // We'd might as well try to switch to a different connection,
adam@1944 286 // while we wait for more input on this one.
adam@1944 287 uw_enqueue(sock);
adam@1944 288 sock = 0;
adam@1944 289 } else {
adam@1944 290 // More input! Move it to the front and continue in this loop.
adam@1944 291 memmove(buf, after, kept);
adam@1944 292 back = buf + kept;
adam@1944 293 }
adam@1917 294 } else {
adam@1917 295 close(sock);
adam@1917 296 sock = 0;
adam@1917 297 }
adam@1931 298 } else if (rr == KEEP_OPEN)
adam@1931 299 sock = 0;
adam@1931 300 else
adamc@853 301 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 302
adamc@138 303 break;
adamc@138 304 }
adamc@116 305 }
adamc@116 306
adamc@854 307 done:
adamc@311 308 uw_reset(ctx);
adamc@116 309 }
adamc@1138 310
adamc@1138 311 return NULL;
adamc@116 312 }
adamc@102 313
adamc@477 314 static void help(char *cmd) {
grrwlf@2071 315 printf("Usage: %s [-p <port>] [-a <IP address>] [-t <thread count>] [-k] [-q] [-T SEC]\nThe '-k' option turns on HTTP keepalive.\nThe '-q' option turns off some chatter on stdout.\nThe -T option sets socket recv timeout (0 disables timeout, default is 5 sec)", cmd);
adamc@477 316 }
adamc@477 317
adamc@502 318 static void sigint(int signum) {
adamc@502 319 printf("Exiting....\n");
adamc@502 320 exit(0);
adamc@502 321 }
adamc@502 322
adamc@138 323 int main(int argc, char *argv[]) {
adamc@116 324 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 325 int sockfd; // listen on sock_fd
adamc@116 326 struct sockaddr_in my_addr;
adamc@116 327 struct sockaddr_in their_addr; // connector's address information
adamc@1134 328 socklen_t sin_size;
adamc@1134 329 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
grrwlf@2071 330 int recv_timeout_sec = 5;
adamc@502 331
adamc@502 332 signal(SIGINT, sigint);
adamc@505 333 signal(SIGPIPE, SIG_IGN);
adamc@505 334
adam@1779 335 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adam@1779 336 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adam@1779 337
grrwlf@2071 338 while ((opt = getopt(argc, argv, "hp:a:t:kqT:")) != -1) {
adamc@472 339 switch (opt) {
adamc@472 340 case '?':
adam@1930 341 fprintf(stderr, "Unknown command-line option\n");
adamc@477 342 help(argv[0]);
adamc@472 343 return 1;
adamc@138 344
adamc@477 345 case 'h':
adamc@477 346 help(argv[0]);
adamc@477 347 return 0;
adamc@477 348
adamc@472 349 case 'p':
adamc@472 350 uw_port = atoi(optarg);
adamc@472 351 if (uw_port <= 0) {
adamc@472 352 fprintf(stderr, "Invalid port number\n");
adamc@477 353 help(argv[0]);
adamc@472 354 return 1;
adamc@472 355 }
adamc@472 356 break;
adamc@472 357
adam@1779 358 case 'a':
adam@1779 359 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
adam@1779 360 fprintf(stderr, "Invalid IP address\n");
adam@1779 361 help(argv[0]);
adam@1779 362 return 1;
adam@1779 363 }
adam@1779 364 break;
adam@1779 365
adamc@472 366 case 't':
adamc@472 367 nthreads = atoi(optarg);
adamc@472 368 if (nthreads <= 0) {
adamc@472 369 fprintf(stderr, "Invalid thread count\n");
adamc@477 370 help(argv[0]);
adamc@472 371 return 1;
adamc@472 372 }
adamc@472 373 break;
adamc@472 374
adam@1917 375 case 'k':
adam@1917 376 keepalive = 1;
adam@1917 377 break;
adam@1917 378
grrwlf@2071 379 case 'T':
grrwlf@2071 380 recv_timeout_sec = atoi(optarg);
grrwlf@2071 381 if (recv_timeout_sec < 0) {
grrwlf@2071 382 fprintf(stderr, "Invalid recv timeout\n");
grrwlf@2071 383 help(argv[0]);
grrwlf@2071 384 return 1;
grrwlf@2071 385 }
grrwlf@2071 386 break;
grrwlf@2071 387
adam@1930 388 case 'q':
adam@1930 389 quiet = 1;
adam@1930 390 break;
adam@1930 391
adamc@472 392 default:
adamc@472 393 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 394 return 1;
adamc@472 395 }
adamc@138 396 }
adamc@138 397
grrwlf@1997 398 uw_request_init(&uw_application, &ls);
adamc@687 399
adamc@138 400 names = calloc(nthreads, sizeof(int));
adamc@116 401
adamc@116 402 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 403
adamc@116 404 if (sockfd < 0) {
adamc@116 405 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 406 return 1;
adamc@116 407 }
adamc@116 408
adamc@116 409 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 410 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 411 return 1;
adamc@116 412 }
adamc@116 413
adamc@116 414 my_addr.sin_family = AF_INET; // host byte order
adamc@311 415 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 416
adamc@116 417 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 418 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 419 return 1;
adamc@116 420 }
adamc@116 421
adamc@311 422 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 423 fprintf(stderr, "Socket listen failed\n");
adamc@116 424 return 1;
adamc@116 425 }
adamc@116 426
adamc@116 427 sin_size = sizeof their_addr;
adamc@116 428
grrwlf@2069 429 qprintf("Listening on port %d....\n", uw_port);
adamc@116 430
adamc@667 431 {
adamc@667 432 pthread_t thread;
adamc@667 433
grrwlf@1997 434 pruner_data *pd = (pruner_data *)malloc(sizeof(pruner_data));
grrwlf@1997 435 pd->app = &uw_application;
grrwlf@1997 436 pd->loggers = &ls;
grrwlf@1997 437
grrwlf@1997 438 if (pthread_create_big(&thread, NULL, client_pruner, pd)) {
adamc@667 439 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 440 return 1;
adamc@667 441 }
adamc@667 442 }
adamc@667 443
adamc@138 444 for (i = 0; i < nthreads; ++i) {
adamc@138 445 pthread_t thread;
adamc@138 446 names[i] = i;
adam@1522 447 if (pthread_create_big(&thread, NULL, worker, &names[i])) {
adamc@138 448 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 449 return 1;
adamc@138 450 }
adamc@138 451 }
adamc@138 452
adamc@116 453 while (1) {
adamc@138 454 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 455
adamc@116 456 if (new_fd < 0) {
grrwlf@2069 457 qfprintf(stderr, "Socket accept failed\n");
adam@1948 458 } else {
grrwlf@2069 459 qprintf("Accepted connection.\n");
adam@1948 460
adam@1948 461 if (keepalive) {
adam@1948 462 int flag = 1;
adam@1948 463 setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
adam@1948 464 }
adam@1948 465
grrwlf@2071 466 if(recv_timeout_sec>0) {
grrwlf@2071 467 int ret;
grrwlf@2071 468 struct timeval tv;
grrwlf@2071 469 memset(&tv, 0, sizeof(struct timeval));
grrwlf@2071 470 tv.tv_sec = recv_timeout_sec;
grrwlf@2071 471 ret = setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
grrwlf@2071 472 if(ret != 0) {
grrwlf@2071 473 qfprintf(stderr, "Timeout setting failed, errcode %d errno '%m'\n", ret);
grrwlf@2071 474 }
grrwlf@2071 475 }
grrwlf@2071 476
adam@1948 477 uw_enqueue(new_fd);
adamc@116 478 }
adamc@116 479 }
adamc@102 480 }
adamc@1121 481
adamc@1121 482 void *uw_init_client_data() {
adamc@1121 483 return NULL;
adamc@1121 484 }
adamc@1121 485
adamc@1121 486 void uw_free_client_data(void *data) {
adamc@1121 487 }
adamc@1121 488
adamc@1121 489 void uw_copy_client_data(void *dst, void *src) {
adamc@1121 490 }
adamc@1121 491
adamc@1121 492 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adam@1941 493 uw_ensure_transaction(ctx);
adam@1941 494 uw_get_app(ctx)->expunger(ctx, cli);
adam@1941 495
adam@1941 496 if (uw_commit(ctx))
adam@1941 497 uw_error(ctx, UNLIMITED_RETRY, "Rerunning expunge transaction");
adamc@1121 498 }
adamc@1121 499
adamc@1121 500 void uw_post_expunge(uw_context ctx, void *data) {
adamc@1121 501 }
adam@1320 502
adam@1320 503 int uw_supports_direct_status = 1;