/** * Give gnuTLS chance to work on the TLS handshake. * * @param connection connection to handshake on * @return #MHD_YES on error or if the handshake is progressing * #MHD_NO if the handshake has completed successfully * and we should start to read/write data */ static int run_tls_handshake (struct MHD_Connection *connection) { int ret; connection->last_activity = MHD_monotonic_sec_counter (); if (MHD_TLS_CONNECTION_INIT == connection->state) { ret = gnutls_handshake (connection->tls_session); if (ret == GNUTLS_E_SUCCESS) { /* set connection state to enable HTTP processing */ connection->state = MHD_CONNECTION_INIT; return MHD_YES; } if ( (GNUTLS_E_AGAIN == ret) || (GNUTLS_E_INTERRUPTED == ret) ) { /* handshake not done */ return MHD_YES; } /* handshake failed */ #ifdef HAVE_MESSAGES MHD_DLOG (connection->daemon, _("Error: received handshake message out of context\n")); #endif MHD_connection_close_ (connection, MHD_REQUEST_TERMINATED_WITH_ERROR); return MHD_YES; } return MHD_NO; }
/** * This function was created to handle per-connection processing that * has to happen even if the socket cannot be read or written to. All * implementations (multithreaded, external select, internal select) * call this function. * * @param connection being handled * @return #MHD_YES if we should continue to process the * connection (not dead yet), #MHD_NO if it died */ static int MHD_tls_connection_handle_idle (struct MHD_Connection *connection) { unsigned int timeout; #if DEBUG_STATES MHD_DLOG (connection->daemon, _("In function %s handling connection at state: %s\n"), __FUNCTION__, MHD_state_to_string (connection->state)); #endif timeout = connection->connection_timeout; if ( (timeout != 0) && (timeout <= (MHD_monotonic_sec_counter() - connection->last_activity))) MHD_connection_close_ (connection, MHD_REQUEST_TERMINATED_TIMEOUT_REACHED); switch (connection->state) { /* on newly created connections we might reach here before any reply has been received */ case MHD_TLS_CONNECTION_INIT: break; /* close connection if necessary */ case MHD_CONNECTION_CLOSED: return MHD_connection_handle_idle (connection); default: if ( (0 != gnutls_record_check_pending (connection->tls_session)) && (MHD_YES != MHD_tls_connection_handle_read (connection)) ) return MHD_YES; return MHD_connection_handle_idle (connection); } #ifdef EPOLL_SUPPORT return MHD_connection_epoll_update_ (connection); #else return MHD_YES; #endif }
/** * Run through the suspended connections and move any that are no * longer suspended back to the active state. * * @remark To be called only from thread that process * daemon's select()/poll()/etc. * * @param daemon daemon context * @return true if a connection was actually resumed */ bool MHD_resume_suspended_connections_ (struct MHD_Daemon *daemon) /* FIXME: rename connections -> requests? */ { struct MHD_Connection *pos; struct MHD_Connection *prev = NULL; bool ret; const bool used_thr_p_c = (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode); mhd_assert (NULL == daemon->worker_pool); ret = false; MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); if (daemon->resuming) { prev = daemon->suspended_connections_tail; /* During shutdown check for resuming is forced. */ mhd_assert((NULL != prev) || (daemon->shutdown)); } daemon->resuming = false; while (NULL != (pos = prev)) { #ifdef UPGRADE_SUPPORT struct MHD_UpgradeResponseHandle * const urh = pos->request.urh; #else /* ! UPGRADE_SUPPORT */ static const void * const urh = NULL; #endif /* ! UPGRADE_SUPPORT */ prev = pos->prev; if ( (! pos->resuming) #ifdef UPGRADE_SUPPORT || ( (NULL != urh) && ( (! urh->was_closed) || (! urh->clean_ready) ) ) #endif /* UPGRADE_SUPPORT */ ) continue; ret = true; mhd_assert (pos->suspended); DLL_remove (daemon->suspended_connections_head, daemon->suspended_connections_tail, pos); pos->suspended = false; if (NULL == urh) { DLL_insert (daemon->connections_head, daemon->connections_tail, pos); if (! used_thr_p_c) { /* Reset timeout timer on resume. */ if (0 != pos->connection_timeout) pos->last_activity = MHD_monotonic_sec_counter(); if (pos->connection_timeout == daemon->connection_default_timeout) XDLL_insert (daemon->normal_timeout_head, daemon->normal_timeout_tail, pos); else XDLL_insert (daemon->manual_timeout_head, daemon->manual_timeout_tail, pos); } #ifdef EPOLL_SUPPORT if (MHD_ELS_EPOLL == daemon->event_loop_syscall) { if (0 != (pos->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL)) MHD_PANIC ("Resumed connection was already in EREADY set\n"); /* we always mark resumed connections as ready, as we might have missed the edge poll event during suspension */ EDLL_insert (daemon->eready_head, daemon->eready_tail, pos); pos->epoll_state |= MHD_EPOLL_STATE_IN_EREADY_EDLL | \ MHD_EPOLL_STATE_READ_READY | MHD_EPOLL_STATE_WRITE_READY; pos->epoll_state &= ~MHD_EPOLL_STATE_SUSPENDED; } #endif } #ifdef UPGRADE_SUPPORT else { struct MHD_Response *response = pos->request.response; /* Data forwarding was finished (for TLS connections) AND * application was closed upgraded connection. * Insert connection into cleanup list. */ if ( (NULL != response) && (MHD_TM_THREAD_PER_CONNECTION != daemon->threading_mode) && (NULL != response->termination_cb) ) response->termination_cb (response->termination_cb_cls, MHD_REQUEST_TERMINATED_COMPLETED_OK, &pos->request.client_context); DLL_insert (daemon->cleanup_head, daemon->cleanup_tail, pos); } #endif /* UPGRADE_SUPPORT */ pos->resuming = false; } MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); if ( (used_thr_p_c) && (ret) ) { /* Wake up suspended connections. */ if (! MHD_itc_activate_(daemon->itc, "w")) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, MHD_SC_ITC_USE_FAILED, _("Failed to signal resume of connection via inter-thread communication channel.")); #endif } } return ret; }