forked from cat/WebBase
resharping writing data
This commit is contained in:
11
src/HTTP.cpp
11
src/HTTP.cpp
@@ -1,6 +1,7 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
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 =================
|
||||
|
@@ -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);
|
||||
};
|
||||
}
|
||||
|
@@ -31,8 +31,9 @@ void HTTPrequest::processHTTPHeader() {
|
||||
processHeaderValues(stream, octetCount, packageSize);
|
||||
processBody();
|
||||
|
||||
processRequest(); // This writes data too
|
||||
sock.close();
|
||||
// RESPOND
|
||||
processRequest();
|
||||
sock.close(); // end
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user