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