|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// daemon_lifecycle.cpp -- the MHD daemon handle + start/stop threading |
| 22 | +// state, and the option-array / start-flag builders that construct the |
| 23 | +// daemon. Extracted from webserver_impl so the coordinator no longer owns |
| 24 | +// the daemon handle, the blocking-start mutex/cond pair, or the daemon- |
| 25 | +// construction machinery. The public webserver::start/stop/is_running/ |
| 26 | +// run/... methods (webserver_lifecycle.cpp) drive this state; those remain |
| 27 | +// on webserver as the orchestration layer and reach the handle + builders |
| 28 | +// through impl_->daemon_. |
| 29 | + |
| 30 | +#include "httpserver/detail/daemon_lifecycle.hpp" |
| 31 | + |
| 32 | +#include <microhttpd.h> |
| 33 | +#include <pthread.h> |
| 34 | + |
| 35 | +#include <cstdint> |
| 36 | +#include <vector> |
| 37 | + |
| 38 | +#include "httpserver/webserver.hpp" |
| 39 | +#include "httpserver/create_webserver.hpp" |
| 40 | +#include "httpserver/http_utils.hpp" |
| 41 | +#include "httpserver/detail/webserver_impl.hpp" |
| 42 | + |
| 43 | +#ifdef HAVE_GNUTLS |
| 44 | +#include <gnutls/gnutls.h> |
| 45 | +#endif // HAVE_GNUTLS |
| 46 | + |
| 47 | +namespace httpserver { |
| 48 | + |
| 49 | +using httpserver::http::http_utils; |
| 50 | + |
| 51 | +namespace detail { |
| 52 | + |
| 53 | +daemon_lifecycle::daemon_lifecycle(webserver_impl* owner, |
| 54 | + MHD_socket bind_socket_val) |
| 55 | + : bind_socket(bind_socket_val), owner_(owner) { |
| 56 | + pthread_mutex_init(&mutexwait, nullptr); |
| 57 | + pthread_cond_init(&mutexcond, nullptr); |
| 58 | +} |
| 59 | + |
| 60 | +daemon_lifecycle::~daemon_lifecycle() { |
| 61 | + pthread_mutex_destroy(&mutexwait); |
| 62 | + pthread_cond_destroy(&mutexcond); |
| 63 | +} |
| 64 | + |
| 65 | +// Wrap MHD_OptionItem aggregate-init so each push reads uniformly |
| 66 | +// across the option-array builders below. |
| 67 | +static MHD_OptionItem make_option(enum MHD_OPTION opt, intptr_t val, |
| 68 | + void* ptr = nullptr) { |
| 69 | + MHD_OptionItem x = {opt, val, ptr}; |
| 70 | + return x; |
| 71 | +} |
| 72 | + |
| 73 | +void daemon_lifecycle::add_base_mhd_options(std::vector<MHD_OptionItem>& iov) const { |
| 74 | + webserver* const parent = owner_->parent; |
| 75 | + iov.push_back(make_option(MHD_OPTION_NOTIFY_COMPLETED, |
| 76 | + (intptr_t) &webserver_impl::request_completed, nullptr)); |
| 77 | + // Per-connection arena anchor. MHD_OPTION_NOTIFY_CONNECTION |
| 78 | + // hands us a per-connection void** (socket_context) on STARTED, where |
| 79 | + // we new a detail::connection_state (which owns the arena), and on |
| 80 | + // CLOSED, where we delete it. This makes the arena's lifetime equal |
| 81 | + // to the MHD_Connection's lifetime; request_completed reuses the |
| 82 | + // arena across keep-alive request boundaries via arena_.release(). |
| 83 | + // The closure pointer is the owning webserver* so the callback can |
| 84 | + // reach impl_->hooks_ (has_hooks_for) / fire_connection_opened / |
| 85 | + // fire_connection_closed. |
| 86 | + iov.push_back(make_option(MHD_OPTION_NOTIFY_CONNECTION, |
| 87 | + (intptr_t) &webserver_impl::connection_notify, parent)); |
| 88 | + iov.push_back(make_option(MHD_OPTION_URI_LOG_CALLBACK, |
| 89 | + (intptr_t) &webserver_impl::uri_log, parent)); |
| 90 | + iov.push_back(make_option(MHD_OPTION_EXTERNAL_LOGGER, |
| 91 | + (intptr_t) &webserver_impl::error_log, parent)); |
| 92 | + iov.push_back(make_option(MHD_OPTION_UNESCAPE_CALLBACK, |
| 93 | + (intptr_t) &webserver_impl::unescaper_func, parent)); |
| 94 | + iov.push_back(make_option(MHD_OPTION_CONNECTION_TIMEOUT, parent->config.connection_timeout)); |
| 95 | + if (bind_socket != MHD_INVALID_SOCKET) { |
| 96 | + iov.push_back(make_option(MHD_OPTION_LISTEN_SOCKET, bind_socket)); |
| 97 | + } |
| 98 | + if (parent->config.max_threads != 0) { |
| 99 | + iov.push_back(make_option(MHD_OPTION_THREAD_POOL_SIZE, parent->config.max_threads)); |
| 100 | + } |
| 101 | + if (parent->config.max_connections != 0) { |
| 102 | + iov.push_back(make_option(MHD_OPTION_CONNECTION_LIMIT, parent->config.max_connections)); |
| 103 | + } |
| 104 | + if (parent->config.memory_limit != 0) { |
| 105 | + iov.push_back(make_option(MHD_OPTION_CONNECTION_MEMORY_LIMIT, parent->config.memory_limit)); |
| 106 | + } |
| 107 | + if (parent->config.per_IP_connection_limit != 0) { |
| 108 | + iov.push_back(make_option(MHD_OPTION_PER_IP_CONNECTION_LIMIT, parent->config.per_IP_connection_limit)); |
| 109 | + } |
| 110 | + if (parent->config.max_thread_stack_size != 0) { |
| 111 | + iov.push_back(make_option(MHD_OPTION_THREAD_STACK_SIZE, parent->config.max_thread_stack_size)); |
| 112 | + } |
| 113 | +#ifdef HAVE_DAUTH |
| 114 | + if (parent->config.nonce_nc_size != 0) { |
| 115 | + iov.push_back(make_option(MHD_OPTION_NONCE_NC_SIZE, parent->config.nonce_nc_size)); |
| 116 | + } |
| 117 | +#endif // HAVE_DAUTH |
| 118 | +} |
| 119 | + |
| 120 | +void daemon_lifecycle::add_tls_mhd_options(std::vector<MHD_OptionItem>& iov) const { |
| 121 | + webserver* const parent = owner_->parent; |
| 122 | + if (parent->config.use_ssl) { |
| 123 | + // const_cast respects the MHD C interface, which takes a void* |
| 124 | + // even though the data is read-only at the library boundary. |
| 125 | + iov.push_back(make_option(MHD_OPTION_HTTPS_MEM_KEY, 0, |
| 126 | + reinterpret_cast<void*>(const_cast<char*>(parent->config.https_mem_key.c_str())))); |
| 127 | + iov.push_back(make_option(MHD_OPTION_HTTPS_MEM_CERT, 0, |
| 128 | + reinterpret_cast<void*>(const_cast<char*>(parent->config.https_mem_cert.c_str())))); |
| 129 | + if (!parent->config.https_mem_trust.empty()) { |
| 130 | + iov.push_back(make_option(MHD_OPTION_HTTPS_MEM_TRUST, 0, |
| 131 | + reinterpret_cast<void*>(const_cast<char*>(parent->config.https_mem_trust.c_str())))); |
| 132 | + } |
| 133 | + if (!parent->config.https_priorities.empty()) { |
| 134 | + iov.push_back(make_option(MHD_OPTION_HTTPS_PRIORITIES, 0, |
| 135 | + reinterpret_cast<void*>(const_cast<char*>(parent->config.https_priorities.c_str())))); |
| 136 | + } |
| 137 | + } |
| 138 | +#ifdef HAVE_DAUTH |
| 139 | + if (parent->config.digest_auth_random != "") { |
| 140 | + iov.push_back(make_option(MHD_OPTION_DIGEST_AUTH_RANDOM, |
| 141 | + parent->config.digest_auth_random.size(), |
| 142 | + const_cast<char*>(parent->config.digest_auth_random.c_str()))); |
| 143 | + } |
| 144 | +#endif // HAVE_DAUTH |
| 145 | +} |
| 146 | + |
| 147 | +void daemon_lifecycle::add_gnutls_mhd_options(std::vector<MHD_OptionItem>& iov) const { |
| 148 | +#ifdef HAVE_GNUTLS |
| 149 | + webserver* const parent = owner_->parent; |
| 150 | + if (parent->config.cred_type != http_utils::NONE) { |
| 151 | + iov.push_back(make_option(MHD_OPTION_HTTPS_CRED_TYPE, parent->config.cred_type)); |
| 152 | + } |
| 153 | + if (parent->config.psk_cred_handler != nullptr && parent->config.use_ssl) { |
| 154 | + iov.push_back(make_option(MHD_OPTION_GNUTLS_PSK_CRED_HANDLER, |
| 155 | + (intptr_t)&webserver_impl::psk_cred_handler_func, parent)); |
| 156 | + } |
| 157 | +#ifdef MHD_OPTION_HTTPS_CERT_CALLBACK |
| 158 | + if (parent->config.sni_callback != nullptr && parent->config.use_ssl) { |
| 159 | + iov.push_back(make_option(MHD_OPTION_HTTPS_CERT_CALLBACK, |
| 160 | + (intptr_t)&webserver_impl::sni_cert_callback_func, parent)); |
| 161 | + } |
| 162 | +#endif // MHD_OPTION_HTTPS_CERT_CALLBACK |
| 163 | +#else // HAVE_GNUTLS |
| 164 | + (void)iov; |
| 165 | +#endif // HAVE_GNUTLS |
| 166 | +} |
| 167 | + |
| 168 | +void daemon_lifecycle::add_extended_mhd_options(std::vector<MHD_OptionItem>& iov) const { |
| 169 | + webserver* const parent = owner_->parent; |
| 170 | + if (parent->config.listen_backlog > 0) { |
| 171 | + iov.push_back(make_option(MHD_OPTION_LISTEN_BACKLOG_SIZE, parent->config.listen_backlog)); |
| 172 | + } |
| 173 | + if (parent->config.address_reuse != 0) { |
| 174 | + iov.push_back(make_option(MHD_OPTION_LISTENING_ADDRESS_REUSE, parent->config.address_reuse)); |
| 175 | + } |
| 176 | + if (parent->config.connection_memory_increment > 0) { |
| 177 | + iov.push_back(make_option(MHD_OPTION_CONNECTION_MEMORY_INCREMENT, |
| 178 | + parent->config.connection_memory_increment)); |
| 179 | + } |
| 180 | + if (parent->config.tcp_fastopen_queue_size > 0) { |
| 181 | + iov.push_back(make_option(MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE, |
| 182 | + parent->config.tcp_fastopen_queue_size)); |
| 183 | + } |
| 184 | + if (parent->config.sigpipe_handled_by_app) { |
| 185 | + iov.push_back(make_option(MHD_OPTION_SIGPIPE_HANDLED_BY_APP, 1)); |
| 186 | + } |
| 187 | + if (parent->config.no_alpn) { |
| 188 | + iov.push_back(make_option(MHD_OPTION_TLS_NO_ALPN, 1)); |
| 189 | + } |
| 190 | + if (parent->config.client_discipline_level >= 0) { |
| 191 | + iov.push_back(make_option(MHD_OPTION_CLIENT_DISCIPLINE_LVL, parent->config.client_discipline_level)); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +void daemon_lifecycle::add_https_extra_options(std::vector<MHD_OptionItem>& iov) const { |
| 196 | + webserver* const parent = owner_->parent; |
| 197 | + if (!parent->config.https_mem_dhparams.empty()) { |
| 198 | + iov.push_back(make_option(MHD_OPTION_HTTPS_MEM_DHPARAMS, 0, |
| 199 | + const_cast<char*>(parent->config.https_mem_dhparams.c_str()))); |
| 200 | + } |
| 201 | + if (!parent->config.https_key_password.empty()) { |
| 202 | + iov.push_back(make_option(MHD_OPTION_HTTPS_KEY_PASSWORD, 0, |
| 203 | + const_cast<char*>(parent->config.https_key_password.c_str()))); |
| 204 | + } |
| 205 | + if (!parent->config.https_priorities_append.empty()) { |
| 206 | + iov.push_back(make_option(MHD_OPTION_HTTPS_PRIORITIES_APPEND, 0, |
| 207 | + const_cast<char*>(parent->config.https_priorities_append.c_str()))); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +void daemon_lifecycle::build_mhd_option_array(std::vector<MHD_OptionItem>& iov) const { |
| 212 | + add_base_mhd_options(iov); |
| 213 | + add_tls_mhd_options(iov); |
| 214 | + add_gnutls_mhd_options(iov); |
| 215 | + add_extended_mhd_options(iov); |
| 216 | + add_https_extra_options(iov); |
| 217 | + iov.push_back(make_option(MHD_OPTION_END, 0, nullptr)); |
| 218 | +} |
| 219 | + |
| 220 | +int daemon_lifecycle::compose_transport_flags() const { |
| 221 | + webserver* const parent = owner_->parent; |
| 222 | + int flags = 0; |
| 223 | + if (parent->config.use_ssl) flags |= MHD_USE_SSL; |
| 224 | + if (parent->config.use_ipv6) flags |= MHD_USE_IPv6; |
| 225 | + if (parent->config.use_dual_stack) flags |= MHD_USE_DUAL_STACK; |
| 226 | + return flags; |
| 227 | +} |
| 228 | + |
| 229 | +int daemon_lifecycle::compose_runtime_flags() const { |
| 230 | + webserver* const parent = owner_->parent; |
| 231 | + int flags = 0; |
| 232 | + if (parent->config.debug) flags |= MHD_USE_DEBUG; |
| 233 | + if (parent->config.pedantic) flags |= MHD_USE_PEDANTIC_CHECKS; |
| 234 | + if (parent->config.deferred_enabled) flags |= MHD_USE_SUSPEND_RESUME; |
| 235 | + if (parent->config.no_listen_socket) flags |= MHD_USE_NO_LISTEN_SOCKET; |
| 236 | + if (parent->config.no_thread_safety) flags |= MHD_USE_NO_THREAD_SAFETY; |
| 237 | + if (parent->config.turbo) flags |= MHD_USE_TURBO; |
| 238 | + if (parent->config.suppress_date_header) flags |= MHD_USE_SUPPRESS_DATE_NO_CLOCK; |
| 239 | +#ifdef HAVE_WEBSOCKET |
| 240 | + if (!owner_->ws_.empty()) flags |= MHD_ALLOW_UPGRADE; |
| 241 | +#endif // HAVE_WEBSOCKET |
| 242 | + return flags; |
| 243 | +} |
| 244 | + |
| 245 | +int daemon_lifecycle::compose_start_flags() const { |
| 246 | + webserver* const parent = owner_->parent; |
| 247 | + int flags = parent->config.start_method; |
| 248 | + flags |= compose_transport_flags(); |
| 249 | + flags |= compose_runtime_flags(); |
| 250 | +#ifdef USE_FASTOPEN |
| 251 | + flags |= MHD_USE_TCP_FASTOPEN; |
| 252 | +#endif |
| 253 | + return flags; |
| 254 | +} |
| 255 | + |
| 256 | +} // namespace detail |
| 257 | +} // namespace httpserver |
0 commit comments