aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/daemon.c')
-rw-r--r--src/microhttpd/daemon.c118
1 files changed, 47 insertions, 71 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index f3578c7e..ffd2a5a7 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -1422,18 +1422,18 @@ thread_main_handle_connection (void *data)
1422 (MHD_CONNECTION_CLOSED != con->state) ) 1422 (MHD_CONNECTION_CLOSED != con->state) )
1423 { 1423 {
1424 const unsigned int timeout = daemon->connection_timeout; 1424 const unsigned int timeout = daemon->connection_timeout;
1425 int was_suspended = MHD_NO; 1425 bool was_suspended = false;
1426#ifdef UPGRADE_SUPPORT 1426#ifdef UPGRADE_SUPPORT
1427 struct MHD_UpgradeResponseHandle * const urh = con->urh; 1427 struct MHD_UpgradeResponseHandle * const urh = con->urh;
1428#else /* ! UPGRADE_SUPPORT */ 1428#else /* ! UPGRADE_SUPPORT */
1429 static const void * const urh = NULL; 1429 static const void * const urh = NULL;
1430#endif /* ! UPGRADE_SUPPORT */ 1430#endif /* ! UPGRADE_SUPPORT */
1431 1431
1432 if ( (MHD_NO != con->suspended) && 1432 if ( (con->suspended) &&
1433 (NULL == urh) ) 1433 (NULL == urh) )
1434 { 1434 {
1435 /* Connection was suspended, wait for resume. */ 1435 /* Connection was suspended, wait for resume. */
1436 was_suspended = MHD_YES; 1436 was_suspended = true;
1437 if (! use_poll) 1437 if (! use_poll)
1438 { 1438 {
1439 FD_ZERO (&rs); 1439 FD_ZERO (&rs);
@@ -1492,11 +1492,11 @@ thread_main_handle_connection (void *data)
1492 continue; /* Check again for resume. */ 1492 continue; /* Check again for resume. */
1493 } /* End of "suspended" branch. */ 1493 } /* End of "suspended" branch. */
1494 1494
1495 if ( (MHD_YES == was_suspended) && 1495 if ( (was_suspended) &&
1496 (0 != con->connection_timeout) ) 1496 (0 != con->connection_timeout) )
1497 { 1497 {
1498 con->last_activity = MHD_monotonic_sec_counter(); /* Reset timeout timer. */ 1498 con->last_activity = MHD_monotonic_sec_counter(); /* Reset timeout timer. */
1499 was_suspended = MHD_NO; 1499 was_suspended = false;
1500 } 1500 }
1501 1501
1502 tvp = NULL; 1502 tvp = NULL;
@@ -2198,21 +2198,17 @@ internal_add_connection (struct MHD_Daemon *daemon,
2198 } 2198 }
2199 2199
2200 2200
2201 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2201 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2202 { 2202 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2203 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2204 }
2205 else
2206 { 2203 {
2207 XDLL_insert (daemon->normal_timeout_head, 2204 XDLL_insert (daemon->normal_timeout_head,
2208 daemon->normal_timeout_tail, 2205 daemon->normal_timeout_tail,
2209 connection); 2206 connection);
2210 } 2207 }
2211 DLL_insert (daemon->connections_head, 2208 DLL_insert (daemon->connections_head,
2212 daemon->connections_tail, 2209 daemon->connections_tail,
2213 connection); 2210 connection);
2214 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2211 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2215 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2216 2212
2217 if (NULL != daemon->notify_connection) 2213 if (NULL != daemon->notify_connection)
2218 daemon->notify_connection (daemon->notify_connection_cls, 2214 daemon->notify_connection (daemon->notify_connection_cls,
@@ -2294,11 +2290,8 @@ internal_add_connection (struct MHD_Daemon *daemon,
2294 MHD_ip_limit_del (daemon, 2290 MHD_ip_limit_del (daemon,
2295 addr, 2291 addr,
2296 addrlen); 2292 addrlen);
2297 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2293 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2298 { 2294 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2299 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2300 }
2301 else
2302 { 2295 {
2303 XDLL_remove (daemon->normal_timeout_head, 2296 XDLL_remove (daemon->normal_timeout_head,
2304 daemon->normal_timeout_tail, 2297 daemon->normal_timeout_tail,
@@ -2307,8 +2300,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
2307 DLL_remove (daemon->connections_head, 2300 DLL_remove (daemon->connections_head,
2308 daemon->connections_tail, 2301 daemon->connections_tail,
2309 connection); 2302 connection);
2310 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2303 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2311 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2312 MHD_pool_destroy (connection->pool); 2304 MHD_pool_destroy (connection->pool);
2313 free (connection->addr); 2305 free (connection->addr);
2314 free (connection); 2306 free (connection);
@@ -2356,11 +2348,8 @@ MHD_suspend_connection (struct MHD_Connection *connection)
2356 daemon = connection->daemon; 2348 daemon = connection->daemon;
2357 if (MHD_ALLOW_SUSPEND_RESUME != (daemon->options & MHD_ALLOW_SUSPEND_RESUME)) 2349 if (MHD_ALLOW_SUSPEND_RESUME != (daemon->options & MHD_ALLOW_SUSPEND_RESUME))
2358 MHD_PANIC (_("Cannot suspend connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n")); 2350 MHD_PANIC (_("Cannot suspend connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n"));
2359 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2351 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2360 { 2352 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2361 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2362 }
2363 else
2364 { 2353 {
2365 if (connection->connection_timeout == daemon->connection_timeout) 2354 if (connection->connection_timeout == daemon->connection_timeout)
2366 XDLL_remove (daemon->normal_timeout_head, 2355 XDLL_remove (daemon->normal_timeout_head,
@@ -2399,9 +2388,8 @@ MHD_suspend_connection (struct MHD_Connection *connection)
2399 connection->epoll_state |= MHD_EPOLL_STATE_SUSPENDED; 2388 connection->epoll_state |= MHD_EPOLL_STATE_SUSPENDED;
2400 } 2389 }
2401#endif 2390#endif
2402 connection->suspended = MHD_YES; 2391 connection->suspended = true;
2403 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2392 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2404 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2405} 2393}
2406 2394
2407 2395
@@ -2421,12 +2409,10 @@ MHD_resume_connection (struct MHD_Connection *connection)
2421 daemon = connection->daemon; 2409 daemon = connection->daemon;
2422 if (MHD_ALLOW_SUSPEND_RESUME != (daemon->options & MHD_ALLOW_SUSPEND_RESUME)) 2410 if (MHD_ALLOW_SUSPEND_RESUME != (daemon->options & MHD_ALLOW_SUSPEND_RESUME))
2423 MHD_PANIC (_("Cannot resume connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n")); 2411 MHD_PANIC (_("Cannot resume connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n"));
2424 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2412 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2425 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 2413 connection->resuming = true;
2426 connection->resuming = MHD_YES; 2414 daemon->resuming = true;
2427 daemon->resuming = MHD_YES; 2415 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2428 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2429 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2430 if ( (MHD_ITC_IS_VALID_(daemon->itc)) && 2416 if ( (MHD_ITC_IS_VALID_(daemon->itc)) &&
2431 (! MHD_itc_activate_ (daemon->itc, "r")) ) 2417 (! MHD_itc_activate_ (daemon->itc, "r")) )
2432 { 2418 {
@@ -2455,10 +2441,9 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
2455 int ret; 2441 int ret;
2456 2442
2457 ret = MHD_NO; 2443 ret = MHD_NO;
2458 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2444 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2459 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2460 2445
2461 if (MHD_NO != daemon->resuming) 2446 if (daemon->resuming)
2462 next = daemon->suspended_connections_head; 2447 next = daemon->suspended_connections_head;
2463 /* Clear the flag *only* if connections will be resumed otherwise 2448 /* Clear the flag *only* if connections will be resumed otherwise
2464 it may accidentally clear flag that was set at the same time in 2449 it may accidentally clear flag that was set at the same time in
@@ -2469,7 +2454,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
2469 function at this time so clearing flag at end will clear it without 2454 function at this time so clearing flag at end will clear it without
2470 actually resuming of new connection. */ 2455 actually resuming of new connection. */
2471 if (NULL != next) 2456 if (NULL != next)
2472 daemon->resuming = MHD_NO; 2457 daemon->resuming = false;
2473 while (NULL != (pos = next)) 2458 while (NULL != (pos = next))
2474 { 2459 {
2475#ifdef UPGRADE_SUPPORT 2460#ifdef UPGRADE_SUPPORT
@@ -2478,7 +2463,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
2478 static const void * const urh = NULL; 2463 static const void * const urh = NULL;
2479#endif /* ! UPGRADE_SUPPORT */ 2464#endif /* ! UPGRADE_SUPPORT */
2480 next = pos->next; 2465 next = pos->next;
2481 if ( (MHD_NO == pos->resuming) 2466 if ( (! pos->resuming)
2482#ifdef UPGRADE_SUPPORT 2467#ifdef UPGRADE_SUPPORT
2483 || ( (NULL != urh) && 2468 || ( (NULL != urh) &&
2484 ( (MHD_NO == urh->was_closed) || 2469 ( (MHD_NO == urh->was_closed) ||
@@ -2533,21 +2518,19 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
2533 2518
2534 } 2519 }
2535#endif /* UPGRADE_SUPPORT */ 2520#endif /* UPGRADE_SUPPORT */
2536 pos->suspended = MHD_NO; 2521 pos->suspended = false;
2537 pos->resuming = MHD_NO; 2522 pos->resuming = false;
2538 } 2523 }
2539 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2524 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2540 { 2525 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2541 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); 2526 (MHD_NO != ret) )
2542 if (MHD_NO != ret) 2527 { /* Wake up suspended connections. */
2543 { /* Wake up suspended connections. */ 2528 if (! MHD_itc_activate_(daemon->itc,
2544 if (! MHD_itc_activate_(daemon->itc, 2529 "w"))
2545 "w"))
2546#ifdef HAVE_MESSAGES 2530#ifdef HAVE_MESSAGES
2547 MHD_DLOG (daemon, 2531 MHD_DLOG (daemon,
2548 _("Failed to signal resume of connection via inter-thread communication channel.")); 2532 _("Failed to signal resume of connection via inter-thread communication channel."));
2549#endif 2533#endif
2550 }
2551 } 2534 }
2552 return ret; 2535 return ret;
2553} 2536}
@@ -2763,16 +2746,13 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
2763{ 2746{
2764 struct MHD_Connection *pos; 2747 struct MHD_Connection *pos;
2765 2748
2766 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2749 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2767 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2768 while (NULL != (pos = daemon->cleanup_head)) 2750 while (NULL != (pos = daemon->cleanup_head))
2769 { 2751 {
2770 DLL_remove (daemon->cleanup_head, 2752 DLL_remove (daemon->cleanup_head,
2771 daemon->cleanup_tail, 2753 daemon->cleanup_tail,
2772 pos); 2754 pos);
2773 2755 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2774 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2775 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2776 2756
2777 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 2757 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2778 (MHD_NO == pos->thread_joined) && 2758 (MHD_NO == pos->thread_joined) &&
@@ -2838,11 +2818,9 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
2838 free (pos->addr); 2818 free (pos->addr);
2839 free (pos); 2819 free (pos);
2840 2820
2841 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2821 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2842 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2843 } 2822 }
2844 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2823 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2845 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
2846} 2824}
2847 2825
2848 2826
@@ -5525,13 +5503,12 @@ close_all_connections (struct MHD_Daemon *daemon)
5525 resumed connection. */ 5503 resumed connection. */
5526 if (0 != (MHD_ALLOW_SUSPEND_RESUME & daemon->options)) 5504 if (0 != (MHD_ALLOW_SUSPEND_RESUME & daemon->options))
5527 { 5505 {
5528 daemon->resuming = MHD_YES; /* Force check for pending resume. */ 5506 daemon->resuming = true; /* Force check for pending resume. */
5529 resume_suspended_connections (daemon); 5507 resume_suspended_connections (daemon);
5530 } 5508 }
5531 /* first, make sure all threads are aware of shutdown; need to 5509 /* first, make sure all threads are aware of shutdown; need to
5532 traverse DLLs in peace... */ 5510 traverse DLLs in peace... */
5533 if (used_thr_p_c) 5511 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
5534 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
5535#ifdef UPGRADE_SUPPORT 5512#ifdef UPGRADE_SUPPORT
5536 if (upg_allowed) 5513 if (upg_allowed)
5537 { 5514 {
@@ -5563,8 +5540,8 @@ close_all_connections (struct MHD_Daemon *daemon)
5563 MHD_connection_finish_forward_ (susp); 5540 MHD_connection_finish_forward_ (susp);
5564 /* Do not use MHD_resume_connection() as mutex is 5541 /* Do not use MHD_resume_connection() as mutex is
5565 * already locked. */ 5542 * already locked. */
5566 susp->resuming = MHD_YES; 5543 susp->resuming = true;
5567 daemon->resuming = MHD_YES; 5544 daemon->resuming = true;
5568 } 5545 }
5569 susp = susp->next; 5546 susp = susp->next;
5570 } 5547 }
@@ -5584,8 +5561,7 @@ close_all_connections (struct MHD_Daemon *daemon)
5584 MHD_PANIC (_("Failed to signal shutdown via inter-thread communication channel")); 5561 MHD_PANIC (_("Failed to signal shutdown via inter-thread communication channel"));
5585#endif 5562#endif
5586 } 5563 }
5587 if (used_thr_p_c) 5564 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
5588 MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
5589 5565
5590 /* now, collect per-connection threads */ 5566 /* now, collect per-connection threads */
5591 if (used_thr_p_c) 5567 if (used_thr_p_c)
@@ -5614,7 +5590,7 @@ close_all_connections (struct MHD_Daemon *daemon)
5614 * application should be moved to cleanup list too. */ 5590 * application should be moved to cleanup list too. */
5615 if (upg_allowed) 5591 if (upg_allowed)
5616 { 5592 {
5617 daemon->resuming = MHD_YES; /* Force check for pending resume. */ 5593 daemon->resuming = true; /* Force check for pending resume. */
5618 resume_suspended_connections (daemon); 5594 resume_suspended_connections (daemon);
5619 } 5595 }
5620#endif /* UPGRADE_SUPPORT */ 5596#endif /* UPGRADE_SUPPORT */