libmicrohttpd

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

commit fc66711ad5b92067c758c807ac6210f808050767
parent 89b4a7fb2d9276c21d84f01bcae720f3f574ffa4
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue, 25 Jan 2022 19:25:38 +0300

Moved mhd_panic() and helpers to separate .c/.h files

Diffstat:
Msrc/include/microhttpd.h | 2+-
Msrc/microhttpd/Makefile.am | 1+
Msrc/microhttpd/daemon.c | 69+--------------------------------------------------------------------
Msrc/microhttpd/internal.h | 45++-------------------------------------------
Asrc/microhttpd/mhd_panic.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/microhttpd/mhd_panic.h | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mw32/common/libmicrohttpd-files.vcxproj | 2++
Mw32/common/libmicrohttpd-filters.vcxproj | 6++++++
8 files changed, 196 insertions(+), 112 deletions(-)

diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -3150,7 +3150,7 @@ MHD_set_connection_value_n (struct MHD_Connection *connection, * simply prints an error message and calls `abort()`. Alternative * implementations might call `exit()` or other similar functions. * - * @param cb new error handler + * @param cb new error handler or NULL to use default handler * @param cls passed to @a cb * @ingroup logging */ diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am @@ -88,6 +88,7 @@ libmicrohttpd_la_SOURCES = \ mhd_sockets.c mhd_sockets.h \ mhd_itc.c mhd_itc.h mhd_itc_types.h \ mhd_compat.c mhd_compat.h \ + mhd_panic.c mhd_panic.h \ response.c response.h if USE_POSIX_THREADS diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -117,47 +117,6 @@ MHD_epoll (struct MHD_Daemon *daemon, #endif /* EPOLL_SUPPORT */ /** - * Default implementation of the panic function, - * prints an error message and aborts. - * - * @param cls unused - * @param file name of the file with the problem - * @param line line number with the problem - * @param reason error message with details - */ -_MHD_NORETURN static void -mhd_panic_std (void *cls, - const char *file, - unsigned int line, - const char *reason) -{ - (void) cls; /* Mute compiler warning. */ -#ifdef HAVE_MESSAGES - fprintf (stderr, - _ ("Fatal error in GNU libmicrohttpd %s:%u: %s\n"), - file, - line, - reason); -#else /* ! HAVE_MESSAGES */ - (void) file; /* Mute compiler warning. */ - (void) line; /* Mute compiler warning. */ - (void) reason; /* Mute compiler warning. */ -#endif - abort (); -} - - -/** - * Handler for fatal errors. - */ -MHD_PanicCallback mhd_panic = (MHD_PanicCallback) NULL; - -/** - * Closure argument for #mhd_panic. - */ -void *mhd_panic_cls = NULL; - -/** * Globally initialise library. */ void @@ -7889,31 +7848,6 @@ MHD_get_daemon_info (struct MHD_Daemon *daemon, /** - * Sets the global error handler to a different implementation. @a cb - * will only be called in the case of typically fatal, serious - * internal consistency issues. These issues should only arise in the - * case of serious memory corruption or similar problems with the - * architecture. While @a cb is allowed to return and MHD will then - * try to continue, this is never safe. - * - * The default implementation that is used if no panic function is set - * simply prints an error message and calls `abort()`. Alternative - * implementations might call `exit()` or other similar functions. - * - * @param cb new error handler - * @param cls passed to @a cb - * @ingroup logging - */ -void -MHD_set_panic_func (MHD_PanicCallback cb, - void *cls) -{ - mhd_panic = cb; - mhd_panic_cls = cls; -} - - -/** * Obtain the version of this library * * @return static version string, e.g. "0.9.9" @@ -8175,8 +8109,7 @@ MHD_init (void) WSADATA wsd; #endif /* MHD_WINSOCK_SOCKETS */ - if (NULL == mhd_panic) - mhd_panic = &mhd_panic_std; + MHD_set_panic_func (NULL, NULL); #if defined(MHD_WINSOCK_SOCKETS) if (0 != WSAStartup (MAKEWORD (2, 2), &wsd)) diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -53,28 +53,8 @@ #define PRIu64 "llu" #endif /* ! PRIu64 */ -#ifdef MHD_PANIC -/* Override any defined MHD_PANIC macro with proper one */ -#undef MHD_PANIC -#endif /* MHD_PANIC */ - -#ifdef HAVE_MESSAGES -/** - * Trigger 'panic' action based on fatal errors. - * - * @param msg error message (const char *) - */ -#define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); \ - BUILTIN_NOT_REACHED; } while (0) -#else -/** - * Trigger 'panic' action based on fatal errors. - * - * @param msg error message (const char *) - */ -#define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); \ - BUILTIN_NOT_REACHED; } while (0) -#endif +/* Must be included before other internal headers! */ +#include "mhd_panic.h" #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) #include "mhd_threads.h" @@ -147,27 +127,6 @@ */ #define MHD_BUF_INC_SIZE 1024 - -/** - * Handler for fatal errors. - */ -extern MHD_PanicCallback mhd_panic; - -/** - * Closure argument for "mhd_panic". - */ -extern void *mhd_panic_cls; - -/* If we have Clang or gcc >= 4.5, use __builtin_unreachable() */ -#if defined(__clang__) || (__GNUC__ > 4) || \ - (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) -#define BUILTIN_NOT_REACHED __builtin_unreachable () -#elif defined(_MSC_FULL_VER) -#define BUILTIN_NOT_REACHED __assume (0) -#else -#define BUILTIN_NOT_REACHED -#endif - #ifndef MHD_STATICSTR_LEN_ /** * Determine length of static string / macro strings at compile time. diff --git a/src/microhttpd/mhd_panic.c b/src/microhttpd/mhd_panic.c @@ -0,0 +1,101 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) + + This library 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. + + This library 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. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * @file microhttpd/mhd_panic.h + * @brief MHD_panic() function and helpers + * @author Daniel Pittman + * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) + */ + +#include "mhd_panic.h" +#include "platform.h" +#include "microhttpd.h" + +/** + * Handler for fatal errors. + */ +MHD_PanicCallback mhd_panic = (MHD_PanicCallback) NULL; + +/** + * Closure argument for #mhd_panic. + */ +void *mhd_panic_cls = NULL; + + +/** + * Default implementation of the panic function, + * prints an error message and aborts. + * + * @param cls unused + * @param file name of the file with the problem + * @param line line number with the problem + * @param reason error message with details + */ +_MHD_NORETURN static void +mhd_panic_std (void *cls, + const char *file, + unsigned int line, + const char *reason) +{ + (void) cls; /* Mute compiler warning. */ +#ifdef HAVE_MESSAGES + fprintf (stderr, + _ ("Fatal error in GNU libmicrohttpd %s:%u: %s\n"), + file, + line, + reason); +#else /* ! HAVE_MESSAGES */ + (void) file; /* Mute compiler warning. */ + (void) line; /* Mute compiler warning. */ + (void) reason; /* Mute compiler warning. */ +#endif + abort (); +} + + +/** + * Sets the global error handler to a different implementation. @a cb + * will only be called in the case of typically fatal, serious + * internal consistency issues. These issues should only arise in the + * case of serious memory corruption or similar problems with the + * architecture. While @a cb is allowed to return and MHD will then + * try to continue, this is never safe. + * + * The default implementation that is used if no panic function is set + * simply prints an error message and calls `abort()`. Alternative + * implementations might call `exit()` or other similar functions. + * + * @param cb new error handler or NULL to use default handler + * @param cls passed to @a cb + * @ingroup logging + */ +void +MHD_set_panic_func (MHD_PanicCallback cb, + void *cls) +{ + if ((MHD_PanicCallback) NULL != cb) + mhd_panic = cb; + else + mhd_panic = &mhd_panic_std; + + mhd_panic_cls = cls; +} diff --git a/src/microhttpd/mhd_panic.h b/src/microhttpd/mhd_panic.h @@ -0,0 +1,82 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) + + This library 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. + + This library 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. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * @file microhttpd/mhd_panic.h + * @brief Declaration and macros for MHD_panic() + * @author Daniel Pittman + * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_PANIC_H +#define MHD_PANIC_H 1 + +#include "mhd_options.h" + +#ifdef MHD_PANIC +/* Override any possible defined MHD_PANIC macro with proper one */ +#undef MHD_PANIC +#endif /* MHD_PANIC */ + +/* If we have Clang or gcc >= 4.5, use __builtin_unreachable() */ +#if defined(__clang__) || (__GNUC__ > 4) || \ + (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +#define BUILTIN_NOT_REACHED __builtin_unreachable () +#elif defined(_MSC_FULL_VER) +#define BUILTIN_NOT_REACHED __assume (0) +#else +#define BUILTIN_NOT_REACHED +#endif + +/* The MHD_PanicCallback type, but without main header. */ +/** + * Handler for fatal errors. + */ +extern void +(*mhd_panic) (void *cls, + const char *file, + unsigned int line, + const char *reason); + +/** + * Closure argument for "mhd_panic". + */ +extern void *mhd_panic_cls; + +#ifdef HAVE_MESSAGES +/** + * Trigger 'panic' action based on fatal errors. + * + * @param msg error message (const char *) + */ +#define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); \ + BUILTIN_NOT_REACHED; } while (0) +#else +/** + * Trigger 'panic' action based on fatal errors. + * + * @param msg error message (const char *) + */ +#define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); \ + BUILTIN_NOT_REACHED; } while (0) +#endif + +#endif /* MHD_PANIC_H */ diff --git a/w32/common/libmicrohttpd-files.vcxproj b/w32/common/libmicrohttpd-files.vcxproj @@ -24,6 +24,7 @@ <ClCompile Include="$(MhdSrc)microhttpd\mhd_compat.c"> <ExcludedFromBuild Condition="'$(PlatformToolsetVersion)'&gt;='140'">true</ExcludedFromBuild> </ClCompile> + <ClCompile Include="$(MhdSrc)microhttpd\mhd_panic.c" /> </ItemGroup> <ItemGroup> <ClInclude Include="$(MhdSrc)include\autoinit_funcs.h" /> @@ -53,6 +54,7 @@ <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc_types.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_compat.h" /> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_panic.h" /> <ClInclude Include="$(MhdW32Common)MHD_config.h" /> </ItemGroup> <ItemGroup> diff --git a/w32/common/libmicrohttpd-filters.vcxproj b/w32/common/libmicrohttpd-filters.vcxproj @@ -109,6 +109,9 @@ <ClInclude Include="$(MhdSrc)microhttpd\mhd_compat.h"> <Filter>Internal Headers</Filter> </ClInclude> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_panic.h"> + <Filter>Internal Headers</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="$(MhdSrc)microhttpd\base64.c"> @@ -174,6 +177,9 @@ <ClCompile Include="$(MhdSrc)microhttpd\mhd_compat.c"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="$(MhdSrc)microhttpd\mhd_panic.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc">