summaryrefslogtreecommitdiff
path: root/src/ot_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_http.c')
-rw-r--r--src/ot_http.c73
1 files changed, 47 insertions, 26 deletions
diff --git a/src/ot_http.c b/src/ot_http.c
index 96b8f4e..2e21546 100644
--- a/src/ot_http.c
+++ b/src/ot_http.c
@@ -141,9 +141,9 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) {
141 141
142 batch_add(cookie->batch, outbuf, ws->reply_size - written_size); 142 batch_add(cookie->batch, outbuf, ws->reply_size - written_size);
143 143
144 /* writeable short data sockets just have a tcp timeout */ 144 /* Reset any connections potentially near their timeout */
145 if (!ws->keep_alive) 145 if (!ws->keep_alive)
146 fdstate_set_expiry(sock, 0); 146 fdstate_set_expiry(sock, time_future_seconds(OT_CLIENT_TIMEOUT_SEND));
147 http_interest_set(sock, cookie, EVENT_OUT); 147 http_interest_set(sock, cookie, EVENT_OUT);
148 } 148 }
149} 149}
@@ -279,7 +279,6 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
279 {"tcp4", TASK_STATS_TCP}, 279 {"tcp4", TASK_STATS_TCP},
280 {"torr", TASK_STATS_TORRENTS}, 280 {"torr", TASK_STATS_TORRENTS},
281 {"fscr", TASK_STATS_FULLSCRAPE}, 281 {"fscr", TASK_STATS_FULLSCRAPE},
282 {"s24s", TASK_STATS_SLASH24S},
283 {"tpbs", TASK_STATS_TPB}, 282 {"tpbs", TASK_STATS_TPB},
284 {"herr", TASK_STATS_HTTPERRORS}, 283 {"herr", TASK_STATS_HTTPERRORS},
285 {"completed", TASK_STATS_COMPLETED}, 284 {"completed", TASK_STATS_COMPLETED},
@@ -313,7 +312,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
313#endif 312#endif
314 313
315 while (scanon) { 314 while (scanon) {
316 switch (urlencoded_match_keyword(keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM)) { 315 switch (urlencoded_match_keyword(keywords_main, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) {
317 case -2: 316 case -2:
318 scanon = 0; 317 scanon = 0;
319 break; /* TERMINATOR */ 318 break; /* TERMINATOR */
@@ -323,11 +322,11 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
323 urlencoded_skip_value(&read_ptr); 322 urlencoded_skip_value(&read_ptr);
324 break; 323 break;
325 case 1: /* matched "mode" */ 324 case 1: /* matched "mode" */
326 if ((mode = urlencoded_match_keyword(keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) 325 if ((mode = urlencoded_match_keyword(keywords_mode, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0)
327 HTTPERROR_400_PARAM; 326 HTTPERROR_400_PARAM;
328 break; 327 break;
329 case 2: /* matched "format" */ 328 case 2: /* matched "format" */
330 if ((format = urlencoded_match_keyword(keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) 329 if ((format = urlencoded_match_keyword(keywords_format, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0)
331 HTTPERROR_400_PARAM; 330 HTTPERROR_400_PARAM;
332 break; 331 break;
333 case 3: 332 case 3:
@@ -342,6 +341,10 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
342 } 341 }
343 342
344 if (mode == TASK_STATS_TPB) { 343 if (mode == TASK_STATS_TPB) {
344#ifdef WANT_RESTRICT_FULLSCRAPE
345 if (!accesslist_is_blessed(cookie->ip, OT_PERMISSION_MAY_FULLSCRAPE))
346 HTTPERROR_403_IP;
347#endif
345#ifdef WANT_COMPRESSION_GZIP 348#ifdef WANT_COMPRESSION_GZIP
346 ws->request[ws->request_size - 1] = 0; 349 ws->request[ws->request_size - 1] = 0;
347#ifndef WANT_COMPRESSION_GZIP_ALWAYS 350#ifndef WANT_COMPRESSION_GZIP_ALWAYS
@@ -363,6 +366,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
363 a home of its own and only then be made reachable by a worker thread */ 366 a home of its own and only then be made reachable by a worker thread */
364 if (!http_adopt(sock)) 367 if (!http_adopt(sock))
365 HTTPERROR_500; 368 HTTPERROR_500;
369 ws->handed_off = 1;
366 fullscrape_deliver(sock, format); 370 fullscrape_deliver(sock, format);
367 return ws->reply_size = -2; 371 return ws->reply_size = -2;
368 } 372 }
@@ -375,9 +379,11 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c
375 379
376 /* Complex stats also include expensive memory debugging tools */ 380 /* Complex stats also include expensive memory debugging tools */
377 fdstate_set_expiry(sock, 0); 381 fdstate_set_expiry(sock, 0);
382 http_interest_set(sock, cookie, 0);
378 383
379 if (!http_adopt(sock)) 384 if (!http_adopt(sock))
380 HTTPERROR_500; 385 HTTPERROR_500;
386 ws->handed_off = 1;
381 stats_deliver(sock, mode); 387 stats_deliver(sock, mode);
382 return ws->reply_size = -2; 388 return ws->reply_size = -2;
383 } 389 }
@@ -402,6 +408,14 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *
402 struct http_data *cookie = fdstate_cookie(sock); 408 struct http_data *cookie = fdstate_cookie(sock);
403 int format = 0; 409 int format = 0;
404 410
411 if (!cookie)
412 HTTPERROR_500;
413
414#ifdef WANT_RESTRICT_FULLSCRAPE
415 if (!accesslist_is_blessed(cookie->ip, OT_PERMISSION_MAY_FULLSCRAPE))
416 HTTPERROR_403_IP;
417#endif
418
405#ifdef WANT_MODEST_FULLSCRAPES 419#ifdef WANT_MODEST_FULLSCRAPES
406 { 420 {
407 ot_scrape_log this_peer, *new_peer; 421 ot_scrape_log this_peer, *new_peer;
@@ -414,7 +428,7 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *
414 pthread_mutex_unlock(&g_modest_fullscrape_mutex); 428 pthread_mutex_unlock(&g_modest_fullscrape_mutex);
415 HTTPERROR_500; 429 HTTPERROR_500;
416 } 430 }
417 if (exactmatch && (this_peer.last_fullscrape - new_peer->last_fullscrape) < OT_MODEST_PEER_TIMEOUT) { 431 if (exactmatch && time_since_seconds(new_peer->last_fullscrape) < OT_MODEST_PEER_TIMEOUT) {
418 pthread_mutex_unlock(&g_modest_fullscrape_mutex); 432 pthread_mutex_unlock(&g_modest_fullscrape_mutex);
419 HTTPERROR_402_NOTMODEST; 433 HTTPERROR_402_NOTMODEST;
420 } 434 }
@@ -464,6 +478,7 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *
464 478
465 if (!http_adopt(sock)) 479 if (!http_adopt(sock))
466 HTTPERROR_500; 480 HTTPERROR_500;
481 ws->handed_off = 1;
467 fullscrape_deliver(sock, format); 482 fullscrape_deliver(sock, format);
468 return ws->reply_size = -2; 483 return ws->reply_size = -2;
469} 484}
@@ -489,7 +504,7 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws,
489 } 504 }
490 505
491 while (scanon) { 506 while (scanon) {
492 switch (urlencoded_match_keyword(keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM)) { 507 switch (urlencoded_match_keyword(keywords_scrape, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) {
493 case -2: 508 case -2:
494 scanon = 0; 509 scanon = 0;
495 break; /* TERMINATOR */ 510 break; /* TERMINATOR */
@@ -499,10 +514,12 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws,
499 urlencoded_skip_value(&read_ptr); 514 urlencoded_skip_value(&read_ptr);
500 break; 515 break;
501 case 1: /* matched "info_hash" */ 516 case 1: /* matched "info_hash" */
517 {
502 /* ignore this, when we have less than 20 bytes */ 518 /* ignore this, when we have less than 20 bytes */
503 if (urlencoded_parse(&read_ptr, (char *)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) 519 ot_hash *slot = multiscrape_buf + numwant++;
520 if (urlencoded_parse(&read_ptr, (char *)slot, (char *)(slot + 1), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash))
504 HTTPERROR_400_PARAM; 521 HTTPERROR_400_PARAM;
505 break; 522 } break;
506 } 523 }
507 } 524 }
508 525
@@ -519,7 +536,7 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws,
519 qsort(multiscrape_buf, numwant, sizeof(ot_hash), compare_hash); 536 qsort(multiscrape_buf, numwant, sizeof(ot_hash), compare_hash);
520 for (i = unique = 1; i < numwant; ++i) 537 for (i = unique = 1; i < numwant; ++i)
521 if (memcmp(multiscrape_buf[i], multiscrape_buf[unique - 1], sizeof(ot_hash))) 538 if (memcmp(multiscrape_buf[i], multiscrape_buf[unique - 1], sizeof(ot_hash)))
522 memcpy(multiscrape_buf[unique++], multiscrape_buf[i], sizeof(ot_hash)); 539 memmove(multiscrape_buf[unique++], multiscrape_buf[i], sizeof(ot_hash));
523 numwant = unique; 540 numwant = unique;
524 541
525 /* Enough for http header + whole scrape string */ 542 /* Enough for http header + whole scrape string */
@@ -606,7 +623,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
606 scanon = 1; 623 scanon = 1;
607 624
608 while (scanon) { 625 while (scanon) {
609 switch (urlencoded_match_keyword(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM)) { 626 switch (urlencoded_match_keyword(keywords_announce, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) {
610 case -2: 627 case -2:
611 scanon = 0; 628 scanon = 0;
612 break; /* TERMINATOR */ 629 break; /* TERMINATOR */
@@ -616,14 +633,14 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
616 urlencoded_skip_value(&read_ptr); 633 urlencoded_skip_value(&read_ptr);
617 break; 634 break;
618 case 1: /* matched "port" */ 635 case 1: /* matched "port" */
619 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); 636 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE);
620 if ((len <= 0) || scan_int(write_ptr, len, &tmp) || (tmp > 0xffff)) 637 if ((len <= 0) || scan_int(write_ptr, len, &tmp) || (tmp < 0) || (tmp > 0xffff))
621 HTTPERROR_400_PARAM; 638 HTTPERROR_400_PARAM;
622 port = htons((uint16_t)tmp); 639 port = htons((uint16_t)tmp);
623 OT_SETPORT(&ws->peer, &port); 640 OT_SETPORT(&ws->peer, &port);
624 break; 641 break;
625 case 2: /* matched "left" */ 642 case 2: /* matched "left" */
626 if ((len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) 643 if ((len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0)
627 HTTPERROR_400_PARAM; 644 HTTPERROR_400_PARAM;
628 if (scan_int(write_ptr, len, &tmp)) 645 if (scan_int(write_ptr, len, &tmp))
629 tmp = 0; 646 tmp = 0;
@@ -631,7 +648,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
631 OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; 648 OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING;
632 break; 649 break;
633 case 3: /* matched "event" */ 650 case 3: /* matched "event" */
634 switch (urlencoded_match_keyword(keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE)) { 651 switch (urlencoded_match_keyword(keywords_announce_event, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) {
635 case -1: 652 case -1:
636 HTTPERROR_400_PARAM; 653 HTTPERROR_400_PARAM;
637 case 1: /* matched "completed" */ 654 case 1: /* matched "completed" */
@@ -645,7 +662,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
645 } 662 }
646 break; 663 break;
647 case 4: /* matched "numwant" */ 664 case 4: /* matched "numwant" */
648 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); 665 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE);
649 if ((len <= 0) || scan_int(write_ptr, len, &tmp)) 666 if ((len <= 0) || scan_int(write_ptr, len, &tmp))
650 HTTPERROR_400_PARAM; 667 HTTPERROR_400_PARAM;
651 if (tmp < 0) 668 if (tmp < 0)
@@ -655,7 +672,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
655 numwant = (size_t)tmp; 672 numwant = (size_t)tmp;
656 break; 673 break;
657 case 5: /* matched "compact" */ 674 case 5: /* matched "compact" */
658 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); 675 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE);
659 if ((len <= 0) || scan_int(write_ptr, len, &tmp)) 676 if ((len <= 0) || scan_int(write_ptr, len, &tmp))
660 HTTPERROR_400_PARAM; 677 HTTPERROR_400_PARAM;
661 if (!tmp) 678 if (!tmp)
@@ -665,7 +682,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
665 if (ws->hash) 682 if (ws->hash)
666 HTTPERROR_400_DOUBLEHASH; 683 HTTPERROR_400_DOUBLEHASH;
667 /* ignore this, when we have less than 20 bytes */ 684 /* ignore this, when we have less than 20 bytes */
668 if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) 685 if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE) != 20)
669 HTTPERROR_400_PARAM; 686 HTTPERROR_400_PARAM;
670 ws->hash = (ot_hash *)write_ptr; 687 ws->hash = (ot_hash *)write_ptr;
671 break; 688 break;
@@ -673,16 +690,19 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct
673 case 7: /* matched "ip" */ 690 case 7: /* matched "ip" */
674 { 691 {
675 char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; 692 char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16;
676 len = urlencoded_parse(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); 693 /* One short of the end, for the terminator below */
694 len = urlencoded_parse(&read_ptr, tmp_buf2, ws->outbuf + G_OUTBUF_SIZE - 1, SCAN_SEARCHPATH_VALUE);
695 if (len <= 0)
696 HTTPERROR_400_PARAM;
677 tmp_buf2[len] = 0; 697 tmp_buf2[len] = 0;
678 if ((len <= 0) || ip_parse(tmp_buf2, tmp_buf1) != (size_t)len) 698 if (ip_parse(tmp_buf2, tmp_buf1) != (size_t)len)
679 HTTPERROR_400_PARAM; 699 HTTPERROR_400_PARAM;
680 OT_SETIP(&ws->peer, tmp_buf1); 700 OT_SETIP(&ws->peer, tmp_buf1);
681 } break; 701 } break;
682#endif 702#endif
683 case 8: /* matched "peer_id" */ 703 case 8: /* matched "peer_id" */
684 /* ignore this, when we have less than 20 bytes */ 704 /* ignore this, when we have less than 20 bytes */
685 if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) 705 if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE) != 20)
686 HTTPERROR_400_PARAM; 706 HTTPERROR_400_PARAM;
687 ws->peer_id = write_ptr; 707 ws->peer_id = write_ptr;
688 break; 708 break;
@@ -729,6 +749,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) {
729 subroutine gets to answer: an error reply finishes with the connection 749 subroutine gets to answer: an error reply finishes with the connection
730 on its way out, and ws outlives the connection it was serving */ 750 on its way out, and ws outlives the connection it was serving */
731 ws->keep_alive = 0; 751 ws->keep_alive = 0;
752 ws->handed_off = 0;
732#ifdef WANT_KEEPALIVE 753#ifdef WANT_KEEPALIVE
733 if ((write_ptr = http_header(ws->request, ws->header_size, "connection")) && (*write_ptr == 'K' || *write_ptr == 'k')) 754 if ((write_ptr = http_header(ws->request, ws->header_size, "connection")) && (*write_ptr == 'K' || *write_ptr == 'k'))
734 ws->keep_alive = 1; 755 ws->keep_alive = 1;
@@ -743,8 +764,8 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) {
743 } 764 }
744 } 765 }
745 766
746 /* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */ 767 /* The request need not be NUL terminated and can be shorter than the literal */
747 if (memcmp(read_ptr, "GET /", 5)) 768 if (ws->request_size < 5 || memcmp(read_ptr, "GET /", 5))
748 HTTPERROR_400; 769 HTTPERROR_400;
749 770
750 /* Skip leading '/' */ 771 /* Skip leading '/' */
@@ -755,7 +776,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) {
755 In reality we abandoned requiring the url to be correct. This now 776 In reality we abandoned requiring the url to be correct. This now
756 only decodes url encoded characters, we check for announces and 777 only decodes url encoded characters, we check for announces and
757 scrapes by looking for "a*" or "sc" */ 778 scrapes by looking for "a*" or "sc" */
758 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_PATH); 779 len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_PATH);
759 780
760 /* If parsing returned an error, leave with not found */ 781 /* If parsing returned an error, leave with not found */
761 if (g_redirecturl && (len == -2)) 782 if (g_redirecturl && (len == -2))
@@ -767,7 +788,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) {
767 if ((*write_ptr == 'a') || (*write_ptr == '?')) 788 if ((*write_ptr == 'a') || (*write_ptr == '?'))
768 http_handle_announce(sock, ws, read_ptr); 789 http_handle_announce(sock, ws, read_ptr);
769#ifdef WANT_FULLSCRAPE 790#ifdef WANT_FULLSCRAPE
770 else if (!memcmp(write_ptr, "scrape HTTP/", 12)) 791 else if (ws->request + ws->request_size - write_ptr >= 12 && !memcmp(write_ptr, "scrape HTTP/", 12))
771 http_handle_fullscrape(sock, ws); 792 http_handle_fullscrape(sock, ws);
772#endif 793#endif
773 /* This is the hardcore match for scrape */ 794 /* This is the hardcore match for scrape */