From 0dcc562b7dcf097a2ae80eb001199e982dc30087 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Thu, 14 Nov 2024 11:09:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=88=D0=B8=D0=B2=D0=BA=D0=B8=20=D0=B8=D0=B7?= =?UTF-8?q?=20=D0=B2=D0=B5=D0=B1=20=D0=BC=D0=BE=D1=80=D0=B4=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/main.cpp | 28 +++++++++++++++ src/server/request_parser.cpp | 13 ++++--- static/main.html | 65 +++++++++++++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 9e54ecc..e74b518 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ cmake-build-* cert.pem key.pem dh.pem +/do-terminal-update.sh diff --git a/src/main.cpp b/src/main.cpp index 4184187..bed9e08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,6 +80,19 @@ class ServerResources { std::unique_ptr api; http::auth::AuthProvider auth{}; + void doTerminaFwUpdate(const http::server::Request& req) { + std::ofstream f("/tmp/firmware.zip", std::ios::binary); + + if (f.is_open()) { + f.write(req.payload.data(), static_cast(req.payload.size())); + f.close(); + + system("do-terminal-update.sh"); + } else { + throw std::runtime_error("File is not open"); + } + } + public: static constexpr const char* INDEX_HTML = "static/main.html"; static constexpr const char* LOGIN_HTML = "static/login.html"; @@ -322,6 +335,21 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); + + s.resources.emplace_back(std::make_unique("/api/firmwareUpdate", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { + if (req.method != "PUT") { + http::server::stockReply(http::server::bad_request, rep); + } + this->doTerminaFwUpdate(req); + + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + std::string result = R"({"status":"ok","fwsize":)"; + result += std::to_string(req.payload.size()); + result += "}"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + })); } ~ServerResources() = default; diff --git a/src/server/request_parser.cpp b/src/server/request_parser.cpp index c4643f6..a6b5ff7 100644 --- a/src/server/request_parser.cpp +++ b/src/server/request_parser.cpp @@ -1,6 +1,5 @@ #include "request_parser.hpp" -#include #include #include "request.hpp" @@ -14,16 +13,16 @@ namespace http::server { */ static bool requestBodySizeResolver(Request& req, size_t reqSize) { // разрешаем тело только для POST запросов - if (req.method != "POST") { - return false; + if (req.method == "POST") { + return reqSize < 0x4000; // 16кб на все POST-запросы к API будет более чем достаточно } - // для обновления прошивки разрешаем большое тело - if (req.url->path == "/api/firmwareUpdate") { + // это для обновления прошивки + if (req.method == "PUT" && req.url->path == "/api/firmwareUpdate") { return reqSize <= HTTP_MAX_PAYLOAD; } - return reqSize < 0x4000; // 16кб на все POST-запросы к API будет более чем достаточно + return false; } static void parseParams(Url& u, const std::string& query) { @@ -277,7 +276,7 @@ namespace http::server { if (content_len.empty()) { return good; } - contentLenghtHeader = std::stol(content_len); + contentLenghtHeader = std::stoul(content_len); if (contentLenghtHeader == 0) { return good; } diff --git a/static/main.html b/static/main.html index be581c4..9287c6b 100644 --- a/static/main.html +++ b/static/main.html @@ -617,10 +617,10 @@

Обновление ПО

- +