aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2007-08-30 01:20:29 +0000
committerChristian Grothoff <christian@grothoff.org>2007-08-30 01:20:29 +0000
commit44f694198ba0d2b9b7172468989a83f6e1e51929 (patch)
treeafb1b269a1ff126a0b36dc1e499f1c8f2e458241
parent269a12fae548ba588b718710e181951018308d47 (diff)
downloadlibmicrohttpd-44f694198ba0d2b9b7172468989a83f6e1e51929.tar.gz
libmicrohttpd-44f694198ba0d2b9b7172468989a83f6e1e51929.zip
do not accept forever
-rw-r--r--src/daemon/daemon.c155
1 files changed, 73 insertions, 82 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 79963d6b..35eeccce 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -447,7 +447,6 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
447 unsigned long long ltimeout; 447 unsigned long long ltimeout;
448 int ds; 448 int ds;
449 time_t now; 449 time_t now;
450 int go_again;
451 450
452 timeout.tv_sec = 0; 451 timeout.tv_sec = 0;
453 timeout.tv_usec = 0; 452 timeout.tv_usec = 0;
@@ -455,88 +454,80 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
455 abort (); 454 abort ();
456 if (daemon->shutdown == MHD_YES) 455 if (daemon->shutdown == MHD_YES)
457 return MHD_NO; 456 return MHD_NO;
458 go_again = MHD_YES; 457 FD_ZERO (&rs);
459 while (go_again == MHD_YES) 458 FD_ZERO (&ws);
459 FD_ZERO (&es);
460 max = 0;
461
462 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
460 { 463 {
461 go_again = MHD_NO; 464 /* single-threaded, go over everything */
462 FD_ZERO (&rs); 465 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max))
463 FD_ZERO (&ws); 466 return MHD_NO;
464 FD_ZERO (&es); 467 }
465 max = 0; 468 else
466 469 {
467 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 470 /* accept only, have one thread per connection */
468 { 471 max = daemon->socket_fd;
469 /* single-threaded, go over everything */ 472 if (max == -1)
470 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max)) 473 return MHD_NO;
471 return MHD_NO; 474 FD_SET (max, &rs);
472 } 475 }
473 else 476 if (may_block == MHD_NO)
474 { 477 {
475 /* accept only, have one thread per connection */ 478 timeout.tv_usec = 0;
476 max = daemon->socket_fd; 479 timeout.tv_sec = 0;
477 if (max == -1) 480 }
478 return MHD_NO; 481 else
479 FD_SET (max, &rs); 482 {
480 } 483 /* ltimeout is in ms */
481 if (may_block == MHD_NO) 484 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout))
482 { 485 {
483 timeout.tv_usec = 0; 486 timeout.tv_usec = (ltimeout % 1000) * 1000;
484 timeout.tv_sec = 0; 487 timeout.tv_sec = ltimeout / 1000;
485 } 488 may_block = MHD_NO;
486 else 489 }
487 { 490 }
488 /* ltimeout is in ms */ 491 num_ready = SELECT (max + 1,
489 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout)) 492 &rs, &ws, &es,
490 { 493 may_block == MHD_NO ? &timeout : NULL);
491 timeout.tv_usec = (ltimeout % 1000) * 1000; 494 if (daemon->shutdown == MHD_YES)
492 timeout.tv_sec = ltimeout / 1000; 495 return MHD_NO;
493 may_block = MHD_NO; 496 if (num_ready < 0)
494 } 497 {
495 } 498 if (errno == EINTR)
496 num_ready = SELECT (max + 1, 499 return MHD_YES;
497 &rs, &ws, &es, 500 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno));
498 may_block == MHD_NO ? &timeout : NULL); 501 return MHD_NO;
499 if (daemon->shutdown == MHD_YES) 502 }
500 return MHD_NO; 503 ds = daemon->socket_fd;
501 if (num_ready < 0) 504 if (ds == -1)
502 { 505 return MHD_YES;
503 if (errno == EINTR) 506 if (FD_ISSET (ds, &rs))
504 return MHD_YES; 507 MHD_accept_connection (daemon);
505 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); 508 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
506 return MHD_NO; 509 {
507 } 510 /* do not have a thread per connection, process all connections now */
508 ds = daemon->socket_fd; 511 now = time (NULL);
509 if (ds == -1) 512 pos = daemon->connections;
510 return MHD_YES; 513 while (pos != NULL)
511 if (FD_ISSET (ds, &rs)) 514 {
512 { 515 ds = pos->socket_fd;
513 MHD_accept_connection (daemon); 516 if (ds != -1)
514 go_again = MHD_YES; 517 {
515 } 518 if (FD_ISSET (ds, &rs))
516 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 519 {
517 { 520 pos->last_activity = now;
518 /* do not have a thread per connection, process all connections now */ 521 MHD_connection_handle_read (pos);
519 now = time (NULL); 522 }
520 pos = daemon->connections; 523 if (FD_ISSET (ds, &ws))
521 while (pos != NULL) 524 {
522 { 525 pos->last_activity = now;
523 ds = pos->socket_fd; 526 MHD_connection_handle_write (pos);
524 if (ds != -1) 527 }
525 { 528 }
526 if (FD_ISSET (ds, &rs)) 529 pos = pos->next;
527 { 530 }
528 pos->last_activity = now;
529 MHD_connection_handle_read (pos);
530 }
531 if (FD_ISSET (ds, &ws))
532 {
533 pos->last_activity = now;
534 MHD_connection_handle_write (pos);
535 }
536 }
537 pos = pos->next;
538 }
539 }
540 } 531 }
541 return MHD_YES; 532 return MHD_YES;
542} 533}