diff --git a/xbmc/network/WebServer.cpp b/xbmc/network/WebServer.cpp index 3a14d51dadf9d..8e1fced651db2 100644 --- a/xbmc/network/WebServer.cpp +++ b/xbmc/network/WebServer.cpp @@ -27,6 +27,7 @@ #ifdef HAS_WEB_SERVER #include #include +#include #include "URL.h" #include "Util.h" @@ -1062,10 +1063,43 @@ void CWebServer::ContentReaderFreeCallback(void *cls) #endif } +// local helper +static void panicHandlerForMHD(void* unused, const char* file, unsigned int line, const char *reason) +{ + CLog::Log(LOGSEVERE, "CWebServer: MHD serious error: reason \"%s\" in file \"%s\" at line %ui", reason ? reason : "", + file ? file : "", line); + throw std::runtime_error("MHD serious error"); // FIXME: better solution? +} + +// local helper +static void logFromMHD(void* unused, const char* fmt, va_list ap) +{ + if (fmt == NULL || fmt[0] == 0) + CLog::Log(LOGERROR, "CWebServer: MHD reported error with empty string"); + else + { + std::string errDsc = StringUtils::FormatV(fmt, ap); + if (errDsc.empty()) + CLog::Log(LOGERROR, "CWebServer: MHD reported error with unprintable string \"%s\"", fmt); + else + { + if (errDsc.at(errDsc.length() - 1) == '\n') + errDsc.erase(errDsc.length() - 1); + + // Most common error is "aborted connection", so log it at LOGDEBUG level + CLog::Log(LOGDEBUG, "CWebServer [MHD]: %s", errDsc.c_str()); + } + } +} + struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port) { unsigned int timeout = 60 * 60 * 24; +#if MHD_VERSION >= 0x00040500 + MHD_set_panic_func(&panicHandlerForMHD, NULL); +#endif + return MHD_start_daemon(flags | #if (MHD_VERSION >= 0x00040002) && (MHD_VERSION < 0x00090B01) // use main thread for each connection, can only handle one request at a @@ -1077,6 +1111,9 @@ struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port) // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop MHD_USE_THREAD_PER_CONNECTION #endif +#if (MHD_VERSION >= 0x00040001) + | MHD_USE_DEBUG /* Print MHD error messages to log */ +#endif , port, NULL, @@ -1090,6 +1127,9 @@ struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port) MHD_OPTION_CONNECTION_LIMIT, 512, MHD_OPTION_CONNECTION_TIMEOUT, timeout, MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this, +#if (MHD_VERSION >= 0x00040001) + MHD_OPTION_EXTERNAL_LOGGER, &logFromMHD, NULL, +#endif // MHD_VERSION >= 0x00040001 MHD_OPTION_END); }