diff --git a/src/HTTP.cpp b/src/HTTP.cpp index 4f8327f..a19f4cf 100644 --- a/src/HTTP.cpp +++ b/src/HTTP.cpp @@ -14,7 +14,7 @@ void HTTPrequest::processRequest() { switch (pathHash) { case "/upload"_hash: writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text", - "This path is not implemented yet!")); + bodyContent)); return; } return; diff --git a/src/HTTPMethods.cpp b/src/HTTPMethods.cpp new file mode 100644 index 0000000..1db66b3 --- /dev/null +++ b/src/HTTPMethods.cpp @@ -0,0 +1,19 @@ +#include "Helpers.hpp" +#include "Main.hpp" + +// Idea is very simple this map has it so that stuff like +// responseMethods["GET","/"] have a method inside them +// There is an invoking method inside each HTTPrequest +// +// So just add your logic here and relax ;) +std::unordered_map< + std::string, + std::unordered_map>> + Webserver::responseMethods; + +void Webserver::initResponses() { + responseMethods["GET"]["/"] = [](HTTPrequest &self) { + self.writeData(Helpers::GenerateResponse( + "200 OK", "text/html", Helpers::ReadFile("www/index.html"))); + }; +} diff --git a/src/Main.hpp b/src/Main.hpp index ec30f0f..197d0ee 100644 --- a/src/Main.hpp +++ b/src/Main.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -27,11 +28,16 @@ public: static HTTPrequest_ptr create(asio::io_context &context); void start(); + void writeData(std::string data); + + // Request itself + std::string requestType, requestPath; + std::unordered_map headers, args; + std::string bodyContent; private: HTTPrequest(asio::io_context &context); void processRequest(); - void writeData(std::string data); // Breaking Header to in lines void processHTTPHeader(); @@ -41,11 +47,6 @@ private: std::size_t headerSize); void inline processBody(); - // Request itself - std::string requestType, requestPath; - std::unordered_map headers, args; - std::string bodyContent; - // Networking asio::ip::tcp::socket sock; asio::streambuf buffer; @@ -58,6 +59,13 @@ public: private: void begin(); + static void initResponses(); + + // Responses + static std::unordered_map< + std::string, + std::unordered_map>> + responseMethods; asio::io_context &io; asio::ip::tcp::acceptor accept; }; diff --git a/src/Webserver.cpp b/src/Webserver.cpp index 906b895..d6cafc8 100644 --- a/src/Webserver.cpp +++ b/src/Webserver.cpp @@ -7,6 +7,8 @@ Webserver::Webserver(asio::io_context &context) accept(context, asio::ip::tcp::endpoint(asio::ip::make_address(IP), PORT)) { std::cout << "Server is up!\n"; + initResponses(); + std::cout << "Path responses are set up!"; begin(); }