diff --git a/CMakeLists.txt b/CMakeLists.txt index b752743..9c22cbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,13 @@ set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address") add_executable( Webserver - "src/Entry.cpp" "src/Webserver.cpp" "src/HTTP.cpp" - "libs/QuickDigest5/quickdigest5.cpp" "src/Helpers.cpp" - "src/HTTPRequestProcess.cpp") + "src/Entry.cpp" + "src/Webserver.cpp" + "src/HTTP.cpp" + "libs/QuickDigest5/quickdigest5.cpp" + "src/Helpers.cpp" + "src/HTTPRequestProcess.cpp" + "./src/HTTPMethods.cpp") add_custom_command( TARGET Webserver diff --git a/src/HTTP.cpp b/src/HTTP.cpp index a19f4cf..88f9958 100644 --- a/src/HTTP.cpp +++ b/src/HTTP.cpp @@ -1,5 +1,6 @@ #include "Helpers.hpp" #include "Main.hpp" +#include void HTTPrequest::start() { // Possible Logging here @@ -7,29 +8,11 @@ void HTTPrequest::start() { } void HTTPrequest::processRequest() { - uint64_t pathHash = Helpers::Pathhash(requestPath); - - // This is very much temp - if (requestType == "POST") { - switch (pathHash) { - case "/upload"_hash: - writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text", - bodyContent)); - return; - } - return; - } - - // This can be further refactored to just "File send" - switch (pathHash) { - case "/"_hash: - writeData(Helpers::GenerateResponse("200 OK", "text/html", - Helpers::ReadFile("www/index.html"))); - return; - default: + try { + Webserver::responseMethods[requestType].at(requestPath)(*this); + } catch (std::exception e) { writeData(Helpers::GenerateResponse("404 Not Found", "text/html", - "Could not find that file!!")); - return; + Helpers::ReadFile("www/error.html"))); } } diff --git a/src/Helpers.hpp b/src/Helpers.hpp index f1a44cf..a26b817 100644 --- a/src/Helpers.hpp +++ b/src/Helpers.hpp @@ -1,9 +1,5 @@ -// Big thanks to -// https://medium.com/@ryan_forrester_/using-switch-statements-with-strings-in-c-a-complete-guide-efa12f64a59d -#include #include #include -#include namespace Helpers { std::string ReadFile(std::string Path); @@ -11,18 +7,4 @@ void getlineAndCount(std::basic_istream &stream, uint64_t &count, std::string &string, char delimit = '\0'); std::string GenerateResponse(std::string statusCode, std::string contentType, std::string content); -// =========== -// Hashing -// =========== -// This is called a polynomial rolling hash, prob going to collide -constexpr uint64_t Pathhash(std::string_view s) { - uint64_t res = 0; - for (uint8_t c : s) { - res = (res * 131) + c; - } - return res; -} } // namespace Helpers -constexpr uint64_t operator""_hash(const char *string, std::size_t count) { - return Helpers::Pathhash(std::string_view(string)); -} diff --git a/src/Main.hpp b/src/Main.hpp index 197d0ee..be4db98 100644 --- a/src/Main.hpp +++ b/src/Main.hpp @@ -57,15 +57,16 @@ class Webserver : public std::enable_shared_from_this { public: Webserver(asio::io_context &context); -private: - void begin(); - static void initResponses(); - // Responses static std::unordered_map< std::string, std::unordered_map>> responseMethods; + +private: + void begin(); + static void initResponses(); + asio::io_context &io; asio::ip::tcp::acceptor accept; }; diff --git a/src/Webserver.cpp b/src/Webserver.cpp index d6cafc8..5c1ea5e 100644 --- a/src/Webserver.cpp +++ b/src/Webserver.cpp @@ -1,5 +1,6 @@ #include "Main.hpp" #include +#include // Webserver is defined at main.hpp, you probably do not want to code here Webserver::Webserver(asio::io_context &context) @@ -8,7 +9,7 @@ Webserver::Webserver(asio::io_context &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!"; + std::cout << "Path responses are set up!" << std::endl; begin(); } diff --git a/www/error.html b/www/error.html new file mode 100644 index 0000000..df20e65 --- /dev/null +++ b/www/error.html @@ -0,0 +1,15 @@ + + + + + + + + + + +

404 NOT FOUND

+

We could not find the requested page! Sorry ~_~

+ + +