Template
1
0

Added processFormData and reorganised the files

This commit is contained in:
2025-07-07 03:11:41 +03:00
parent 4962bb5a2d
commit 481b4b410a
10 changed files with 114 additions and 15 deletions

58
src/HTTP/HTTP.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include "../Helpers.hpp"
#include "../Main.hpp"
#include <stdexcept>
void HTTPrequest::start() {
// Possible Logging here
processHTTPHeader();
}
void HTTPrequest::processRequest() {
try {
Webserver::responseMethods[requestType].at(requestPath)(this);
} catch (std::out_of_range &e) {
sendResponse("404 Not Found", "text/html",
Helpers::ReadFile("www/error.html"));
}
}
void HTTPrequest::sendResponse(std::string status, std::string mime,
std::string data) {
// Logging here perhaps
std::stringstream output;
output << "HTTP/1.1 " << status << "\r\n"
<< "Content-Type: " << mime << "\r\nContent-Length: " << data.size()
<< "\r\nConnection: close\r\n\r\n"
<< data;
// Idea here is that in TCP oncee you sent a shutdown you should read until
// EOF. When that happens, you know that the package is fully recieved
responseText = output.str();
asio::async_write(
sock, asio::buffer(responseText),
[this, self = shared_from_this()](std::error_code, std::size_t) {
sock.shutdown(asio::ip::tcp::socket::shutdown_send);
waitForClientClose();
});
}
// I'm not happy with this
void HTTPrequest::waitForClientClose() {
auto buf = std::array<char, 1>(); // I don't think there is a smaller possible
// constructor :(
sock.async_read_some(asio::buffer(buf),
[this, self = shared_from_this()](
const std::error_code &error, std::size_t) {
if (error) {
sock.close(); // This triggers when EOF
}
waitForClientClose(); // Ew recursion. Must fix.
});
}
// ================= CLASS HANDLING SPECIFIC =================
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
asio::ip::tcp::socket &HTTPrequest::socket() { return sock; }
HTTPrequest::HTTPrequest_ptr HTTPrequest::create(asio::io_context &context) {
return HTTPrequest_ptr(new HTTPrequest(context));
}