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.c71
1 files changed, 48 insertions, 23 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index aec6aa86..c879a2df 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -216,6 +216,8 @@ MHD_check_global_init_ (void)
216#endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ 216#endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
217#endif 217#endif
218} 218}
219
220
219#endif /* ! _AUTOINIT_FUNCS_ARE_SUPPORTED */ 221#endif /* ! _AUTOINIT_FUNCS_ARE_SUPPORTED */
220 222
221 223
@@ -654,6 +656,7 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
654 return -1; 656 return -1;
655} 657}
656 658
659
657/** 660/**
658 * Initialize security aspects of the HTTPS daemon 661 * Initialize security aspects of the HTTPS daemon
659 * 662 *
@@ -684,6 +687,8 @@ MHD_TLS_init (struct MHD_Daemon *daemon)
684 return -1; 687 return -1;
685 } 688 }
686} 689}
690
691
687#endif /* HTTPS_SUPPORT */ 692#endif /* HTTPS_SUPPORT */
688 693
689 694
@@ -858,6 +863,7 @@ urh_from_fdset (struct MHD_UpgradeResponseHandle *urh,
858 } 863 }
859} 864}
860 865
866
861#ifdef HAVE_POLL 867#ifdef HAVE_POLL
862 868
863/** 869/**
@@ -950,6 +956,8 @@ urh_from_pollfd (struct MHD_UpgradeResponseHandle *urh,
950 if (0 != (p[1].revents & MHD_POLL_REVENTS_ERRROR)) 956 if (0 != (p[1].revents & MHD_POLL_REVENTS_ERRROR))
951 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY | MHD_EPOLL_STATE_WRITE_READY; 957 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY | MHD_EPOLL_STATE_WRITE_READY;
952} 958}
959
960
953#endif /* HAVE_POLL */ 961#endif /* HAVE_POLL */
954#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */ 962#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
955 963
@@ -1236,10 +1244,8 @@ call_handlers (struct MHD_Connection *con,
1236 immediately. 1244 immediately.
1237 As writeability of socket was not checked and it may have 1245 As writeability of socket was not checked and it may have
1238 some data pending in system buffers, use this optimization 1246 some data pending in system buffers, use this optimization
1239 only for non-blocking sockets. */ 1247 only for non-blocking sockets. *//* No need to check 'ret' as connection is always in
1240 /* No need to check 'ret' as connection is always in 1248 * MHD_CONNECTION_CLOSED state if 'ret' is equal 'MHD_NO'. */else if (on_fasttrack && con->sk_nonblck)
1241 * MHD_CONNECTION_CLOSED state if 'ret' is equal 'MHD_NO'. */
1242 else if (on_fasttrack && con->sk_nonblck)
1243 { 1249 {
1244 if (MHD_CONNECTION_HEADERS_SENDING == con->state) 1250 if (MHD_CONNECTION_HEADERS_SENDING == con->state)
1245 { 1251 {
@@ -1312,6 +1318,8 @@ cleanup_upgraded_connection (struct MHD_Connection *connection)
1312 connection->urh = NULL; 1318 connection->urh = NULL;
1313 free (urh); 1319 free (urh);
1314} 1320}
1321
1322
1315#endif /* UPGRADE_SUPPORT */ 1323#endif /* UPGRADE_SUPPORT */
1316 1324
1317 1325
@@ -1371,8 +1379,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh)
1371 * check for any pending data even if socket is not marked 1379 * check for any pending data even if socket is not marked
1372 * as 'ready' (signal may arrive after poll()/select()). 1380 * as 'ready' (signal may arrive after poll()/select()).
1373 * Socketpair for forwarding is always in non-blocking mode 1381 * Socketpair for forwarding is always in non-blocking mode
1374 * so no risk that recv() will block the thread. */ 1382 * so no risk that recv() will block the thread. */if (0 != urh->out_buffer_size)
1375 if (0 != urh->out_buffer_size)
1376 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY; 1383 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY;
1377 /* Discard any data received form remote. */ 1384 /* Discard any data received form remote. */
1378 urh->in_buffer_used = 0; 1385 urh->in_buffer_used = 0;
@@ -1388,11 +1395,8 @@ process_urh (struct MHD_UpgradeResponseHandle *urh)
1388 * fail after remote disconnect was detected) may discard data in system 1395 * fail after remote disconnect was detected) may discard data in system
1389 * buffers received by system but not yet read by recv(). 1396 * buffers received by system but not yet read by recv().
1390 * So, before trying send() on any socket, recv() must be performed at first 1397 * So, before trying send() on any socket, recv() must be performed at first
1391 * otherwise last part of incoming data may be lost. */ 1398 * otherwise last part of incoming data may be lost. *//* If disconnect or error was detected - try to read from socket
1392 1399 * to dry data possibly pending is system buffers. */if (0 != (MHD_EPOLL_STATE_ERROR & urh->app.celi))
1393 /* If disconnect or error was detected - try to read from socket
1394 * to dry data possibly pending is system buffers. */
1395 if (0 != (MHD_EPOLL_STATE_ERROR & urh->app.celi))
1396 urh->app.celi |= MHD_EPOLL_STATE_READ_READY; 1400 urh->app.celi |= MHD_EPOLL_STATE_READ_READY;
1397 if (0 != (MHD_EPOLL_STATE_ERROR & urh->mhd.celi)) 1401 if (0 != (MHD_EPOLL_STATE_ERROR & urh->mhd.celi))
1398 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY; 1402 urh->mhd.celi |= MHD_EPOLL_STATE_READ_READY;
@@ -1669,6 +1673,8 @@ process_urh (struct MHD_UpgradeResponseHandle *urh)
1669 urh->mhd.celi &= ~MHD_EPOLL_STATE_READ_READY; 1673 urh->mhd.celi &= ~MHD_EPOLL_STATE_READ_READY;
1670 } 1674 }
1671} 1675}
1676
1677
1672#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */ 1678#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
1673 1679
1674#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 1680#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
@@ -1823,6 +1829,8 @@ thread_main_connection_upgrade (struct MHD_Connection *con)
1823 /* Do not set 'urh->clean_ready' yet as 'urh' will be used 1829 /* Do not set 'urh->clean_ready' yet as 'urh' will be used
1824 * in connection thread for a little while. */ 1830 * in connection thread for a little while. */
1825} 1831}
1832
1833
1826#endif /* UPGRADE_SUPPORT */ 1834#endif /* UPGRADE_SUPPORT */
1827 1835
1828 1836
@@ -2182,7 +2190,7 @@ thread_main_handle_connection (void *data)
2182 MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN : 2190 MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN :
2183 MHD_REQUEST_TERMINATED_WITH_ERROR); 2191 MHD_REQUEST_TERMINATED_WITH_ERROR);
2184 MHD_connection_handle_idle (con); 2192 MHD_connection_handle_idle (con);
2185 exit: 2193exit:
2186 if (NULL != con->response) 2194 if (NULL != con->response)
2187 { 2195 {
2188 MHD_destroy_response (con->response); 2196 MHD_destroy_response (con->response);
@@ -2208,6 +2216,8 @@ thread_main_handle_connection (void *data)
2208 } 2216 }
2209 return (MHD_THRD_RTRN_TYPE_) 0; 2217 return (MHD_THRD_RTRN_TYPE_) 0;
2210} 2218}
2219
2220
2211#endif 2221#endif
2212 2222
2213 2223
@@ -2248,6 +2258,8 @@ MHD_tls_push_func_ (gnutls_transport_ptr_t trnsp,
2248#endif /* (MHD_SCKT_SEND_MAX_SIZE_ < SSIZE_MAX) || (0 == SSIZE_MAX) */ 2258#endif /* (MHD_SCKT_SEND_MAX_SIZE_ < SSIZE_MAX) || (0 == SSIZE_MAX) */
2249 return MHD_send_ ((MHD_socket) (intptr_t) (trnsp), data, data_size); 2259 return MHD_send_ ((MHD_socket) (intptr_t) (trnsp), data, data_size);
2250} 2260}
2261
2262
2251#endif /* MHD_TLSLIB_DONT_SUPPRESS_SIGPIPE */ 2263#endif /* MHD_TLSLIB_DONT_SUPPRESS_SIGPIPE */
2252 2264
2253 2265
@@ -2327,6 +2339,8 @@ psk_gnutls_adapter (gnutls_session_t session,
2327 return -1; 2339 return -1;
2328#endif 2340#endif
2329} 2341}
2342
2343
2330#endif /* HTTPS_SUPPORT */ 2344#endif /* HTTPS_SUPPORT */
2331 2345
2332 2346
@@ -2742,7 +2756,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
2742#endif 2756#endif
2743 } 2757 }
2744 return MHD_YES; 2758 return MHD_YES;
2745 cleanup: 2759cleanup:
2746 if (NULL != daemon->notify_connection) 2760 if (NULL != daemon->notify_connection)
2747 daemon->notify_connection (daemon->notify_connection_cls, 2761 daemon->notify_connection (daemon->notify_connection_cls,
2748 connection, 2762 connection,
@@ -3223,7 +3237,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
3223 { 3237 {
3224 MHD_socket_close_chk_ (s); 3238 MHD_socket_close_chk_ (s);
3225 } 3239 }
3226 if ( MHD_SCKT_ERR_IS_LOW_RESOURCES_ (err) ) 3240 if (MHD_SCKT_ERR_IS_LOW_RESOURCES_ (err) )
3227 { 3241 {
3228 /* system/process out of resources */ 3242 /* system/process out of resources */
3229 if (0 == daemon->connections) 3243 if (0 == daemon->connections)
@@ -3375,8 +3389,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
3375 this is not true as if we fail to do manually remove it, 3389 this is not true as if we fail to do manually remove it,
3376 we are still seeing an event for this fd in epoll, 3390 we are still seeing an event for this fd in epoll,
3377 causing grief (use-after-free...) --- at least on my 3391 causing grief (use-after-free...) --- at least on my
3378 system. */ 3392 system. */if (0 != epoll_ctl (daemon->epoll_fd,
3379 if (0 != epoll_ctl (daemon->epoll_fd,
3380 EPOLL_CTL_DEL, 3393 EPOLL_CTL_DEL,
3381 pos->socket_fd, 3394 pos->socket_fd,
3382 NULL)) 3395 NULL))
@@ -3632,7 +3645,7 @@ MHD_run_from_select (struct MHD_Daemon *daemon,
3632 if (0 != (daemon->options 3645 if (0 != (daemon->options
3633 & (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_POLL)) ) 3646 & (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_POLL)) )
3634 return MHD_NO; 3647 return MHD_NO;
3635 if ((NULL == read_fd_set)||(NULL == write_fd_set)) 3648 if ((NULL == read_fd_set) || (NULL == write_fd_set))
3636 return MHD_NO; 3649 return MHD_NO;
3637 if (NULL == except_fd_set) 3650 if (NULL == except_fd_set)
3638 { /* Workaround to maintain backward compatibility. */ 3651 { /* Workaround to maintain backward compatibility. */
@@ -3770,6 +3783,8 @@ MHD_select (struct MHD_Daemon *daemon,
3770#if defined(MHD_WINSOCK_SOCKETS) 3783#if defined(MHD_WINSOCK_SOCKETS)
3771 } 3784 }
3772} 3785}
3786
3787
3773#endif /* MHD_WINSOCK_SOCKETS */ 3788#endif /* MHD_WINSOCK_SOCKETS */
3774 } 3789 }
3775 /* Stop listening if we are at the configured connection limit */ 3790 /* Stop listening if we are at the configured connection limit */
@@ -4130,6 +4145,8 @@ MHD_poll_listen_socket (struct MHD_Daemon *daemon,
4130 (void) MHD_accept_connection (daemon); 4145 (void) MHD_accept_connection (daemon);
4131 return MHD_YES; 4146 return MHD_YES;
4132} 4147}
4148
4149
4133#endif 4150#endif
4134 4151
4135 4152
@@ -4319,6 +4336,8 @@ run_epoll_for_upgrade (struct MHD_Daemon *daemon)
4319 4336
4320 return MHD_YES; 4337 return MHD_YES;
4321} 4338}
4339
4340
4322#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */ 4341#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
4323 4342
4324 4343
@@ -4607,8 +4626,7 @@ MHD_epoll (struct MHD_Daemon *daemon,
4607 here. 4626 here.
4608 4627
4609 Connections with custom timeouts must all be looked at, as we 4628 Connections with custom timeouts must all be looked at, as we
4610 do not bother to sort that (presumably very short) list. */ 4629 do not bother to sort that (presumably very short) list. */prev = daemon->manual_timeout_tail;
4611 prev = daemon->manual_timeout_tail;
4612 while (NULL != (pos = prev)) 4630 while (NULL != (pos = prev))
4613 { 4631 {
4614 prev = pos->prevX; 4632 prev = pos->prevX;
@@ -4628,6 +4646,8 @@ MHD_epoll (struct MHD_Daemon *daemon,
4628 } 4646 }
4629 return MHD_YES; 4647 return MHD_YES;
4630} 4648}
4649
4650
4631#endif 4651#endif
4632 4652
4633 4653
@@ -4759,6 +4779,8 @@ MHD_polling_thread (void *cls)
4759 4779
4760 return (MHD_THRD_RTRN_TYPE_) 0; 4780 return (MHD_THRD_RTRN_TYPE_) 0;
4761} 4781}
4782
4783
4762#endif 4784#endif
4763 4785
4764 4786
@@ -5644,6 +5666,8 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
5644 } 5666 }
5645 return MHD_YES; 5667 return MHD_YES;
5646} 5668}
5669
5670
5647#endif 5671#endif
5648 5672
5649 5673
@@ -6553,7 +6577,7 @@ MHD_start_daemon_va (unsigned int flags,
6553 return daemon; 6577 return daemon;
6554 6578
6555#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 6579#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
6556 thread_failed: 6580thread_failed:
6557 /* If no worker threads created, then shut down normally. Calling 6581 /* If no worker threads created, then shut down normally. Calling
6558 MHD_stop_daemon (as we do below) doesn't work here since it 6582 MHD_stop_daemon (as we do below) doesn't work here since it
6559 assumes a 0-sized thread pool means we had been in the default 6583 assumes a 0-sized thread pool means we had been in the default
@@ -6577,7 +6601,7 @@ MHD_start_daemon_va (unsigned int flags,
6577 return NULL; 6601 return NULL;
6578#endif 6602#endif
6579 6603
6580 free_and_fail: 6604free_and_fail:
6581 /* clean up basic memory state in 'daemon' and return NULL to 6605 /* clean up basic memory state in 'daemon' and return NULL to
6582 indicate failure */ 6606 indicate failure */
6583#ifdef EPOLL_SUPPORT 6607#ifdef EPOLL_SUPPORT
@@ -7053,7 +7077,7 @@ MHD_get_version (void)
7053 (((int) MHD_VERSION >> 24) & 0xFF), 7077 (((int) MHD_VERSION >> 24) & 0xFF),
7054 (((int) MHD_VERSION >> 16) & 0xFF), 7078 (((int) MHD_VERSION >> 16) & 0xFF),
7055 (((int) MHD_VERSION >> 8) & 0xFF)); 7079 (((int) MHD_VERSION >> 8) & 0xFF));
7056 if ((0 >= res)||(sizeof(ver) <= res)) 7080 if ((0 >= res) || (sizeof(ver) <= res))
7057 return "0.0.0"; /* Can't return real version*/ 7081 return "0.0.0"; /* Can't return real version*/
7058 } 7082 }
7059 return ver; 7083 return ver;
@@ -7301,7 +7325,7 @@ MHD_init (void)
7301 if (0 != WSAStartup (MAKEWORD (2, 2), &wsd)) 7325 if (0 != WSAStartup (MAKEWORD (2, 2), &wsd))
7302 MHD_PANIC (_ ("Failed to initialize winsock\n")); 7326 MHD_PANIC (_ ("Failed to initialize winsock\n"));
7303 mhd_winsock_inited_ = 1; 7327 mhd_winsock_inited_ = 1;
7304 if ((2 != LOBYTE (wsd.wVersion))&&(2 != HIBYTE (wsd.wVersion))) 7328 if ((2 != LOBYTE (wsd.wVersion)) && (2 != HIBYTE (wsd.wVersion)))
7305 MHD_PANIC (_ ("Winsock version 2.2 is not available\n")); 7329 MHD_PANIC (_ ("Winsock version 2.2 is not available\n"));
7306#endif /* MHD_WINSOCK_SOCKETS */ 7330#endif /* MHD_WINSOCK_SOCKETS */
7307#ifdef HTTPS_SUPPORT 7331#ifdef HTTPS_SUPPORT
@@ -7346,6 +7370,7 @@ MHD_fini (void)
7346 MHD_monotonic_sec_counter_finish (); 7370 MHD_monotonic_sec_counter_finish ();
7347} 7371}
7348 7372
7373
7349#ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED 7374#ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED
7350_SET_INIT_AND_DEINIT_FUNCS (MHD_init, MHD_fini); 7375_SET_INIT_AND_DEINIT_FUNCS (MHD_init, MHD_fini);
7351#endif /* _AUTOINIT_FUNCS_ARE_SUPPORTED */ 7376#endif /* _AUTOINIT_FUNCS_ARE_SUPPORTED */