daemon_event_update.c (6398B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2025 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/daemon_event_update.c 41 * @brief The implementation of MHD_daemon_event_update() function for external 42 * events updates 43 * @author Karlson2k (Evgeny Grin) 44 */ 45 46 #include "mhd_sys_options.h" 47 48 #include "sys_bool_type.h" 49 50 #include "mhd_assert.h" 51 52 #include "mhd_daemon.h" 53 #include "mhd_connection.h" 54 55 #include "daemon_logger.h" 56 57 #ifdef mhd_DEBUG_POLLING_FDS 58 # include "mhd_itc.h" 59 # include <stdio.h> 60 #endif /* mhd_DEBUG_POLLING_FDS */ 61 62 #include "mhd_public_api.h" 63 64 MHD_EXTERN_ 65 MHD_FN_PAR_NONNULL_ (1) MHD_FN_PAR_NONNULL_ (2) void 66 MHD_daemon_event_update ( 67 struct MHD_Daemon *MHD_RESTRICT daemon, 68 struct MHD_EventUpdateContext *MHD_RESTRICT ecb_cntx, 69 enum MHD_FdState fd_current_state) 70 { 71 bool broken_app_data; 72 bool unneeded_event; 73 74 if (mhd_DAEMON_STATE_STARTED > daemon->state) 75 return; 76 if (! mhd_WM_INT_HAS_EXT_EVENTS (daemon->wmode_int)) 77 return; /* FIXME: log error? */ 78 if (mhd_DAEMON_STATE_STARTED < daemon->state) 79 return; 80 81 #ifdef mhd_DEBUG_POLLING_FDS 82 if (1) 83 { 84 char state_str[] = "x:x:x"; 85 state_str[0] = MHD_FD_STATE_IS_SET_RECV (fd_current_state) ? 'R' : '-'; 86 state_str[2] = MHD_FD_STATE_IS_SET_SEND (fd_current_state) ? 'W' : '-'; 87 state_str[4] = MHD_FD_STATE_IS_SET_EXCEPT (fd_current_state) ? 'E' : '-'; 88 89 switch ((mhd_SockRelMarker) ecb_cntx) 90 { 91 case mhd_SOCKET_REL_MARKER_EMPTY: 92 fprintf (stderr, 93 "### MHD_daemon_event_update(daemon, [unknown], %s)\n", 94 state_str); 95 break; 96 case mhd_SOCKET_REL_MARKER_ITC: 97 fprintf (stderr, 98 "### MHD_daemon_event_update(daemon, [ITC: %2llu], %s)\n", 99 (unsigned long long) mhd_itc_r_fd (daemon->threading.itc), 100 state_str); 101 break; 102 case mhd_SOCKET_REL_MARKER_LISTEN: 103 fprintf (stderr, 104 "### MHD_daemon_event_update(daemon, [lstn: %2llu], %s)\n", 105 (unsigned long long) daemon->net.listen.fd, 106 state_str); 107 break; 108 default: 109 fprintf (stderr, 110 "### MHD_daemon_event_update(daemon, [conn: %2llu], %s)\n", 111 (unsigned long long) 112 (((struct MHD_Connection *) ecb_cntx)->sk.fd), 113 state_str); 114 break; 115 } 116 } 117 #endif /* mhd_DEBUG_POLLING_FDS */ 118 119 broken_app_data = false; 120 unneeded_event = false; 121 122 switch ((mhd_SockRelMarker) ecb_cntx) 123 { 124 case mhd_SOCKET_REL_MARKER_EMPTY: 125 broken_app_data = true; 126 break; 127 case mhd_SOCKET_REL_MARKER_ITC: 128 #ifdef MHD_SUPPORT_THREADS 129 if (MHD_FD_STATE_IS_SET_EXCEPT (fd_current_state)) 130 daemon->events.data.extr.itc_data.is_broken = true; 131 else 132 { 133 daemon->events.data.extr.itc_data.is_active = 134 MHD_FD_STATE_IS_SET_RECV (fd_current_state); 135 unneeded_event = MHD_FD_STATE_IS_SET_SEND (fd_current_state); 136 } 137 #else /* ! MHD_SUPPORT_THREADS */ 138 broken_app_data = true; 139 #endif /* ! MHD_SUPPORT_THREADS */ 140 break; 141 case mhd_SOCKET_REL_MARKER_LISTEN: 142 if (MHD_INVALID_SOCKET == daemon->net.listen.fd) 143 broken_app_data = true; 144 else if (MHD_FD_STATE_IS_SET_EXCEPT (fd_current_state)) 145 daemon->net.listen.is_broken = true; 146 else 147 { 148 daemon->events.accept_pending = 149 MHD_FD_STATE_IS_SET_RECV (fd_current_state); 150 unneeded_event = MHD_FD_STATE_IS_SET_SEND (fd_current_state); 151 } 152 break; 153 default: 154 if (((struct MHD_Connection *) ecb_cntx)->daemon != daemon) 155 broken_app_data = true; 156 else 157 { 158 struct MHD_Connection *const c = ((struct MHD_Connection *) ecb_cntx); 159 unsigned int err_flag; 160 161 mhd_assert (MHD_FD_STATE_NONE != c->extr_event.reg_for); 162 163 unneeded_event = (0 != ((~((unsigned int) c->extr_event.reg_for)) 164 & ((unsigned int) fd_current_state))); 165 166 /* Preserve connection's "error flag" */ 167 err_flag = (((unsigned int) c->sk.ready) 168 & (unsigned int) mhd_SOCKET_NET_STATE_ERROR_READY); 169 170 c->sk.ready = 171 (enum mhd_SocketNetState) 172 (err_flag | (((unsigned int) fd_current_state) 173 & ((unsigned int) c->extr_event.reg_for))); 174 } 175 break; 176 } 177 178 if (broken_app_data) 179 { 180 mhd_LOG_MSG (daemon, \ 181 MHD_SC_EXTR_EVENT_BROKEN_DATA, \ 182 "MHD_daemon_event_update() is called with broken content " \ 183 "data"); 184 } 185 else if (unneeded_event) 186 { 187 mhd_LOG_MSG (daemon, \ 188 MHD_SC_EXTR_EVENT_BROKEN_DATA, \ 189 "MHD_daemon_event_update() is called with status that has " \ 190 "not been requested"); 191 } 192 }