Index: ps/trunk/source/ps/scripting/JSInterface_Game.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_Game.cpp +++ ps/trunk/source/ps/scripting/JSInterface_Game.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -24,9 +24,9 @@ #include "network/NetServer.h" #include "ps/CLogger.h" #include "ps/Game.h" +#include "ps/GameSetup/GameSetup.h" #include "ps/Replay.h" #include "ps/World.h" -#include "ps/GameSetup/GameSetup.h" #include "scriptinterface/ScriptInterface.h" #include "simulation2/system/TurnManager.h" #include "simulation2/Simulation2.h" Index: ps/trunk/source/rlinterface/RLInterface.h =================================================================== --- ps/trunk/source/rlinterface/RLInterface.h +++ ps/trunk/source/rlinterface/RLInterface.h @@ -92,6 +92,7 @@ private: static void* MgCallback(mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info); + static std::string GetRequestContent(struct mg_connection *conn); /** * Process commands, update the simulation by one turn. Index: ps/trunk/source/rlinterface/RLInterface.cpp =================================================================== --- ps/trunk/source/rlinterface/RLInterface.cpp +++ ps/trunk/source/rlinterface/RLInterface.cpp @@ -24,8 +24,8 @@ #include "gui/GUIManager.h" #include "ps/CLogger.h" #include "ps/Game.h" -#include "ps/Loader.h" #include "ps/GameSetup/GameSetup.h" +#include "ps/Loader.h" #include "simulation2/Simulation2.h" #include "simulation2/components/ICmpAIInterface.h" #include "simulation2/components/ICmpTemplateManager.h" @@ -133,8 +133,8 @@ if (uri == "/reset") { - const char* val = mg_get_header(conn, "Content-Length"); - if (!val) + std::string data = GetRequestContent(conn); + if (data.empty()) { mg_printf(conn, "%s", noPostData); return handled; @@ -149,11 +149,7 @@ if (len != -1) scenario.playerID = std::stoi(playerID); - const int bufSize = std::atoi(val); - std::unique_ptr buf = std::unique_ptr(new char[bufSize]); - mg_read(conn, buf.get(), bufSize); - const std::string content(buf.get(), bufSize); - scenario.content = content; + scenario.content = std::move(data); const std::string gameState = interface->Reset(std::move(scenario)); @@ -167,30 +163,21 @@ return handled; } - const char* val = mg_get_header(conn, "Content-Length"); - if (!val) - { - mg_printf(conn, "%s", noPostData); - return handled; - } - int bufSize = std::atoi(val); - std::unique_ptr buf = std::unique_ptr(new char[bufSize]); - mg_read(conn, buf.get(), bufSize); - const std::string postData(buf.get(), bufSize); - std::stringstream postStream(postData); + std::string data = GetRequestContent(conn); + std::stringstream postStream(data); std::string line; std::vector commands; while (std::getline(postStream, line, '\n')) { - GameCommand cmd; const std::size_t splitPos = line.find(";"); - if (splitPos != std::string::npos) - { - cmd.playerID = std::stoi(line.substr(0, splitPos)); - cmd.json_cmd = line.substr(splitPos + 1); - commands.push_back(cmd); - } + if (splitPos == std::string::npos) + continue; + + GameCommand cmd; + cmd.playerID = std::stoi(line.substr(0, splitPos)); + cmd.json_cmd = line.substr(splitPos + 1); + commands.push_back(std::move(cmd)); } const std::string gameState = interface->Step(std::move(commands)); if (gameState.empty()) @@ -207,17 +194,13 @@ mg_printf(conn, "%s", notRunningResponse); return handled; } - const char* val = mg_get_header(conn, "Content-Length"); - if (!val) + const std::string data = GetRequestContent(conn); + if (data.empty()) { mg_printf(conn, "%s", noPostData); return handled; } - const int bufSize = std::atoi(val); - std::unique_ptr buf = std::unique_ptr(new char[bufSize]); - mg_read(conn, buf.get(), bufSize); - const std::string postData(buf.get(), bufSize); - std::stringstream postStream(postData); + std::stringstream postStream(data); std::string line; std::vector templateNames; while (std::getline(postStream, line, '\n')) @@ -254,7 +237,22 @@ debug_warn(L"Invalid Mongoose event type"); return nullptr; } -}; +} + +std::string Interface::GetRequestContent(struct mg_connection *conn) +{ + const static std::string NO_CONTENT; + const char* val = mg_get_header(conn, "Content-Length"); + if (!val) + { + return NO_CONTENT; + } + const int contentSize = std::atoi(val); + + std::string content(contentSize, ' '); + mg_read(conn, content.data(), contentSize); + return content; +} bool Interface::TryGetGameMessage(GameMessage& msg) {