From cc3eb54b3b622942c491095e4fc5e6f1525f515a Mon Sep 17 00:00:00 2001 From: cat Date: Fri, 4 Jul 2025 21:51:54 +0300 Subject: [PATCH] resharping writing data --- src/HTTP.cpp | 11 ++++++----- src/HTTPMethods.cpp | 8 ++++++-- src/HTTPRequestProcess.cpp | 5 +++-- src/Main.hpp | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/HTTP.cpp b/src/HTTP.cpp index 88f9958..f4875d4 100644 --- a/src/HTTP.cpp +++ b/src/HTTP.cpp @@ -1,6 +1,7 @@ #include "Helpers.hpp" #include "Main.hpp" #include +#include void HTTPrequest::start() { // Possible Logging here @@ -11,15 +12,15 @@ void HTTPrequest::processRequest() { try { Webserver::responseMethods[requestType].at(requestPath)(*this); } catch (std::exception e) { - writeData(Helpers::GenerateResponse("404 Not Found", "text/html", - Helpers::ReadFile("www/error.html"))); + sendResponse("404 Not Found", "text/html", + Helpers::ReadFile("www/error.html")); } } -void HTTPrequest::writeData(std::string data) { +void HTTPrequest::sendResponse(std::string status, std::string mime, + std::string data) { // Logging here perhaps - asio::async_write(sock, asio::buffer(data), - [](std::error_code, std::size_t) {}); + asio::write(sock, asio::buffer(data)); } // ================= CLASS HANDLING SPECIFIC ================= diff --git a/src/HTTPMethods.cpp b/src/HTTPMethods.cpp index 1db66b3..59bcd87 100644 --- a/src/HTTPMethods.cpp +++ b/src/HTTPMethods.cpp @@ -13,7 +13,11 @@ std::unordered_map< void Webserver::initResponses() { responseMethods["GET"]["/"] = [](HTTPrequest &self) { - self.writeData(Helpers::GenerateResponse( - "200 OK", "text/html", Helpers::ReadFile("www/index.html"))); + self.sendResponse("200 OK", "text/html", + Helpers::ReadFile("www/index.html")); + }; + + responseMethods["POST"]["/upload"] = [](HTTPrequest &self) { + self.sendResponse("200 OK", "text/text", self.bodyContent); }; } diff --git a/src/HTTPRequestProcess.cpp b/src/HTTPRequestProcess.cpp index 648fed1..76aa65d 100644 --- a/src/HTTPRequestProcess.cpp +++ b/src/HTTPRequestProcess.cpp @@ -31,8 +31,9 @@ void HTTPrequest::processHTTPHeader() { processHeaderValues(stream, octetCount, packageSize); processBody(); - processRequest(); // This writes data too - sock.close(); + // RESPOND + processRequest(); + sock.close(); // end } }); } diff --git a/src/Main.hpp b/src/Main.hpp index be4db98..3278b59 100644 --- a/src/Main.hpp +++ b/src/Main.hpp @@ -28,7 +28,7 @@ public: static HTTPrequest_ptr create(asio::io_context &context); void start(); - void writeData(std::string data); + void sendResponse(std::string status, std::string mime, std::string data); // Request itself std::string requestType, requestPath;