https://github.com/xbmc/xbmc/pull/18157 From 805564c28966c61d1875b49e1e7fa7fd811380a0 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 2 Jul 2020 14:36:09 -0400 Subject: [PATCH 1/2] [webserver] Compatibility with libmicrohttpd 0.9.71 From the libmicrohttpd 0.9.71 release notes: The release introduces an 'enum MHD_Result' instead of for certain API misuse bugs by providing better types (not everything is an 'int'). While this does NOT change the binary API, this change _will_ cause compiler warnings for all legacy code -- until 'int' is replaced with 'enum MHD_Result'. --- xbmc/network/WebServer.cpp | 45 ++++++++++--------- xbmc/network/WebServer.h | 30 ++++++------- .../httprequesthandler/HTTPFileHandler.cpp | 2 +- .../httprequesthandler/HTTPFileHandler.h | 2 +- .../HTTPImageTransformationHandler.cpp | 2 +- .../HTTPImageTransformationHandler.h | 2 +- .../httprequesthandler/HTTPJsonRpcHandler.cpp | 2 +- .../httprequesthandler/HTTPJsonRpcHandler.h | 2 +- .../httprequesthandler/HTTPPythonHandler.cpp | 2 +- .../httprequesthandler/HTTPPythonHandler.h | 2 +- .../HTTPRequestHandlerUtils.cpp | 4 +- .../HTTPRequestHandlerUtils.h | 4 +- .../HTTPWebinterfaceAddonsHandler.cpp | 2 +- .../HTTPWebinterfaceAddonsHandler.h | 2 +- .../httprequesthandler/IHTTPRequestHandler.h | 8 +++- 15 files changed, 60 insertions(+), 51 deletions(-) diff --git a/xbmc/network/WebServer.cpp b/xbmc/network/WebServer.cpp index 783404227785..53549aafa9ce 100644 --- a/xbmc/network/WebServer.cpp +++ b/xbmc/network/WebServer.cpp @@ -86,7 +86,7 @@ static MHD_Response* create_response(size_t size, const void* data, int free, in return MHD_create_response_from_buffer(size, const_cast(data), mode); } -int CWebServer::AskForAuthentication(const HTTPRequest& request) const +MHD_RESULT CWebServer::AskForAuthentication(const HTTPRequest& request) const { struct MHD_Response *response = create_response(0, nullptr, MHD_NO, MHD_NO); if (!response) @@ -95,7 +95,7 @@ int CWebServer::AskForAuthentication(const HTTPRequest& request) const return MHD_NO; } - int ret = AddHeader(response, MHD_HTTP_HEADER_CONNECTION, "close"); + MHD_RESULT ret = AddHeader(response, MHD_HTTP_HEADER_CONNECTION, "close"); if (!ret) { CLog::Log(LOGERROR, "CWebServer[%hu]: unable to prepare HTTP Unauthorized response", m_port); @@ -105,7 +105,10 @@ int CWebServer::AskForAuthentication(const HTTPRequest& request) const LogResponse(request, MHD_HTTP_UNAUTHORIZED); - ret = MHD_queue_basic_auth_fail_response(request.connection, "XBMC", response); + // This MHD_RESULT cast is only necessary for libmicrohttpd 0.9.71 + // The return type of MHD_queue_basic_auth_fail_response was fixed for future versions + // See https://git.gnunet.org/libmicrohttpd.git/commit/?id=860b42e9180da4dcd7e8690a3fcdb4e37e5772c5 + ret = static_cast(MHD_queue_basic_auth_fail_response(request.connection, "XBMC", response)); MHD_destroy_response(response); return ret; @@ -135,7 +138,7 @@ bool CWebServer::IsAuthenticated(const HTTPRequest& request) const return authenticated; } -int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection, +MHD_RESULT CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) @@ -163,7 +166,7 @@ int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection, return webServer->HandlePartialRequest(connection, connectionHandler, request, upload_data, upload_data_size, con_cls); } -int CWebServer::HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, const char *upload_data, size_t *upload_data_size, void **con_cls) +MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, const char *upload_data, size_t *upload_data_size, void **con_cls) { std::unique_ptr conHandler(connectionHandler); @@ -276,7 +279,7 @@ int CWebServer::HandlePartialRequest(struct MHD_Connection *connection, Connecti return SendErrorResponse(request, MHD_HTTP_NOT_FOUND, request.method); } -int CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key, +MHD_RESULT CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) @@ -294,13 +297,13 @@ int CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char * return MHD_YES; } -int CWebServer::HandleRequest(const std::shared_ptr& handler) +MHD_RESULT CWebServer::HandleRequest(const std::shared_ptr& handler) { if (handler == nullptr) return MHD_NO; HTTPRequest request = handler->GetRequest(); - int ret = handler->HandleRequest(); + MHD_RESULT ret = handler->HandleRequest(); if (ret == MHD_NO) { CLog::Log(LOGERROR, "CWebServer[%hu]: failed to handle HTTP request for %s", m_port, request.pathUrl.c_str()); @@ -348,7 +351,7 @@ int CWebServer::HandleRequest(const std::shared_ptr& handle return FinalizeRequest(handler, responseDetails.status, response); } -int CWebServer::FinalizeRequest(const std::shared_ptr& handler, int responseStatus, struct MHD_Response *response) +MHD_RESULT CWebServer::FinalizeRequest(const std::shared_ptr& handler, int responseStatus, struct MHD_Response *response) { if (handler == nullptr || response == nullptr) return MHD_NO; @@ -562,7 +565,7 @@ void CWebServer::FinalizePostDataProcessing(ConnectionHandler *connectionHandler MHD_destroy_post_processor(connectionHandler->postprocessor); } -int CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const { if (handler == nullptr) return MHD_NO; @@ -620,7 +623,7 @@ int CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const { if (handler == nullptr) return MHD_NO; @@ -700,7 +703,7 @@ int CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const { if (handler == nullptr) return MHD_NO; @@ -850,7 +853,7 @@ int CWebServer::CreateFileDownloadResponse(const std::shared_ptr(data), free ? MHD_YES : MHD_NO, copy ? MHD_YES : MHD_NO); if (response == nullptr) @@ -893,20 +896,20 @@ int CWebServer::CreateMemoryDownloadResponse(struct MHD_Connection *connection, return MHD_YES; } -int CWebServer::SendResponse(const HTTPRequest& request, int responseStatus, MHD_Response *response) const +MHD_RESULT CWebServer::SendResponse(const HTTPRequest& request, int responseStatus, MHD_Response *response) const { LogResponse(request, responseStatus); - int ret = MHD_queue_response(request.connection, responseStatus, response); + MHD_RESULT ret = MHD_queue_response(request.connection, responseStatus, response); MHD_destroy_response(response); return ret; } -int CWebServer::SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const +MHD_RESULT CWebServer::SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const { struct MHD_Response *response = nullptr; - int ret = CreateErrorResponse(request.connection, errorType, method, response); + MHD_RESULT ret = CreateErrorResponse(request.connection, errorType, method, response); if (ret == MHD_NO) return MHD_NO; @@ -1296,10 +1299,10 @@ std::string CWebServer::CreateMimeTypeFromExtension(const char *ext) return CMime::GetMimeType(ext); } -int CWebServer::AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value) const +MHD_RESULT CWebServer::AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value) const { if (response == nullptr || name.empty()) - return 0; + return MHD_NO; CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer[%hu] [OUT] %s: %s", m_port, name.c_str(), value.c_str()); diff --git a/xbmc/network/WebServer.h b/xbmc/network/WebServer.h index c7a909304a21..1274a2e0ed40 100644 --- a/xbmc/network/WebServer.h +++ b/xbmc/network/WebServer.h @@ -56,17 +56,17 @@ class CWebServer virtual void LogRequest(const char* uri) const; - virtual int HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, + virtual MHD_RESULT HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, const char *upload_data, size_t *upload_data_size, void **con_cls); - virtual int HandleRequest(const std::shared_ptr& handler); - virtual int FinalizeRequest(const std::shared_ptr& handler, int responseStatus, struct MHD_Response *response); + virtual MHD_RESULT HandleRequest(const std::shared_ptr& handler); + virtual MHD_RESULT FinalizeRequest(const std::shared_ptr& handler, int responseStatus, struct MHD_Response *response); private: struct MHD_Daemon* StartMHD(unsigned int flags, int port); std::shared_ptr FindRequestHandler(const HTTPRequest& request) const; - int AskForAuthentication(const HTTPRequest& request) const; + MHD_RESULT AskForAuthentication(const HTTPRequest& request) const; bool IsAuthenticated(const HTTPRequest& request) const; bool IsRequestCacheable(const HTTPRequest& request) const; @@ -76,18 +76,18 @@ class CWebServer bool ProcessPostData(const HTTPRequest& request, ConnectionHandler *connectionHandler, const char *upload_data, size_t *upload_data_size, void **con_cls) const; void FinalizePostDataProcessing(ConnectionHandler *connectionHandler) const; - int CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; - int CreateRangedMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; + MHD_RESULT CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; + MHD_RESULT CreateRangedMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; - int CreateRedirect(struct MHD_Connection *connection, const std::string &strURL, struct MHD_Response *&response) const; - int CreateFileDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; - int CreateErrorResponse(struct MHD_Connection *connection, int responseType, HTTPMethod method, struct MHD_Response *&response) const; - int CreateMemoryDownloadResponse(struct MHD_Connection *connection, const void *data, size_t size, bool free, bool copy, struct MHD_Response *&response) const; + MHD_RESULT CreateRedirect(struct MHD_Connection *connection, const std::string &strURL, struct MHD_Response *&response) const; + MHD_RESULT CreateFileDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const; + MHD_RESULT CreateErrorResponse(struct MHD_Connection *connection, int responseType, HTTPMethod method, struct MHD_Response *&response) const; + MHD_RESULT CreateMemoryDownloadResponse(struct MHD_Connection *connection, const void *data, size_t size, bool free, bool copy, struct MHD_Response *&response) const; - int SendResponse(const HTTPRequest& request, int responseStatus, MHD_Response *response) const; - int SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const; + MHD_RESULT SendResponse(const HTTPRequest& request, int responseStatus, MHD_Response *response) const; + MHD_RESULT SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const; - int AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value) const; + MHD_RESULT AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value) const; void LogRequest(const HTTPRequest& request) const; void LogResponse(const HTTPRequest& request, int responseStatus) const; @@ -100,11 +100,11 @@ class CWebServer static ssize_t ContentReaderCallback (void *cls, uint64_t pos, char *buf, size_t max); static void ContentReaderFreeCallback(void *cls); - static int AnswerToConnection (void *cls, struct MHD_Connection *connection, + static MHD_RESULT AnswerToConnection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); - static int HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key, + static MHD_RESULT HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size); diff --git a/xbmc/network/httprequesthandler/HTTPFileHandler.cpp b/xbmc/network/httprequesthandler/HTTPFileHandler.cpp index 2101d49f0911..26e53901dbfa 100644 --- a/xbmc/network/httprequesthandler/HTTPFileHandler.cpp +++ b/xbmc/network/httprequesthandler/HTTPFileHandler.cpp @@ -23,7 +23,7 @@ CHTTPFileHandler::CHTTPFileHandler(const HTTPRequest &request) m_lastModified() { } -int CHTTPFileHandler::HandleRequest() +MHD_RESULT CHTTPFileHandler::HandleRequest() { return !m_url.empty() ? MHD_YES : MHD_NO; } diff --git a/xbmc/network/httprequesthandler/HTTPFileHandler.h b/xbmc/network/httprequesthandler/HTTPFileHandler.h index 3c74b5275092..6121315c6f5f 100644 --- a/xbmc/network/httprequesthandler/HTTPFileHandler.h +++ b/xbmc/network/httprequesthandler/HTTPFileHandler.h @@ -19,7 +19,7 @@ class CHTTPFileHandler : public IHTTPRequestHandler public: ~CHTTPFileHandler() override = default; - int HandleRequest() override; + MHD_RESULT HandleRequest() override; bool CanHandleRanges() const override { return m_canHandleRanges; } bool CanBeCached() const override { return m_canBeCached; } diff --git a/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.cpp b/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.cpp index de42e7fd3017..6902be012532 100644 --- a/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.cpp +++ b/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.cpp @@ -104,7 +104,7 @@ bool CHTTPImageTransformationHandler::CanHandleRequest(const HTTPRequest &reques options.find(TRANSFORMATION_OPTION_HEIGHT) != options.end()); } -int CHTTPImageTransformationHandler::HandleRequest() +MHD_RESULT CHTTPImageTransformationHandler::HandleRequest() { if (m_response.type == HTTPError) return MHD_YES; diff --git a/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.h b/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.h index c55015ec4eb0..0d17afc3250a 100644 --- a/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.h +++ b/xbmc/network/httprequesthandler/HTTPImageTransformationHandler.h @@ -23,7 +23,7 @@ class CHTTPImageTransformationHandler : public IHTTPRequestHandler IHTTPRequestHandler* Create(const HTTPRequest &request) const override { return new CHTTPImageTransformationHandler(request); } bool CanHandleRequest(const HTTPRequest &request)const override; - int HandleRequest() override; + MHD_RESULT HandleRequest() override; bool CanHandleRanges() const override { return true; } bool CanBeCached() const override { return true; } diff --git a/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.cpp b/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.cpp index e8e2fa36924b..a4c3c198eba3 100644 --- a/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.cpp +++ b/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.cpp @@ -25,7 +25,7 @@ bool CHTTPJsonRpcHandler::CanHandleRequest(const HTTPRequest &request) const return (request.pathUrl.compare("/jsonrpc") == 0); } -int CHTTPJsonRpcHandler::HandleRequest() +MHD_RESULT CHTTPJsonRpcHandler::HandleRequest() { CHTTPClient client(m_request.method); bool isRequest = false; diff --git a/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.h b/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.h index 67c14b666ef6..2659fd549c25 100644 --- a/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.h +++ b/xbmc/network/httprequesthandler/HTTPJsonRpcHandler.h @@ -24,7 +24,7 @@ class CHTTPJsonRpcHandler : public IHTTPRequestHandler IHTTPRequestHandler* Create(const HTTPRequest &request) const override { return new CHTTPJsonRpcHandler(request); } bool CanHandleRequest(const HTTPRequest &request) const override; - int HandleRequest() override; + MHD_RESULT HandleRequest() override; HttpResponseRanges GetResponseData() const override; diff --git a/xbmc/network/httprequesthandler/HTTPPythonHandler.cpp b/xbmc/network/httprequesthandler/HTTPPythonHandler.cpp index 5f9aeef10f2e..a07ef0d3ac31 100644 --- a/xbmc/network/httprequesthandler/HTTPPythonHandler.cpp +++ b/xbmc/network/httprequesthandler/HTTPPythonHandler.cpp @@ -112,7 +112,7 @@ bool CHTTPPythonHandler::CanHandleRequest(const HTTPRequest &request) const return true; } -int CHTTPPythonHandler::HandleRequest() +MHD_RESULT CHTTPPythonHandler::HandleRequest() { if (m_response.type == HTTPError || m_response.type == HTTPRedirect) return MHD_YES; diff --git a/xbmc/network/httprequesthandler/HTTPPythonHandler.h b/xbmc/network/httprequesthandler/HTTPPythonHandler.h index 03c150693ff4..166430e68d51 100644 --- a/xbmc/network/httprequesthandler/HTTPPythonHandler.h +++ b/xbmc/network/httprequesthandler/HTTPPythonHandler.h @@ -25,7 +25,7 @@ class CHTTPPythonHandler : public IHTTPRequestHandler bool CanBeCached() const override { return false; } bool GetLastModifiedDate(CDateTime &lastModified) const override; - int HandleRequest() override; + MHD_RESULT HandleRequest() override; HttpResponseRanges GetResponseData() const override { return m_responseRanges; } diff --git a/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.cpp b/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.cpp index 80d1d6733475..f2ea1f2e51ed 100644 --- a/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.cpp +++ b/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.cpp @@ -61,7 +61,7 @@ bool HTTPRequestHandlerUtils::GetRequestedRanges(struct MHD_Connection *connecti return ranges.Parse(GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE), totalLength); } -int HTTPRequestHandlerUtils::FillArgumentMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value) +MHD_RESULT HTTPRequestHandlerUtils::FillArgumentMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value) { if (cls == nullptr || key == nullptr) return MHD_NO; @@ -72,7 +72,7 @@ int HTTPRequestHandlerUtils::FillArgumentMap(void *cls, enum MHD_ValueKind kind, return MHD_YES; } -int HTTPRequestHandlerUtils::FillArgumentMultiMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value) +MHD_RESULT HTTPRequestHandlerUtils::FillArgumentMultiMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value) { if (cls == nullptr || key == nullptr) return MHD_NO; diff --git a/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.h b/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.h index 9a07801914e4..0ec5ed1bf706 100644 --- a/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.h +++ b/xbmc/network/httprequesthandler/HTTPRequestHandlerUtils.h @@ -25,6 +25,6 @@ class HTTPRequestHandlerUtils private: HTTPRequestHandlerUtils() = delete; - static int FillArgumentMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value); - static int FillArgumentMultiMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value); + static MHD_RESULT FillArgumentMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value); + static MHD_RESULT FillArgumentMultiMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value); }; diff --git a/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.cpp b/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.cpp index 01a6b503bdf6..0716a5df96ca 100644 --- a/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.cpp +++ b/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.cpp @@ -18,7 +18,7 @@ bool CHTTPWebinterfaceAddonsHandler::CanHandleRequest(const HTTPRequest &request return (request.pathUrl.compare("/addons") == 0 || request.pathUrl.compare("/addons/") == 0); } -int CHTTPWebinterfaceAddonsHandler::HandleRequest() +MHD_RESULT CHTTPWebinterfaceAddonsHandler::HandleRequest() { m_responseData = ADDON_HEADER; ADDON::VECADDONS addons; diff --git a/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.h b/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.h index e9b1c6d29a41..23cea36d1436 100644 --- a/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.h +++ b/xbmc/network/httprequesthandler/HTTPWebinterfaceAddonsHandler.h @@ -21,7 +21,7 @@ class CHTTPWebinterfaceAddonsHandler : public IHTTPRequestHandler IHTTPRequestHandler* Create(const HTTPRequest &request) const override { return new CHTTPWebinterfaceAddonsHandler(request); } bool CanHandleRequest(const HTTPRequest &request) const override; - int HandleRequest() override; + MHD_RESULT HandleRequest() override; HttpResponseRanges GetResponseData() const override; diff --git a/xbmc/network/httprequesthandler/IHTTPRequestHandler.h b/xbmc/network/httprequesthandler/IHTTPRequestHandler.h index 4b1e40a587a7..567c8e55ee9b 100644 --- a/xbmc/network/httprequesthandler/IHTTPRequestHandler.h +++ b/xbmc/network/httprequesthandler/IHTTPRequestHandler.h @@ -22,6 +22,12 @@ #include "utils/HttpRangeUtils.h" +#if MHD_VERSION >= 0x00097002 +using MHD_RESULT = MHD_Result; +#else +using MHD_RESULT = int; +#endif + class CDateTime; class CWebServer; @@ -114,7 +120,7 @@ class IHTTPRequestHandler * * \return MHD_NO if a severe error has occurred otherwise MHD_YES. */ - virtual int HandleRequest() = 0; + virtual MHD_RESULT HandleRequest() = 0; /*! * \brief Whether the HTTP response could also be provided in ranges. From 8ef421ba6cd029e174bddf12120ccb355816d667 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 9 Jul 2020 22:30:40 -0400 Subject: [PATCH 2/2] [webserver] run clang-format --- xbmc/network/WebServer.cpp | 565 ++++++++++++++++++++++--------------- 1 file changed, 340 insertions(+), 225 deletions(-) diff --git a/xbmc/network/WebServer.cpp b/xbmc/network/WebServer.cpp index 53549aafa9ce..01b2dcdb1175 100644 --- a/xbmc/network/WebServer.cpp +++ b/xbmc/network/WebServer.cpp @@ -17,33 +17,37 @@ #include #endif +#include "ServiceBroker.h" +#include "Util.h" +#include "XBDateTime.h" #include "filesystem/File.h" #include "network/httprequesthandler/HTTPRequestHandlerUtils.h" #include "network/httprequesthandler/IHTTPRequestHandler.h" #include "settings/AdvancedSettings.h" #include "settings/Settings.h" #include "settings/SettingsComponent.h" -#include "ServiceBroker.h" #include "threads/SingleLock.h" -#include "Util.h" #include "utils/FileUtils.h" -#include "utils/log.h" #include "utils/Mime.h" #include "utils/StringUtils.h" #include "utils/URIUtils.h" #include "utils/Variant.h" -#include "XBDateTime.h" +#include "utils/log.h" #define MAX_POST_BUFFER_SIZE 2048 -#define PAGE_FILE_NOT_FOUND "File not foundFile not found" -#define NOT_SUPPORTED "Not SupportedThe method you are trying to use is not supported by this server" +#define PAGE_FILE_NOT_FOUND \ + "File not foundFile not found" +#define NOT_SUPPORTED \ + "Not SupportedThe method you are trying to use is not " \ + "supported by this server" #define HEADER_VALUE_NO_CACHE "no-cache" -#define HEADER_NEWLINE "\r\n" +#define HEADER_NEWLINE "\r\n" -typedef struct { +typedef struct +{ std::shared_ptr file; CHttpRanges ranges; size_t rangeCountTotal; @@ -56,13 +60,13 @@ typedef struct { } HttpFileDownloadContext; CWebServer::CWebServer() - : m_authenticationUsername("kodi"), - m_authenticationPassword(""), - m_key(), - m_cert() + : m_authenticationUsername("kodi") + , m_authenticationPassword("") + , m_key() + , m_cert() { #if defined(TARGET_DARWIN) - void *stack_addr; + void* stack_addr; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_getstack(&attr, &stack_addr, &m_thread_stacksize); @@ -88,7 +92,7 @@ static MHD_Response* create_response(size_t size, const void* data, int free, in MHD_RESULT CWebServer::AskForAuthentication(const HTTPRequest& request) const { - struct MHD_Response *response = create_response(0, nullptr, MHD_NO, MHD_NO); + struct MHD_Response* response = create_response(0, nullptr, MHD_NO, MHD_NO); if (!response) { CLog::Log(LOGERROR, "CWebServer[%hu]: unable to create HTTP Unauthorized response", m_port); @@ -108,7 +112,8 @@ MHD_RESULT CWebServer::AskForAuthentication(const HTTPRequest& request) const // This MHD_RESULT cast is only necessary for libmicrohttpd 0.9.71 // The return type of MHD_queue_basic_auth_fail_response was fixed for future versions // See https://git.gnunet.org/libmicrohttpd.git/commit/?id=860b42e9180da4dcd7e8690a3fcdb4e37e5772c5 - ret = static_cast(MHD_queue_basic_auth_fail_response(request.connection, "XBMC", response)); + ret = static_cast( + MHD_queue_basic_auth_fail_response(request.connection, "XBMC", response)); MHD_destroy_response(response); return ret; @@ -138,10 +143,14 @@ bool CWebServer::IsAuthenticated(const HTTPRequest& request) const return authenticated; } -MHD_RESULT CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection, - const char *url, const char *method, - const char *version, const char *upload_data, - size_t *upload_data_size, void **con_cls) +MHD_RESULT CWebServer::AnswerToConnection(void* cls, + struct MHD_Connection* connection, + const char* url, + const char* method, + const char* version, + const char* upload_data, + size_t* upload_data_size, + void** con_cls) { if (cls == nullptr || con_cls == nullptr || *con_cls == nullptr) { @@ -149,7 +158,7 @@ MHD_RESULT CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *conn return MHD_NO; } - CWebServer *webServer = reinterpret_cast(cls); + CWebServer* webServer = reinterpret_cast(cls); if (webServer == nullptr) { CLog::Log(LOGERROR, "CWebServer[unknown]: invalid request received"); @@ -158,15 +167,22 @@ MHD_RESULT CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *conn ConnectionHandler* connectionHandler = reinterpret_cast(*con_cls); HTTPMethod methodType = GetHTTPMethod(method); - HTTPRequest request = { webServer, connection, connectionHandler->fullUri, url, methodType, version }; + HTTPRequest request = {webServer, connection, connectionHandler->fullUri, + url, methodType, version}; if (connectionHandler->isNew) webServer->LogRequest(request); - return webServer->HandlePartialRequest(connection, connectionHandler, request, upload_data, upload_data_size, con_cls); + return webServer->HandlePartialRequest(connection, connectionHandler, request, upload_data, + upload_data_size, con_cls); } -MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, const char *upload_data, size_t *upload_data_size, void **con_cls) +MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection* connection, + ConnectionHandler* connectionHandler, + const HTTPRequest& request, + const char* upload_data, + size_t* upload_data_size, + void** con_cls) { std::unique_ptr conHandler(connectionHandler); @@ -199,20 +215,22 @@ MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection *connection, C if (handler->GetLastModifiedDate(lastModified) && lastModified.IsValid()) { // handle If-Modified-Since or If-Unmodified-Since - std::string ifModifiedSince = HTTPRequestHandlerUtils::GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_MODIFIED_SINCE); - std::string ifUnmodifiedSince = HTTPRequestHandlerUtils::GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE); + std::string ifModifiedSince = HTTPRequestHandlerUtils::GetRequestHeaderValue( + connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_MODIFIED_SINCE); + std::string ifUnmodifiedSince = HTTPRequestHandlerUtils::GetRequestHeaderValue( + connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE); CDateTime ifModifiedSinceDate; CDateTime ifUnmodifiedSinceDate; // handle If-Modified-Since (but only if the response is cacheable) - if (cacheable && - ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince) && - lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate) + if (cacheable && ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince) && + lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate) { - struct MHD_Response *response = create_response(0, nullptr, MHD_NO, MHD_NO); + struct MHD_Response* response = create_response(0, nullptr, MHD_NO, MHD_NO); if (response == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP 304 response", m_port); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP 304 response", + m_port); return MHD_NO; } @@ -220,7 +238,7 @@ MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection *connection, C } // handle If-Unmodified-Since else if (ifUnmodifiedSinceDate.SetFromRFC1123DateTime(ifUnmodifiedSince) && - lastModified.GetAsUTCDateTime() > ifUnmodifiedSinceDate) + lastModified.GetAsUTCDateTime() > ifUnmodifiedSinceDate) return SendErrorResponse(request, MHD_HTTP_PRECONDITION_FAILED, request.method); } @@ -275,19 +293,25 @@ MHD_RESULT CWebServer::HandlePartialRequest(struct MHD_Connection *connection, C return HandleRequest(requestHandler); } - CLog::Log(LOGERROR, "CWebServer[%hu]: couldn't find any request handler for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: couldn't find any request handler for %s", m_port, + request.pathUrl.c_str()); return SendErrorResponse(request, MHD_HTTP_NOT_FOUND, request.method); } -MHD_RESULT CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key, - const char *filename, const char *content_type, - const char *transfer_encoding, const char *data, uint64_t off, - size_t size) +MHD_RESULT CWebServer::HandlePostField(void* cls, + enum MHD_ValueKind kind, + const char* key, + const char* filename, + const char* content_type, + const char* transfer_encoding, + const char* data, + uint64_t off, + size_t size) { - ConnectionHandler *conHandler = (ConnectionHandler *)cls; + ConnectionHandler* conHandler = (ConnectionHandler*)cls; - if (conHandler == nullptr || conHandler->requestHandler == nullptr || - key == nullptr || data == nullptr || size == 0) + if (conHandler == nullptr || conHandler->requestHandler == nullptr || key == nullptr || + data == nullptr || size == 0) { CLog::Log(LOGERROR, "CWebServer: unable to handle HTTP POST field"); return MHD_NO; @@ -306,58 +330,64 @@ MHD_RESULT CWebServer::HandleRequest(const std::shared_ptr& MHD_RESULT ret = handler->HandleRequest(); if (ret == MHD_NO) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to handle HTTP request for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to handle HTTP request for %s", m_port, + request.pathUrl.c_str()); return SendErrorResponse(request, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } - const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); - struct MHD_Response *response = nullptr; + const HTTPResponseDetails& responseDetails = handler->GetResponseDetails(); + struct MHD_Response* response = nullptr; switch (responseDetails.type) { - case HTTPNone: - CLog::Log(LOGERROR, "CWebServer[%hu]: HTTP request handler didn't process %s", m_port, request.pathUrl.c_str()); - return MHD_NO; + case HTTPNone: + CLog::Log(LOGERROR, "CWebServer[%hu]: HTTP request handler didn't process %s", m_port, + request.pathUrl.c_str()); + return MHD_NO; - case HTTPRedirect: - ret = CreateRedirect(request.connection, handler->GetRedirectUrl(), response); - break; + case HTTPRedirect: + ret = CreateRedirect(request.connection, handler->GetRedirectUrl(), response); + break; - case HTTPFileDownload: - ret = CreateFileDownloadResponse(handler, response); - break; + case HTTPFileDownload: + ret = CreateFileDownloadResponse(handler, response); + break; - case HTTPMemoryDownloadNoFreeNoCopy: - case HTTPMemoryDownloadNoFreeCopy: - case HTTPMemoryDownloadFreeNoCopy: - case HTTPMemoryDownloadFreeCopy: - ret = CreateMemoryDownloadResponse(handler, response); - break; + case HTTPMemoryDownloadNoFreeNoCopy: + case HTTPMemoryDownloadNoFreeCopy: + case HTTPMemoryDownloadFreeNoCopy: + case HTTPMemoryDownloadFreeCopy: + ret = CreateMemoryDownloadResponse(handler, response); + break; - case HTTPError: - ret = CreateErrorResponse(request.connection, responseDetails.status, request.method, response); - break; + case HTTPError: + ret = CreateErrorResponse(request.connection, responseDetails.status, request.method, response); + break; - default: - CLog::Log(LOGERROR, "CWebServer[%hu]: internal error while HTTP request handler processed %s", m_port, request.pathUrl.c_str()); - return SendErrorResponse(request, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); + default: + CLog::Log(LOGERROR, "CWebServer[%hu]: internal error while HTTP request handler processed %s", + m_port, request.pathUrl.c_str()); + return SendErrorResponse(request, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } if (ret == MHD_NO) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create HTTP response for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create HTTP response for %s", m_port, + request.pathUrl.c_str()); return SendErrorResponse(request, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } return FinalizeRequest(handler, responseDetails.status, response); } -MHD_RESULT CWebServer::FinalizeRequest(const std::shared_ptr& handler, int responseStatus, struct MHD_Response *response) +MHD_RESULT CWebServer::FinalizeRequest(const std::shared_ptr& handler, + int responseStatus, + struct MHD_Response* response) { if (handler == nullptr || response == nullptr) return MHD_NO; - const HTTPRequest &request = handler->GetRequest(); - const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); + const HTTPRequest& request = handler->GetRequest(); + const HTTPResponseDetails& responseDetails = handler->GetResponseDetails(); // if the request handler has set a content type and it hasn't been set as a header, add it if (!responseDetails.contentType.empty()) @@ -383,7 +413,8 @@ MHD_RESULT CWebServer::FinalizeRequest(const std::shared_ptrCanBeCached() || maxAge == 0) - handler->AddResponseHeader(MHD_HTTP_HEADER_CACHE_CONTROL, "private, max-age=0, " HEADER_VALUE_NO_CACHE); + handler->AddResponseHeader(MHD_HTTP_HEADER_CACHE_CONTROL, + "private, max-age=0, " HEADER_VALUE_NO_CACHE); else { // create the value of the Cache-Control header @@ -410,23 +441,25 @@ MHD_RESULT CWebServer::FinalizeRequest(const std::shared_ptr 0) - handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, StringUtils::Format("%" PRIu64, responseDetails.totalLength)); + handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, + StringUtils::Format("%" PRIu64, responseDetails.totalLength)); // add all headers set by the request handler - for (std::multimap::const_iterator it = responseDetails.headers.begin(); it != responseDetails.headers.end(); ++it) + for (std::multimap::const_iterator it = responseDetails.headers.begin(); + it != responseDetails.headers.end(); ++it) AddHeader(response, it->first, it->second); return SendResponse(request, responseStatus, response); } -std::shared_ptr CWebServer::FindRequestHandler(const HTTPRequest& request) const +std::shared_ptr CWebServer::FindRequestHandler( + const HTTPRequest& request) const { // look for a IHTTPRequestHandler which can take care of the current request auto requestHandlerIt = std::find_if(m_requestHandlers.cbegin(), m_requestHandlers.cend(), - [&request](const IHTTPRequestHandler* requestHandler) - { - return requestHandler->CanHandleRequest(request); - }); + [&request](const IHTTPRequestHandler* requestHandler) { + return requestHandler->CanHandleRequest(request); + }); // we found a matching IHTTPRequestHandler so let's get a new instance for this request if (requestHandlerIt != m_requestHandlers.cend()) @@ -438,7 +471,8 @@ std::shared_ptr CWebServer::FindRequestHandler(const HTTPRe bool CWebServer::IsRequestCacheable(const HTTPRequest& request) const { // handle Cache-Control - std::string cacheControl = HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CACHE_CONTROL); + std::string cacheControl = HTTPRequestHandlerUtils::GetRequestHeaderValue( + request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CACHE_CONTROL); if (!cacheControl.empty()) { std::vector cacheControls = StringUtils::Split(cacheControl, ","); @@ -453,23 +487,26 @@ bool CWebServer::IsRequestCacheable(const HTTPRequest& request) const } // handle Pragma - std::string pragma = HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_PRAGMA); + std::string pragma = HTTPRequestHandlerUtils::GetRequestHeaderValue( + request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_PRAGMA); if (pragma.compare(HEADER_VALUE_NO_CACHE) == 0) return false; return true; } -bool CWebServer::IsRequestRanged(const HTTPRequest& request, const CDateTime &lastModified) const +bool CWebServer::IsRequestRanged(const HTTPRequest& request, const CDateTime& lastModified) const { // parse the Range header and store it in the request object CHttpRanges ranges; - bool ranged = ranges.Parse(HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE)); + bool ranged = ranges.Parse(HTTPRequestHandlerUtils::GetRequestHeaderValue( + request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE)); // handle If-Range header but only if the Range header is present if (ranged && lastModified.IsValid()) { - std::string ifRange = HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_RANGE); + std::string ifRange = HTTPRequestHandlerUtils::GetRequestHeaderValue( + request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_RANGE); if (!ifRange.empty() && lastModified.IsValid()) { CDateTime ifRangeDate; @@ -485,7 +522,10 @@ bool CWebServer::IsRequestRanged(const HTTPRequest& request, const CDateTime &la return !ranges.IsEmpty(); } -void CWebServer::SetupPostDataProcessing(const HTTPRequest& request, ConnectionHandler *connectionHandler, std::shared_ptr handler, void **con_cls) const +void CWebServer::SetupPostDataProcessing(const HTTPRequest& request, + ConnectionHandler* connectionHandler, + std::shared_ptr handler, + void** con_cls) const { connectionHandler->requestHandler = handler; @@ -493,7 +533,8 @@ void CWebServer::SetupPostDataProcessing(const HTTPRequest& request, ConnectionH *con_cls = connectionHandler; // get the content-type of the POST data - const auto contentType = HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE); + const auto contentType = HTTPRequestHandlerUtils::GetRequestHeaderValue( + request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE); if (contentType.empty()) return; @@ -503,21 +544,31 @@ void CWebServer::SetupPostDataProcessing(const HTTPRequest& request, ConnectionH return; // otherwise we can use MHD's POST processor - connectionHandler->postprocessor = MHD_create_post_processor(request.connection, MAX_POST_BUFFER_SIZE, &CWebServer::HandlePostField, static_cast(connectionHandler)); + connectionHandler->postprocessor = MHD_create_post_processor( + request.connection, MAX_POST_BUFFER_SIZE, &CWebServer::HandlePostField, + static_cast(connectionHandler)); // MHD doesn't seem to be able to handle this post request if (connectionHandler->postprocessor == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: unable to create HTTP POST processor for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: unable to create HTTP POST processor for %s", m_port, + request.pathUrl.c_str()); connectionHandler->errorStatus = MHD_HTTP_INTERNAL_SERVER_ERROR; } } -bool CWebServer::ProcessPostData(const HTTPRequest& request, ConnectionHandler *connectionHandler, const char *upload_data, size_t *upload_data_size, void **con_cls) const +bool CWebServer::ProcessPostData(const HTTPRequest& request, + ConnectionHandler* connectionHandler, + const char* upload_data, + size_t* upload_data_size, + void** con_cls) const { if (connectionHandler->requestHandler == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: cannot handle partial HTTP POST for %s request because there is no valid request handler available", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, + "CWebServer[%hu]: cannot handle partial HTTP POST for %s request because there is no " + "valid request handler available", + m_port, request.pathUrl.c_str()); connectionHandler->errorStatus = MHD_HTTP_INTERNAL_SERVER_ERROR; } @@ -534,15 +585,18 @@ bool CWebServer::ProcessPostData(const HTTPRequest& request, ConnectionHandler * bool postDataHandled = false; // either use MHD's POST processor if (connectionHandler->postprocessor != nullptr) - postDataHandled = MHD_post_process(connectionHandler->postprocessor, upload_data, *upload_data_size) == MHD_YES; + postDataHandled = MHD_post_process(connectionHandler->postprocessor, upload_data, + *upload_data_size) == MHD_YES; // or simply copy the data to the handler else if (connectionHandler->requestHandler != nullptr) - postDataHandled = connectionHandler->requestHandler->AddPostData(upload_data, *upload_data_size); + postDataHandled = + connectionHandler->requestHandler->AddPostData(upload_data, *upload_data_size); // abort if the received POST data couldn't be handled if (!postDataHandled) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to handle HTTP POST data for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to handle HTTP POST data for %s", m_port, + request.pathUrl.c_str()); #if (MHD_VERSION >= 0x00095213) connectionHandler->errorStatus = MHD_HTTP_PAYLOAD_TOO_LARGE; #else @@ -557,7 +611,7 @@ bool CWebServer::ProcessPostData(const HTTPRequest& request, ConnectionHandler * return true; } -void CWebServer::FinalizePostDataProcessing(ConnectionHandler *connectionHandler) const +void CWebServer::FinalizePostDataProcessing(ConnectionHandler* connectionHandler) const { if (connectionHandler->postprocessor == nullptr) return; @@ -565,13 +619,14 @@ void CWebServer::FinalizePostDataProcessing(ConnectionHandler *connectionHandler MHD_destroy_post_processor(connectionHandler->postprocessor); } -MHD_RESULT CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateMemoryDownloadResponse( + const std::shared_ptr& handler, struct MHD_Response*& response) const { if (handler == nullptr) return MHD_NO; - const HTTPRequest &request = handler->GetRequest(); - const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); + const HTTPRequest& request = handler->GetRequest(); + const HTTPResponseDetails& responseDetails = handler->GetResponseDetails(); HttpResponseRanges responseRanges = handler->GetResponseData(); // check if the response is completely empty @@ -580,9 +635,11 @@ MHD_RESULT CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr 1) || - (!request.ranges.IsEmpty() && responseRanges.size() > request.ranges.Size())) + (!request.ranges.IsEmpty() && responseRanges.size() > request.ranges.Size())) { - CLog::Log(LOGWARNING, "CWebServer[%hu]: response contains more ranges (%d) than the request asked for (%d)", m_port, (int)responseRanges.size(), (int)request.ranges.Size()); + CLog::Log(LOGWARNING, + "CWebServer[%hu]: response contains more ranges (%d) than the request asked for (%d)", + m_port, (int)responseRanges.size(), (int)request.ranges.Size()); return SendErrorResponse(request, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } @@ -594,7 +651,10 @@ MHD_RESULT CWebServer::CreateMemoryDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse( + const std::shared_ptr& handler, struct MHD_Response*& response) const { if (handler == nullptr) return MHD_NO; - const HTTPRequest &request = handler->GetRequest(); - const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); + const HTTPRequest& request = handler->GetRequest(); + const HTTPResponseDetails& responseDetails = handler->GetResponseDetails(); HttpResponseRanges responseRanges = handler->GetResponseData(); // if there's no or only one range this is not the right place @@ -639,7 +704,8 @@ MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr< // extract all the valid ranges and calculate their total length uint64_t firstRangePosition = 0; HttpResponseRanges ranges; - for (HttpResponseRanges::const_iterator range = responseRanges.begin(); range != responseRanges.end(); ++range) + for (HttpResponseRanges::const_iterator range = responseRanges.begin(); + range != responseRanges.end(); ++range) { // ignore invalid ranges if (!range->IsValid()) @@ -661,8 +727,10 @@ MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr< // adjust the HTTP status of the response handler->SetResponseStatus(MHD_HTTP_PARTIAL_CONTENT); // add Content-Range header - handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_RANGE, - HttpRangeUtils::GenerateContentRangeHeaderValue(firstRangePosition, lastRangePosition, responseDetails.totalLength)); + handler->AddResponseHeader( + MHD_HTTP_HEADER_CONTENT_RANGE, + HttpRangeUtils::GenerateContentRangeHeaderValue(firstRangePosition, lastRangePosition, + responseDetails.totalLength)); // generate a multipart boundary std::string multipartBoundary = HttpRangeUtils::GenerateMultipartBoundary(); @@ -673,7 +741,8 @@ MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr< handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_TYPE, contentType); // generate the multipart boundary with the Content-Type header field - std::string multipartBoundaryWithHeader = HttpRangeUtils::GenerateMultipartBoundaryWithHeader(multipartBoundary, contentType); + std::string multipartBoundaryWithHeader = + HttpRangeUtils::GenerateMultipartBoundaryWithHeader(multipartBoundary, contentType); std::string result; // add all the ranges to the result @@ -684,31 +753,39 @@ MHD_RESULT CWebServer::CreateRangedMemoryDownloadResponse(const std::shared_ptr< result += HEADER_NEWLINE; // generate and append the multipart boundary with the full header (Content-Type and Content-Length) - result += HttpRangeUtils::GenerateMultipartBoundaryWithHeader(multipartBoundaryWithHeader, &*range); + result += + HttpRangeUtils::GenerateMultipartBoundaryWithHeader(multipartBoundaryWithHeader, &*range); // and append the data of the range - result.append(static_cast(range->GetData()), static_cast(range->GetLength())); + result.append(static_cast(range->GetData()), + static_cast(range->GetLength())); // check if we need to free the range data - if (responseDetails.type == HTTPMemoryDownloadFreeNoCopy || responseDetails.type == HTTPMemoryDownloadFreeCopy) + if (responseDetails.type == HTTPMemoryDownloadFreeNoCopy || + responseDetails.type == HTTPMemoryDownloadFreeCopy) free(const_cast(range->GetData())); } result += HttpRangeUtils::GenerateMultipartBoundaryEnd(multipartBoundary); // add Content-Length header - handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, StringUtils::Format("%" PRIu64, static_cast(result.size()))); + handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, + StringUtils::Format("%" PRIu64, static_cast(result.size()))); // finally create the response - return CreateMemoryDownloadResponse(request.connection, result.c_str(), result.size(), false, true, response); + return CreateMemoryDownloadResponse(request.connection, result.c_str(), result.size(), false, + true, response); } -MHD_RESULT CWebServer::CreateRedirect(struct MHD_Connection *connection, const std::string &strURL, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateRedirect(struct MHD_Connection* connection, + const std::string& strURL, + struct MHD_Response*& response) const { response = create_response(0, nullptr, MHD_NO, MHD_NO); if (response == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create HTTP redirect response to %s", m_port, strURL.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create HTTP redirect response to %s", m_port, + strURL.c_str()); return MHD_NO; } @@ -716,13 +793,14 @@ MHD_RESULT CWebServer::CreateRedirect(struct MHD_Connection *connection, const s return MHD_YES; } -MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptr& handler, struct MHD_Response *&response) const +MHD_RESULT CWebServer::CreateFileDownloadResponse( + const std::shared_ptr& handler, struct MHD_Response*& response) const { if (handler == nullptr) return MHD_NO; - const HTTPRequest &request = handler->GetRequest(); - const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); + const HTTPRequest& request = handler->GetRequest(); + const HTTPResponseDetails& responseDetails = handler->GetResponseDetails(); HttpResponseRanges responseRanges = handler->GetResponseData(); std::shared_ptr file = std::make_shared(); @@ -764,7 +842,8 @@ MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptrranges = request.ranges; else - HTTPRequestHandlerUtils::GetRequestedRanges(request.connection, fileLength, context->ranges); + HTTPRequestHandlerUtils::GetRequestedRanges(request.connection, fileLength, + context->ranges); } uint64_t firstPosition = 0; @@ -796,14 +875,18 @@ MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptr\r\nContent-Type: \r\n - context->boundaryWithHeader = HttpRangeUtils::GenerateMultipartBoundaryWithHeader(context->boundary, context->contentType); + context->boundaryWithHeader = HttpRangeUtils::GenerateMultipartBoundaryWithHeader( + context->boundary, context->contentType); context->boundaryEnd = HttpRangeUtils::GenerateMultipartBoundaryEnd(context->boundary); // for every range, we need to add a boundary with header - for (HttpRanges::const_iterator range = context->ranges.Begin(); range != context->ranges.End(); ++range) + for (HttpRanges::const_iterator range = context->ranges.Begin(); + range != context->ranges.End(); ++range) { // we need to temporarily add the Content-Range header to the boundary to be able to determine the length - std::string completeBoundaryWithHeader = HttpRangeUtils::GenerateMultipartBoundaryWithHeader(context->boundaryWithHeader, &*range); + std::string completeBoundaryWithHeader = + HttpRangeUtils::GenerateMultipartBoundaryWithHeader(context->boundaryWithHeader, + &*range); totalLength += completeBoundaryWithHeader.size(); // add a newline before any new multipart boundary @@ -818,13 +901,14 @@ MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptrranges.GetFirstPosition(context->writePosition); // create the response object - response = MHD_create_response_from_callback(totalLength, 2048, - &CWebServer::ContentReaderCallback, - context.get(), - &CWebServer::ContentReaderFreeCallback); + response = + MHD_create_response_from_callback(totalLength, 2048, &CWebServer::ContentReaderCallback, + context.get(), &CWebServer::ContentReaderFreeCallback); if (response == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP response for %s to be filled from %s", m_port, request.pathUrl.c_str(), filePath.c_str()); + CLog::Log(LOGERROR, + "CWebServer[%hu]: failed to create a HTTP response for %s to be filled from %s", + m_port, request.pathUrl.c_str(), filePath.c_str()); return MHD_NO; } @@ -832,18 +916,22 @@ MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptrAddResponseHeader(MHD_HTTP_HEADER_CONTENT_RANGE, HttpRangeUtils::GenerateContentRangeHeaderValue(firstPosition, lastPosition, fileLength)); + handler->AddResponseHeader( + MHD_HTTP_HEADER_CONTENT_RANGE, + HttpRangeUtils::GenerateContentRangeHeaderValue(firstPosition, lastPosition, fileLength)); } else { response = create_response(0, nullptr, MHD_NO, MHD_NO); if (response == nullptr) { - CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP HEAD response for %s", m_port, request.pathUrl.c_str()); + CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP HEAD response for %s", m_port, + request.pathUrl.c_str()); return MHD_NO; } - handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, StringUtils::Format("%" PRId64, fileLength)); + handler->AddResponseHeader(MHD_HTTP_HEADER_CONTENT_LENGTH, + StringUtils::Format("%" PRId64, fileLength)); } // set the Content-Type header @@ -853,40 +941,50 @@ MHD_RESULT CWebServer::CreateFileDownloadResponse(const std::shared_ptr(data), free ? MHD_YES : MHD_NO, copy ? MHD_YES : MHD_NO); + response = create_response(size, const_cast(data), free ? MHD_YES : MHD_NO, + copy ? MHD_YES : MHD_NO); if (response == nullptr) { CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP download response", m_port); @@ -896,7 +994,9 @@ MHD_RESULT CWebServer::CreateMemoryDownloadResponse(struct MHD_Connection *conne return MHD_YES; } -MHD_RESULT CWebServer::SendResponse(const HTTPRequest& request, int responseStatus, MHD_Response *response) const +MHD_RESULT CWebServer::SendResponse(const HTTPRequest& request, + int responseStatus, + MHD_Response* response) const { LogResponse(request, responseStatus); @@ -906,9 +1006,11 @@ MHD_RESULT CWebServer::SendResponse(const HTTPRequest& request, int responseStat return ret; } -MHD_RESULT CWebServer::SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const +MHD_RESULT CWebServer::SendErrorResponse(const HTTPRequest& request, + int errorType, + HTTPMethod method) const { - struct MHD_Response *response = nullptr; + struct MHD_Response* response = nullptr; MHD_RESULT ret = CreateErrorResponse(request.connection, errorType, method, response); if (ret == MHD_NO) return MHD_NO; @@ -916,9 +1018,9 @@ MHD_RESULT CWebServer::SendErrorResponse(const HTTPRequest& request, int errorTy return SendResponse(request, errorType, response); } -void* CWebServer::UriRequestLogger(void *cls, const char *uri) +void* CWebServer::UriRequestLogger(void* cls, const char* uri) { - CWebServer *webServer = reinterpret_cast(cls); + CWebServer* webServer = reinterpret_cast(cls); // log the full URI if (webServer == nullptr) @@ -938,13 +1040,15 @@ void CWebServer::LogRequest(const char* uri) const CLog::Log(LOGDEBUG, "CWebServer[%hu]: request received for %s", m_port, uri); } -ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, size_t max) +ssize_t CWebServer::ContentReaderCallback(void* cls, uint64_t pos, char* buf, size_t max) { - HttpFileDownloadContext *context = (HttpFileDownloadContext *)cls; + HttpFileDownloadContext* context = (HttpFileDownloadContext*)cls; if (context == nullptr || context->file == nullptr) return -1; - CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer [OUT] write maximum %zu bytes from %" PRIu64 " (%" PRIu64 ")", max, context->writePosition, pos); + CLog::Log(LOGDEBUG, LOGWEBSERVER, + "CWebServer [OUT] write maximum %zu bytes from %" PRIu64 " (%" PRIu64 ")", max, + context->writePosition, pos); // check if we need to add the end-boundary if (context->rangeCountTotal > 1 && context->ranges.IsEmpty()) @@ -981,7 +1085,8 @@ ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, si } // put together the boundary for the current range - std::string boundary = HttpRangeUtils::GenerateMultipartBoundaryWithHeader(context->boundaryWithHeader, &range); + std::string boundary = + HttpRangeUtils::GenerateMultipartBoundaryWithHeader(context->boundaryWithHeader, &range); // copy the boundary into the buffer memcpy(buf, boundary.c_str(), boundary.size()); @@ -1002,7 +1107,8 @@ ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, si maximum = std::min(maximum, end - context->writePosition + 1); // seek to the position if necessary - if (context->file->GetPosition() < 0 || context->writePosition != static_cast(context->file->GetPosition())) + if (context->file->GetPosition() < 0 || + context->writePosition != static_cast(context->file->GetPosition())) context->file->Seek(context->writePosition); // read data from the file @@ -1013,7 +1119,9 @@ ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, si // add the number of read bytes to the number of written bytes written += res; - CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer [OUT] wrote %d bytes from %" PRIu64 " in range (%" PRIu64 " - %" PRIu64 ")", written, context->writePosition, start, end); + CLog::Log(LOGDEBUG, LOGWEBSERVER, + "CWebServer [OUT] wrote %d bytes from %" PRIu64 " in range (%" PRIu64 " - %" PRIu64 ")", + written, context->writePosition, start, end); // update the current write position context->writePosition += res; @@ -1029,19 +1137,22 @@ ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, si return written; } -void CWebServer::ContentReaderFreeCallback(void *cls) +void CWebServer::ContentReaderFreeCallback(void* cls) { - HttpFileDownloadContext *context = (HttpFileDownloadContext *)cls; + HttpFileDownloadContext* context = (HttpFileDownloadContext*)cls; delete context; CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer [OUT] done"); } // local helper -static void panicHandlerForMHD(void* unused, const char* file, unsigned int line, const char *reason) +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); + 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? } @@ -1066,7 +1177,7 @@ static void logFromMHD(void* unused, const char* fmt, va_list ap) } } -bool CWebServer::LoadCert(std::string &skey, std::string &scert) +bool CWebServer::LoadCert(std::string& skey, std::string& scert) { XFILE::CFile file; XFILE::auto_buffer buf; @@ -1096,7 +1207,9 @@ bool CWebServer::LoadCert(std::string &skey, std::string &scert) if (!skey.empty() && !scert.empty()) { - CLog::Log(LOGERROR, "WebServer %s: found server key: %s, certificate: %s, HTTPS support enabled", __FUNCTION__, keyFile, certFile); + CLog::Log(LOGERROR, + "WebServer %s: found server key: %s, certificate: %s, HTTPS support enabled", + __FUNCTION__, keyFile, certFile); return true; } return false; @@ -1109,63 +1222,51 @@ struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port) MHD_set_panic_func(&panicHandlerForMHD, nullptr); - if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_SERVICES_WEBSERVERSSL) && - MHD_is_feature_supported(MHD_FEATURE_SSL) == MHD_YES && - LoadCert(m_key, m_cert)) + if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool( + CSettings::SETTING_SERVICES_WEBSERVERSSL) && + MHD_is_feature_supported(MHD_FEATURE_SSL) == MHD_YES && LoadCert(m_key, m_cert)) // SSL enabled - return MHD_start_daemon(flags | - // one thread per connection - // WARNING: set MHD_OPTION_CONNECTION_TIMEOUT to something higher than 1 - // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop - MHD_USE_THREAD_PER_CONNECTION + return MHD_start_daemon( + flags | + // one thread per connection + // WARNING: set MHD_OPTION_CONNECTION_TIMEOUT to something higher than 1 + // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop + MHD_USE_THREAD_PER_CONNECTION #if (MHD_VERSION >= 0x00095207) - | MHD_USE_INTERNAL_POLLING_THREAD /* MHD_USE_THREAD_PER_CONNECTION must be used only with MHD_USE_INTERNAL_POLLING_THREAD since 0.9.54 */ + | + MHD_USE_INTERNAL_POLLING_THREAD /* MHD_USE_THREAD_PER_CONNECTION must be used only with MHD_USE_INTERNAL_POLLING_THREAD since 0.9.54 */ #endif - | MHD_USE_DEBUG /* Print MHD error messages to log */ - | MHD_USE_SSL - , - port, - 0, - 0, - &CWebServer::AnswerToConnection, - this, - - MHD_OPTION_CONNECTION_LIMIT, 512, - MHD_OPTION_CONNECTION_TIMEOUT, timeout, - MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this, - MHD_OPTION_EXTERNAL_LOGGER, &logFromMHD, 0, - MHD_OPTION_THREAD_STACK_SIZE, m_thread_stacksize, - MHD_OPTION_HTTPS_MEM_KEY, m_key.c_str(), - MHD_OPTION_HTTPS_MEM_CERT, m_cert.c_str(), - MHD_OPTION_HTTPS_PRIORITIES, ciphers, - MHD_OPTION_END); + | MHD_USE_DEBUG /* Print MHD error messages to log */ + | MHD_USE_SSL, + port, 0, 0, &CWebServer::AnswerToConnection, this, + + MHD_OPTION_CONNECTION_LIMIT, 512, MHD_OPTION_CONNECTION_TIMEOUT, timeout, + MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this, + MHD_OPTION_EXTERNAL_LOGGER, &logFromMHD, 0, MHD_OPTION_THREAD_STACK_SIZE, + m_thread_stacksize, MHD_OPTION_HTTPS_MEM_KEY, m_key.c_str(), MHD_OPTION_HTTPS_MEM_CERT, + m_cert.c_str(), MHD_OPTION_HTTPS_PRIORITIES, ciphers, MHD_OPTION_END); // No SSL - return MHD_start_daemon(flags | - // one thread per connection - // WARNING: set MHD_OPTION_CONNECTION_TIMEOUT to something higher than 1 - // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop - MHD_USE_THREAD_PER_CONNECTION + return MHD_start_daemon( + flags | + // one thread per connection + // WARNING: set MHD_OPTION_CONNECTION_TIMEOUT to something higher than 1 + // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop + MHD_USE_THREAD_PER_CONNECTION #if (MHD_VERSION >= 0x00095207) - | MHD_USE_INTERNAL_POLLING_THREAD /* MHD_USE_THREAD_PER_CONNECTION must be used only with MHD_USE_INTERNAL_POLLING_THREAD since 0.9.54 */ + | + MHD_USE_INTERNAL_POLLING_THREAD /* MHD_USE_THREAD_PER_CONNECTION must be used only with MHD_USE_INTERNAL_POLLING_THREAD since 0.9.54 */ #endif - | MHD_USE_DEBUG /* Print MHD error messages to log */ - , - port, - 0, - 0, - &CWebServer::AnswerToConnection, - this, - - MHD_OPTION_CONNECTION_LIMIT, 512, - MHD_OPTION_CONNECTION_TIMEOUT, timeout, - MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this, - MHD_OPTION_EXTERNAL_LOGGER, &logFromMHD, 0, - MHD_OPTION_THREAD_STACK_SIZE, m_thread_stacksize, - MHD_OPTION_END); + | MHD_USE_DEBUG /* Print MHD error messages to log */ + , + port, 0, 0, &CWebServer::AnswerToConnection, this, + + MHD_OPTION_CONNECTION_LIMIT, 512, MHD_OPTION_CONNECTION_TIMEOUT, timeout, + MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this, MHD_OPTION_EXTERNAL_LOGGER, + &logFromMHD, 0, MHD_OPTION_THREAD_STACK_SIZE, m_thread_stacksize, MHD_OPTION_END); } -bool CWebServer::Start(uint16_t port, const std::string &username, const std::string &password) +bool CWebServer::Start(uint16_t port, const std::string& username, const std::string& password) { SetCredentials(username, password); if (!m_running) @@ -1219,7 +1320,7 @@ bool CWebServer::WebServerSupportsSSL() return MHD_is_feature_supported(MHD_FEATURE_SSL) == MHD_YES; } -void CWebServer::SetCredentials(const std::string &username, const std::string &password) +void CWebServer::SetCredentials(const std::string& username, const std::string& password) { CSingleLock lock(m_critSection); @@ -1228,7 +1329,7 @@ void CWebServer::SetCredentials(const std::string &username, const std::string & m_authenticationRequired = !m_authenticationPassword.empty(); } -void CWebServer::RegisterRequestHandler(IHTTPRequestHandler *handler) +void CWebServer::RegisterRequestHandler(IHTTPRequestHandler* handler) { if (handler == nullptr) return; @@ -1239,15 +1340,18 @@ void CWebServer::RegisterRequestHandler(IHTTPRequestHandler *handler) m_requestHandlers.push_back(handler); std::sort(m_requestHandlers.begin(), m_requestHandlers.end(), - [](IHTTPRequestHandler* lhs, IHTTPRequestHandler* rhs) { return rhs->GetPriority() < lhs->GetPriority(); }); + [](IHTTPRequestHandler* lhs, IHTTPRequestHandler* rhs) { + return rhs->GetPriority() < lhs->GetPriority(); + }); } -void CWebServer::UnregisterRequestHandler(IHTTPRequestHandler *handler) +void CWebServer::UnregisterRequestHandler(IHTTPRequestHandler* handler) { if (handler == nullptr) return; - m_requestHandlers.erase(std::remove(m_requestHandlers.begin(), m_requestHandlers.end(), handler), m_requestHandlers.end()); + m_requestHandlers.erase(std::remove(m_requestHandlers.begin(), m_requestHandlers.end(), handler), + m_requestHandlers.end()); } void CWebServer::LogRequest(const HTTPRequest& request) const @@ -1256,11 +1360,14 @@ void CWebServer::LogRequest(const HTTPRequest& request) const return; std::multimap headerValues; - HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_HEADER_KIND, headerValues); + HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_HEADER_KIND, + headerValues); std::multimap getValues; - HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_GET_ARGUMENT_KIND, getValues); + HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_GET_ARGUMENT_KIND, + getValues); - CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] %s %s %s", m_port, request.version.c_str(), GetHTTPMethod(request.method).c_str(), request.pathUrlFull.c_str()); + CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] %s %s %s", m_port, request.version.c_str(), + GetHTTPMethod(request.method).c_str(), request.pathUrlFull.c_str()); if (!getValues.empty()) { @@ -1268,11 +1375,13 @@ void CWebServer::LogRequest(const HTTPRequest& request) const for (const auto get : getValues) values.push_back(get.first + " = " + get.second); - CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] Query arguments: %s", m_port, StringUtils::Join(values, "; ").c_str()); + CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] Query arguments: %s", m_port, + StringUtils::Join(values, "; ").c_str()); } for (const auto header : headerValues) - CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] %s: %s", m_port, header.first.c_str(), header.second.c_str()); + CLog::Log(LOGDEBUG, "CWebServer[%hu] [IN] %s: %s", m_port, header.first.c_str(), + header.second.c_str()); } void CWebServer::LogResponse(const HTTPRequest& request, int responseStatus) const @@ -1281,15 +1390,18 @@ void CWebServer::LogResponse(const HTTPRequest& request, int responseStatus) con return; std::multimap headerValues; - HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_HEADER_KIND, headerValues); + HTTPRequestHandlerUtils::GetRequestHeaderValues(request.connection, MHD_HEADER_KIND, + headerValues); - CLog::Log(LOGDEBUG, "CWebServer[%hu] [OUT] %s %d %s", m_port, request.version.c_str(), responseStatus, request.pathUrlFull.c_str()); + CLog::Log(LOGDEBUG, "CWebServer[%hu] [OUT] %s %d %s", m_port, request.version.c_str(), + responseStatus, request.pathUrlFull.c_str()); for (const auto header : headerValues) - CLog::Log(LOGDEBUG, "CWebServer[%hu] [OUT] %s: %s", m_port, header.first.c_str(), header.second.c_str()); + CLog::Log(LOGDEBUG, "CWebServer[%hu] [OUT] %s: %s", m_port, header.first.c_str(), + header.second.c_str()); } -std::string CWebServer::CreateMimeTypeFromExtension(const char *ext) +std::string CWebServer::CreateMimeTypeFromExtension(const char* ext) { if (strcmp(ext, ".kar") == 0) return "audio/midi"; @@ -1299,12 +1411,15 @@ std::string CWebServer::CreateMimeTypeFromExtension(const char *ext) return CMime::GetMimeType(ext); } -MHD_RESULT CWebServer::AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value) const +MHD_RESULT CWebServer::AddHeader(struct MHD_Response* response, + const std::string& name, + const std::string& value) const { if (response == nullptr || name.empty()) return MHD_NO; - CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer[%hu] [OUT] %s: %s", m_port, name.c_str(), value.c_str()); + CLog::Log(LOGDEBUG, LOGWEBSERVER, "CWebServer[%hu] [OUT] %s: %s", m_port, name.c_str(), + value.c_str()); return MHD_add_response_header(response, name.c_str(), value.c_str()); }