libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 16d9f9e426a8ea1f5c05a21554824b7981c0df3b
parent a123fcc9ee0bf7a3d02f1230395b92b55bc56202
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Thu, 15 Oct 2020 12:55:24 +0300

mhd_send: fixed broken sendfile() on FreeBSD, v0.9.67 regression

Diffstat:
Msrc/microhttpd/connection.c | 41-----------------------------------------
Msrc/microhttpd/connection.h | 10----------
Msrc/microhttpd/daemon.c | 3++-
Msrc/microhttpd/mhd_send.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/microhttpd/mhd_send.h | 21++++++++-------------
5 files changed, 76 insertions(+), 90 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -125,47 +125,6 @@ */ #define MHD_SENFILE_CHUNK_THR_P_C_ (0x200000) -#ifdef HAVE_FREEBSD_SENDFILE -#ifdef SF_FLAGS -/** - * FreeBSD sendfile() flags - */ -static int freebsd_sendfile_flags_; - -/** - * FreeBSD sendfile() flags for thread-per-connection - */ -static int freebsd_sendfile_flags_thd_p_c_; -#endif /* SF_FLAGS */ -/** - * Initialises static variables - */ -void -MHD_conn_init_static_ (void) -{ -/* FreeBSD 11 and later allow to specify read-ahead size - * and handles SF_NODISKIO differently. - * SF_FLAGS defined only on FreeBSD 11 and later. */ -#ifdef SF_FLAGS - long sys_page_size = sysconf (_SC_PAGESIZE); - if (0 > sys_page_size) - { /* Failed to get page size. */ - freebsd_sendfile_flags_ = SF_NODISKIO; - freebsd_sendfile_flags_thd_p_c_ = SF_NODISKIO; - } - else - { - freebsd_sendfile_flags_ = - SF_FLAGS ((uint16_t) (MHD_SENFILE_CHUNK_ / sys_page_size), SF_NODISKIO); - freebsd_sendfile_flags_thd_p_c_ = - SF_FLAGS ((uint16_t) (MHD_SENFILE_CHUNK_THR_P_C_ / sys_page_size), - SF_NODISKIO); - } -#endif /* SF_FLAGS */ -} - - -#endif /* HAVE_FREEBSD_SENDFILE */ /** * Callback for receiving data from the socket. * diff --git a/src/microhttpd/connection.h b/src/microhttpd/connection.h @@ -62,16 +62,6 @@ #define MHD_ERR_INVAL_ (-3078) -#ifdef HAVE_FREEBSD_SENDFILE -/** - * Initialises static variables - */ -void -MHD_conn_init_static_ (void); - -#endif /* HAVE_FREEBSD_SENDFILE */ - - /** * Set callbacks for this connection to those for HTTP. * diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -42,6 +42,7 @@ #include "mhd_sockets.h" #include "mhd_itc.h" #include "mhd_compat.h" +#include "mhd_send.h" #if HAVE_SEARCH_H #include <search.h> @@ -7496,7 +7497,7 @@ MHD_init (void) #endif /* HTTPS_SUPPORT */ MHD_monotonic_sec_counter_init (); #ifdef HAVE_FREEBSD_SENDFILE - MHD_conn_init_static_ (); + MHD_send_init_static_vars_ (); #endif /* HAVE_FREEBSD_SENDFILE */ MHD_init_mem_pools_ (); } diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c @@ -36,8 +36,74 @@ * and every place where sendfile(), sendfile64(), setsockopt() * are used. */ +#ifdef MHD_LINUX_SOLARIS_SENDFILE +#include <sys/sendfile.h> +#endif /* MHD_LINUX_SOLARIS_SENDFILE */ +#if defined(HAVE_FREEBSD_SENDFILE) || defined(HAVE_DARWIN_SENDFILE) +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/uio.h> +#endif /* HAVE_FREEBSD_SENDFILE || HAVE_DARWIN_SENDFILE */ +#ifdef HAVE_SYS_PARAM_H +/* For FreeBSD version identification */ +#include <sys/param.h> +#endif /* HAVE_SYS_PARAM_H */ #include "mhd_send.h" + +/** + * sendfile() chuck size + */ +#define MHD_SENFILE_CHUNK_ (0x20000) + +/** + * sendfile() chuck size for thread-per-connection + */ +#define MHD_SENFILE_CHUNK_THR_P_C_ (0x200000) + +#ifdef HAVE_FREEBSD_SENDFILE +#ifdef SF_FLAGS +/** + * FreeBSD sendfile() flags + */ +static int freebsd_sendfile_flags_; + +/** + * FreeBSD sendfile() flags for thread-per-connection + */ +static int freebsd_sendfile_flags_thd_p_c_; +#endif /* SF_FLAGS */ +/** + * Initialises static variables + */ +void +MHD_send_init_static_vars_ (void) +{ +/* FreeBSD 11 and later allow to specify read-ahead size + * and handles SF_NODISKIO differently. + * SF_FLAGS defined only on FreeBSD 11 and later. */ +#ifdef SF_FLAGS + long sys_page_size = sysconf (_SC_PAGESIZE); + if (0 > sys_page_size) + { /* Failed to get page size. */ + freebsd_sendfile_flags_ = SF_NODISKIO; + freebsd_sendfile_flags_thd_p_c_ = SF_NODISKIO; + } + else + { + freebsd_sendfile_flags_ = + SF_FLAGS ((uint16_t) (MHD_SENFILE_CHUNK_ / sys_page_size), SF_NODISKIO); + freebsd_sendfile_flags_thd_p_c_ = + SF_FLAGS ((uint16_t) (MHD_SENFILE_CHUNK_THR_P_C_ / sys_page_size), + SF_NODISKIO); + } +#endif /* SF_FLAGS */ +} + + +#endif /* HAVE_FREEBSD_SENDFILE */ + + /** * Handle setsockopt calls. * @@ -471,31 +537,6 @@ MHD_send_on_connection2_ (struct MHD_Connection *connection, } -/** - * sendfile() chuck size - */ -#define MHD_SENFILE_CHUNK_ (0x20000) - -/** - * sendfile() chuck size for thread-per-connection - */ -#define MHD_SENFILE_CHUNK_THR_P_C_ (0x200000) - -#ifdef HAVE_FREEBSD_SENDFILE -#ifdef SF_FLAGS -/** - * FreeBSD sendfile() flags - */ -static int freebsd_sendfile_flags_; - -/** - * FreeBSD sendfile() flags for thread-per-connection - */ -static int freebsd_sendfile_flags_thd_p_c_; -#endif /* SF_FLAGS */ - -#endif /* HAVE_FREEBSD_SENDFILE */ - #if defined(_MHD_HAVE_SENDFILE) /** * Function for sending responses backed by file FD. diff --git a/src/microhttpd/mhd_send.h b/src/microhttpd/mhd_send.h @@ -39,19 +39,14 @@ #include "connection_https.h" #endif -#ifdef MHD_LINUX_SOLARIS_SENDFILE -#include <sys/sendfile.h> -#endif /* MHD_LINUX_SOLARIS_SENDFILE */ -#if defined(HAVE_FREEBSD_SENDFILE) || defined(HAVE_DARWIN_SENDFILE) -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/uio.h> -#endif /* HAVE_FREEBSD_SENDFILE || HAVE_DARWIN_SENDFILE */ - -#ifdef HAVE_SYS_PARAM_H -/* For FreeBSD version identification */ -#include <sys/param.h> -#endif /* HAVE_SYS_PARAM_H */ +#ifdef HAVE_FREEBSD_SENDFILE +/** + * Initialises static variables + */ +void +MHD_send_init_static_vars_ (void); + +#endif /* HAVE_FREEBSD_SENDFILE */ /** * The enumeration of send socket options.