conn_data_recv.c (6196B)
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) 2024-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/conn_data_recv.c 41 * @brief The implementation of data receiving functions for connection 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 45 #include "mhd_sys_options.h" 46 47 #include "conn_data_recv.h" 48 49 #include "mhd_sys_options.h" 50 51 #include "sys_bool_type.h" 52 #include "sys_base_types.h" 53 54 #include "mhd_assert.h" 55 56 #include "mhd_connection.h" 57 58 #include "stream_funcs.h" 59 #include "mhd_socket_error_funcs.h" 60 #include "sckt_recv.h" 61 62 #include "mhd_recv.h" 63 64 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ void 65 mhd_conn_data_recv (struct MHD_Connection *c, 66 bool has_err) 67 { 68 void *buf; 69 size_t buf_size; 70 size_t received; 71 enum mhd_SocketError res; 72 73 mhd_assert (mhd_HTTP_STAGE_CLOSED != c->stage); 74 mhd_assert (NULL != c->read_buffer); 75 mhd_assert (c->read_buffer_size > c->read_buffer_offset); 76 mhd_assert (! has_err || \ 77 (0 != (c->sk.ready & mhd_SOCKET_NET_STATE_ERROR_READY))); 78 mhd_assert ((0 == (c->sk.ready & mhd_SOCKET_NET_STATE_ERROR_READY)) || \ 79 has_err); 80 mhd_assert (mhd_SOCKET_ERR_NO_ERROR == c->sk.state.discnt_err); 81 82 buf = c->read_buffer + c->read_buffer_offset; 83 buf_size = c->read_buffer_size - c->read_buffer_offset; 84 85 res = mhd_recv (c, 86 buf_size, 87 (char *) buf, 88 &received); 89 90 if ((mhd_SOCKET_ERR_NO_ERROR != res) || has_err) 91 { 92 /* Handle errors */ 93 if ((mhd_SOCKET_ERR_NO_ERROR == res) && (0 == received)) 94 { 95 c->sk.state.rmt_shut_wr = true; 96 res = mhd_SOCKET_ERR_REMT_DISCONN; 97 } 98 99 if (has_err && (mhd_SOCKET_ERR_NO_ERROR == c->sk.state.discnt_err)) 100 { 101 /* Try to get the real error from the socket */ 102 if (! mhd_SOCKET_ERR_IS_HARD (res) && c->sk.props.is_nonblck) 103 { 104 /* Re-try the last time with direct socket recv() to detect the error */ 105 uint_fast64_t dummy_buf; 106 res = mhd_sckt_recv (&(c->sk), 107 sizeof(dummy_buf), 108 (char *) &dummy_buf, 109 &received); 110 } 111 if (mhd_SOCKET_ERR_IS_HARD (res)) 112 { 113 c->sk.state.discnt_err = res; 114 mhd_SCKT_NET_ST_SET_FLAG (&(c->sk.ready), 115 mhd_SOCKET_NET_STATE_ERROR_READY); 116 } 117 else 118 c->sk.state.discnt_err = mhd_socket_error_get_from_socket (c->sk.fd); 119 } 120 121 return; 122 } 123 124 if (0 == received) 125 c->sk.state.rmt_shut_wr = true; 126 127 c->read_buffer_offset += received; 128 mhd_stream_update_activity_mark (c); // TODO: centralise activity update 129 return; 130 } 131 132 133 #if 0 // TODO: report disconnect 134 if ((bytes_read < 0) || socket_error) 135 { 136 if (MHD_ERR_CONNRESET_ == bytes_read) 137 { 138 if ( (mhd_HTTP_STAGE_INIT < c->stage) && 139 (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) ) 140 { 141 #ifdef HAVE_MESSAGES 142 MHD_DLOG (c->daemon, 143 _ ("Socket has been disconnected when reading request.\n")); 144 #endif 145 c->discard_request = true; 146 } 147 MHD_connection_close_ (c, 148 MHD_REQUEST_TERMINATED_READ_ERROR); 149 return; 150 } 151 152 #ifdef HAVE_MESSAGES 153 if (mhd_HTTP_STAGE_INIT != c->stage) 154 MHD_DLOG (c->daemon, 155 _ ("Connection socket is closed when reading " \ 156 "request due to the error: %s\n"), 157 (bytes_read < 0) ? str_conn_error_ (bytes_read) : 158 "detected c closure"); 159 #endif 160 CONNECTION_CLOSE_ERROR (c, 161 NULL); 162 return; 163 } 164 165 #if 0 // TODO: handle remote shut WR 166 if (0 == bytes_read) 167 { /* Remote side closed c. */ // FIXME: Actually NOT! 168 c->sk.state.rmt_shut_wr = true; 169 if ( (mhd_HTTP_STAGE_INIT < c->stage) && 170 (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) ) 171 { 172 #ifdef HAVE_MESSAGES 173 MHD_DLOG (c->daemon, 174 _ ("Connection was closed by remote side with incomplete " 175 "request.\n")); 176 #endif 177 c->discard_request = true; 178 MHD_connection_close_ (c, 179 MHD_REQUEST_TERMINATED_CLIENT_ABORT); 180 } 181 else if (mhd_HTTP_STAGE_INIT == c->stage) 182 /* This termination code cannot be reported to the application 183 * because application has not been informed yet about this request */ 184 MHD_connection_close_ (c, 185 MHD_REQUEST_TERMINATED_COMPLETED_OK); 186 else 187 MHD_connection_close_ (c, 188 MHD_REQUEST_TERMINATED_WITH_ERROR); 189 return; 190 } 191 #endif 192 #endif