libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit 6fc97075722341824d2bbec2bc6bf93697b22333
parent 28c21f154ada928cefa8bb83461f99c299f1f475
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Tue,  9 Dec 2025 13:31:21 +0100

Moved plain sockets recv() to dedicated file

Diffstat:
Msrc/mhd2/Makefile.am | 1+
Msrc/mhd2/mhd_recv.c | 53++++++-----------------------------------------------
Asrc/mhd2/sckt_recv.c | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mhd2/sckt_recv.h | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 189 insertions(+), 47 deletions(-)

diff --git a/src/mhd2/Makefile.am b/src/mhd2/Makefile.am @@ -64,6 +64,7 @@ libmicrohttpd2_la_SOURCES = \ mhd_mono_clock.c mhd_mono_clock.h \ mempool_funcs.c mempool_funcs.h mempool_types.h \ mhd_read_file.c mhd_read_file.h \ + sckt_recv.c sckt_recv.h \ mhd_recv.c mhd_recv.h \ mhd_send.c mhd_send.h \ mhd_daemon.h \ diff --git a/src/mhd2/mhd_recv.c b/src/mhd2/mhd_recv.c @@ -44,60 +44,17 @@ #include "mhd_sys_options.h" -#include "mhd_recv.h" - #include "mhd_connection.h" -#include "mhd_socket_type.h" -#include "sys_sockets_headers.h" -#include "mhd_sockets_macros.h" - -#include "mhd_limits.h" - #include "mhd_assert.h" -#include "mhd_socket_error_funcs.h" #ifdef MHD_SUPPORT_HTTPS # include "mhd_tls_funcs.h" #endif -static MHD_FN_PAR_NONNULL_ALL_ -MHD_FN_PAR_OUT_SIZE_ (3,2) MHD_FN_PAR_OUT_ (4) enum mhd_SocketError -mhd_recv_plain (struct MHD_Connection *restrict c, - size_t buf_size, - char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)], - size_t *restrict received) -{ - /* Plain TCP connection */ - ssize_t res; - enum mhd_SocketError err; - - if (MHD_SCKT_SEND_MAX_SIZE_ < buf_size) - buf_size = MHD_SCKT_SEND_MAX_SIZE_; +#include "sckt_recv.h" - res = mhd_sys_recv (c->sk.fd, buf, buf_size); - if (0 <= res) - { - *received = (size_t) res; - if ((buf_size > (size_t) res) - || ! c->sk.props.is_nonblck) - c->sk.ready = (enum mhd_SocketNetState) /* Clear 'recv-ready' */ - (((unsigned int) c->sk.ready) - & (~(enum mhd_SocketNetState) - mhd_SOCKET_NET_STATE_RECV_READY)); - return mhd_SOCKET_ERR_NO_ERROR; /* Success exit point */ - } - - err = mhd_socket_error_get_from_sys_err (mhd_SCKT_GET_LERR ()); - - if (mhd_SOCKET_ERR_AGAIN == err) - c->sk.ready = (enum mhd_SocketNetState) /* Clear 'recv-ready' */ - (((unsigned int) c->sk.ready) - & (~(enum mhd_SocketNetState) - mhd_SOCKET_NET_STATE_RECV_READY)); - - return err; /* Failure exit point */ -} +#include "mhd_recv.h" #ifdef MHD_SUPPORT_HTTPS @@ -157,7 +114,6 @@ mhd_recv (struct MHD_Connection *restrict c, char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)], size_t *restrict received) { - mhd_assert (MHD_INVALID_SOCKET != c->sk.fd); mhd_assert (mhd_HTTP_STAGE_CLOSED != c->stage); #ifdef MHD_SUPPORT_HTTPS @@ -168,5 +124,8 @@ mhd_recv (struct MHD_Connection *restrict c, received); #endif /* MHD_SUPPORT_HTTPS */ - return mhd_recv_plain (c, buf_size, buf, received); + return mhd_sckt_recv (&(c->sk), + buf_size, + buf, + received); } diff --git a/src/mhd2/sckt_recv.c b/src/mhd2/sckt_recv.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2024-2025 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/mhd2/sckt_recv.c + * @brief The definition of the mhd_sckt_recv() function + * @author Karlson2k (Evgeny Grin) + */ + +#include "mhd_sys_options.h" + +#include "sys_base_types.h" + +#include "mhd_socket_type.h" +#include "sys_sockets_headers.h" +#include "mhd_sockets_macros.h" + +#include "mhd_assert.h" + +#include "mhd_limits.h" + +#include "mhd_socket_error.h" + +#include "mhd_conn_socket.h" + +#include "mhd_socket_error_funcs.h" + +#include "sckt_recv.h" + + +MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ +MHD_FN_PAR_OUT_SIZE_ (3,2) MHD_FN_PAR_OUT_ (4) enum mhd_SocketError +mhd_sckt_recv (struct mhd_ConnSocket *restrict sk, + size_t buf_size, + char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)], + size_t *restrict received) +{ + ssize_t res; + enum mhd_SocketError err; + + mhd_assert (MHD_INVALID_SOCKET != sk->fd); + + if (MHD_SCKT_SEND_MAX_SIZE_ < buf_size) + buf_size = MHD_SCKT_SEND_MAX_SIZE_; + + res = mhd_sys_recv (sk->fd, buf, buf_size); + if (0 <= res) + { + /* When the socket is blocking, always clean "recv-ready" flag + after successful read as complete data could be already retrieved + and no more data is pending. + Note: blocking sockets are never used with edge-triggering. */ + if ((buf_size > (size_t) res) + || ! sk->props.is_nonblck) + mhd_SCKT_NET_ST_CLEAR_FLAG (&(sk->ready), + mhd_SOCKET_NET_STATE_RECV_READY); + + *received = (size_t) res; + + return mhd_SOCKET_ERR_NO_ERROR; /* Success exit point */ + } + + err = mhd_socket_error_get_from_sys_err (mhd_SCKT_GET_LERR ()); + + if (mhd_SOCKET_ERR_AGAIN == err) + mhd_SCKT_NET_ST_CLEAR_FLAG (&(sk->ready), + mhd_SOCKET_NET_STATE_RECV_READY); + + return err; /* Failure exit point */ +} diff --git a/src/mhd2/sckt_recv.h b/src/mhd2/sckt_recv.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2024-2025 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/mhd2/sckt_recv.h + * @brief The declaration of the mhd_sckt_recv() function + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_SCKT_RECV_H +#define MHD_SCKT_RECV_H 1 + +#include "mhd_sys_options.h" + +#include "sys_sizet_type.h" + +#include "mhd_socket_error.h" + +struct mhd_ConnSocket; /* Forward declaration */ + +/** + * Receive the data from the network socket. + * + * Clear #mhd_SOCKET_NET_STATE_RECV_READY in @a sk->ready if necessary. + * + * @param sk the socket data + * @param buf_size the size of the @a buf buffer + * @param[out] buf the buffer to fill with the received data + * @param[out] received the pointer to variable to set the size of the data + * actually put to the @a buffer + * @return mhd_SOCKET_ERR_NO_ERROR if receive succeed (the @a received gets + * the received size) or socket error + */ +MHD_INTERNAL enum mhd_SocketError +mhd_sckt_recv (struct mhd_ConnSocket *restrict sk, + size_t buf_size, + char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)], + size_t *restrict received) +MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_SIZE_ (3,2) MHD_FN_PAR_OUT_ (4); + + +#endif /* ! MHD_SCKT_RECV_H */