Template
1
0

resharping writing data

This commit is contained in:
2025-07-04 21:51:54 +03:00
parent 0d87542196
commit cc3eb54b3b
4 changed files with 16 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
#include "Helpers.hpp" #include "Helpers.hpp"
#include "Main.hpp" #include "Main.hpp"
#include <exception> #include <exception>
#include <string>
void HTTPrequest::start() { void HTTPrequest::start() {
// Possible Logging here // Possible Logging here
@@ -11,15 +12,15 @@ void HTTPrequest::processRequest() {
try { try {
Webserver::responseMethods[requestType].at(requestPath)(*this); Webserver::responseMethods[requestType].at(requestPath)(*this);
} catch (std::exception e) { } catch (std::exception e) {
writeData(Helpers::GenerateResponse("404 Not Found", "text/html", sendResponse("404 Not Found", "text/html",
Helpers::ReadFile("www/error.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 // Logging here perhaps
asio::async_write(sock, asio::buffer(data), asio::write(sock, asio::buffer(data));
[](std::error_code, std::size_t) {});
} }
// ================= CLASS HANDLING SPECIFIC ================= // ================= CLASS HANDLING SPECIFIC =================

View File

@@ -13,7 +13,11 @@ std::unordered_map<
void Webserver::initResponses() { void Webserver::initResponses() {
responseMethods["GET"]["/"] = [](HTTPrequest &self) { responseMethods["GET"]["/"] = [](HTTPrequest &self) {
self.writeData(Helpers::GenerateResponse( self.sendResponse("200 OK", "text/html",
"200 OK", "text/html", Helpers::ReadFile("www/index.html"))); Helpers::ReadFile("www/index.html"));
};
responseMethods["POST"]["/upload"] = [](HTTPrequest &self) {
self.sendResponse("200 OK", "text/text", self.bodyContent);
}; };
} }

View File

@@ -31,8 +31,9 @@ void HTTPrequest::processHTTPHeader() {
processHeaderValues(stream, octetCount, packageSize); processHeaderValues(stream, octetCount, packageSize);
processBody(); processBody();
processRequest(); // This writes data too // RESPOND
sock.close(); processRequest();
sock.close(); // end
} }
}); });
} }

View File

@@ -28,7 +28,7 @@ public:
static HTTPrequest_ptr create(asio::io_context &context); static HTTPrequest_ptr create(asio::io_context &context);
void start(); void start();
void writeData(std::string data); void sendResponse(std::string status, std::string mime, std::string data);
// Request itself // Request itself
std::string requestType, requestPath; std::string requestType, requestPath;