From 6e66c5c3628f3013ae0aa786309b4bb0ba39ec0c Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Thu, 2 Jan 2025 18:42:50 -0800 Subject: [PATCH] works --- dChatServer/ChatWebAPI.cpp | 23 +++++++++++------------ dChatServer/ChatWebAPI.h | 9 +++------ dCommon/GeneralUtils.cpp | 15 +++++++++++++++ dCommon/GeneralUtils.h | 5 +++++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/dChatServer/ChatWebAPI.cpp b/dChatServer/ChatWebAPI.cpp index 1948ca73..4ba9d2af 100644 --- a/dChatServer/ChatWebAPI.cpp +++ b/dChatServer/ChatWebAPI.cpp @@ -10,9 +10,17 @@ #include "PlayerContainer.h" #include "GeneralUtils.h" -void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) { +typedef struct mg_connection mg_connection; +typedef struct mg_http_message mg_http_message; + +namespace { + const std::string root_path = "/api/v1/"; + const char* json_content_type = "Content-Type: application/json\r\n"; +} + +void HandleRequests(mg_connection* connection, int request, void* request_data) { if (request == MG_EV_HTTP_MSG) { - struct mg_http_message* http_msg = static_cast(request_data); + const mg_http_message* const http_msg = static_cast(request_data); if (!http_msg) { mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid Request\"}"); return; @@ -22,7 +30,7 @@ void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, v if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) { // handle announcements if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) { - auto data = ParseJSON(http_msg->body.buf); + auto data = GeneralUtils::TryParse(http_msg->body.buf); if (!data) { mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}"); return; @@ -133,12 +141,3 @@ void ChatWebAPI::Listen() { void ChatWebAPI::ReceiveRequests() { mg_mgr_poll(&mgr, 15); } - -std::optional ChatWebAPI::ParseJSON(char* data) { - try { - return std::make_optional(json::parse(data)); - } catch (const std::exception& e) { - LOG_DEBUG("Failed to parse JSON: %s", e.what()); - return std::nullopt; - } -} diff --git a/dChatServer/ChatWebAPI.h b/dChatServer/ChatWebAPI.h index f563fd1b..591b207d 100644 --- a/dChatServer/ChatWebAPI.h +++ b/dChatServer/ChatWebAPI.h @@ -7,6 +7,8 @@ using json = nlohmann::json; +typedef struct mg_mgr mg_mgr; + class ChatWebAPI { public: ChatWebAPI(); @@ -14,12 +16,7 @@ public: void ReceiveRequests(); void Listen(); private: - static void HandleRequests(struct mg_connection *c, int ev, void *ev_data); - static std::optional ParseJSON(char * data); - - struct mg_mgr mgr; - inline static const std::string root_path = "/api/v1/"; - inline static const char * json_content_type = "Content-Type: application/json\r\n"; + mg_mgr mgr; }; #endif diff --git a/dCommon/GeneralUtils.cpp b/dCommon/GeneralUtils.cpp index fcf6e2e7..03b6a0bc 100644 --- a/dCommon/GeneralUtils.cpp +++ b/dCommon/GeneralUtils.cpp @@ -327,6 +327,21 @@ std::vector GeneralUtils::GetSqlFileNamesFromFolder(const std::stri return sortedFiles; } +#include "json.hpp" + +using json = nlohmann::json; + +template<> +[[nodiscard]] std::optional GeneralUtils::TryParse(std::string_view str) { + try { + return json::parse(str); + } catch (const std::exception& e) { + return std::nullopt; + } catch (...) { + return std::nullopt; + } +} + #if !(__GNUC__ >= 11 || _MSC_VER >= 1924) // MacOS floating-point parse function specializations diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index 663ac7bc..1565a3d7 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -18,6 +18,7 @@ #include "dPlatforms.h" #include "Game.h" #include "Logger.h" +#include "json_fwd.hpp" enum eInventoryType : uint32_t; enum class eObjectBits : size_t; @@ -201,6 +202,10 @@ namespace GeneralUtils { return isParsed ? static_cast(result) : std::optional{}; } + template + requires(!Numeric) + [[nodiscard]] std::optional TryParse(std::string_view str); + #if !(__GNUC__ >= 11 || _MSC_VER >= 1924) // MacOS floating-point parse helper function specializations