diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 6f3fff3..8edddc4 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/03/19 17:35:09 by adjoly ### ########.fr */ +/* Updated: 2025/03/20 09:27:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,10 @@ #include "cppeleven.hpp" #include "node/ANode.hpp" +#include "node/Array.hpp" +#include +#include +#include #include #include #include @@ -23,29 +27,148 @@ namespace webserv { namespace config { +int32_t parseSize(std::string size) { + if (size[size.size()] == 'M') + return std::atoi(size.c_str()) * 1024 * 1024; + if (size[size.size()] == 'K') + return std::atoi(size.c_str()) * 1024; + if (isalpha(size[size.size()])) + return std::atoi(size.c_str()); + return -1; +} + class Route { public: Route(std::map *node) { if (node == not_nullptr) throw std::runtime_error("location table does not exist"); - std::map *errorPagesTable = (*node)["error_pages"]->getTable(); - if (errorPagesTable == not_nullptr) - throw std::runtime_error("error_pages not present"); + + _methods[0] = false; + _methods[1] = false; + _methods[2] = false; + + std::map::iterator it; + + it = (*node).find("redirect"); + if (it != node->end()) { + toml::ANode *redirNode = it->second; + if (redirNode->type() == toml::STRING) { + _redirect = true; + _root = *(std::string *)redirNode->getValue(); + _cookies = false; + _uploads = false; + _dirlist = false; + _cgi = not_nullptr; + _methods[0] = false; + _methods[1] = false; + _methods[2] = false; + _err_pages = not_nullptr; + _upRoot = not_nullptr; + return; + } + } + + it = (*node).find("uploads"); + if (it != node->end()) { + toml::ANode *uploadsNode = it->second; + if (uploadsNode->type() == toml::BOOL) + _uploads = *(bool *)uploadsNode->getValue(); + } + it = (*node).find("dirlist"); + if (it != node->end()) { + toml::ANode *dirlistNode = it->second; + if (dirlistNode->type() == toml::BOOL) + _dirlist = *(bool *)dirlistNode->getValue(); + } + it = (*node).find("cookies"); + if (it != node->end()) { + toml::ANode *cookiesNode = it->second; + if (cookiesNode->type() == toml::BOOL) + _cookies = *(bool *)cookiesNode->getValue(); + } + it = (*node).find("root"); + if (it != node->end()) { + toml::ANode *rootNode = it->second; + if (rootNode->type() == toml::STRING) + _root = *(std::string *)rootNode->getValue(); + } + it = (*node).find("client_max_body_size"); + if (it != node->end()) { + toml::ANode *rootNode = it->second; + if (rootNode->type() == toml::STRING) { + int32_t maxBody = + parseSize(*(std::string *)rootNode->getValue()); + if (maxBody != 0 && maxBody != -1) + _max_body = maxBody; + else { + std::cerr << "root size is not correctttt" + << std::endl; /// change that later TODO + _max_body = 10485760; + } + } + } + + it = (*node).find("methods"); + if (it != node->end()) { + toml::ANode *methodsNode = it->second; + if (methodsNode->type() == toml::ARRAY) { + std::vector::iterator methods = + methodsNode->getArray()->begin(); + for (; methods != methodsNode->getArray()->end(); methods++) { + if ((*methods)->type() == toml::STRING) { + std::string method = + *(std::string *)(*methods)->getValue(); + if (method == "GET") + _methods[0] = true; + else if (method == "POST") + _methods[1] = true; + else if (method == "DELETE") + _methods[2] = true; + else + throw std::runtime_error("da fuk that not a valid " + "methods bro"); // replace + // with + // warning + // log + } else { + throw std::runtime_error( + "error why did you put something elle than a " + "string"); // replace with log func + } + } + } + } + + it = (*node).find("error_pages"); + if (it != node->end()) { + toml::ANode *errorPagesNode = it->second; + // for loop on it and add to error pages and atoi the name of the + // page(need to be a deep copy) TODO + } + + it = (*node).find("cgi"); + if (it != node->end()) { + toml::ANode *cgiNode = it->second; + // for loop on it and add to cgi (need to be a deep copy) TODO + } } ~Route(void) {} protected: private: - bool _dirlist; - bool _cookies; - bool _uploads; - bool _redirect; - int32_t _maxBody; + bool _dirlist; + bool _cookies; + bool _uploads; + bool _redirect; + + int32_t _max_body; + std::string _root; std::string _upRoot; std::map *_cgi; - std::vector *_methods; - std::map *_errPages; + + bool _methods[3]; // 1: GET, 2: POST, 3: DELETE + std::map *_err_pages; }; } // namespace config diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index a6f37ff..955354f 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/03/19 16:54:35 by adjoly ### ########.fr */ +/* Updated: 2025/03/19 23:09:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,10 @@ class Server { public: protected: private: - Route *_default; - std::vector *_routes; + Route *_default; + std::vector *_routes; + std::string host; + std::vector server_names; }; } // namespace config diff --git a/includes/log.hpp b/includes/log.hpp new file mode 100644 index 0000000..d863e80 --- /dev/null +++ b/includes/log.hpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* log.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ +/* Updated: 2025/03/20 14:42:26 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace webserv { +class Logger { + public: + Logger(std::string fileName) { + if (fileName.empty()) + _ttyOnly = true; + else { + _file.open(fileName.c_str(), std::ios::app); + _ttyOnly = false; + } + if (!_file.is_open()) { + throw std::runtime_error("could not open fileeee"); // TODO change that shit but i dont know what to put other than a htrow + } + } + + ~Logger(void) { _file.close(); } + + void info(std::string &msg) { + std::stringstream ss = printPogitMsg("✏️", "webserv", "info", msg); + std::cerr << ss << std::endl; + if (!_ttyOnly) { + _file << ss << std::endl; + } + } + void warn(std::string &msg) { + std::stringstream ss = printPogitMsg("🔨", "webserv", "warning", msg); + std::cerr << ss << std::endl; + if (!_ttyOnly) { + _file << ss << std::endl; + } + + } + void error(std::string &msg) { + std::stringstream ss = printPogitMsg("🚧", "webserv", "error", msg); + std::cerr << ss << std::endl; + if (!_ttyOnly) { + _file << ss << std::endl; + } + } + + protected: + private: + std::stringstream printPogitMsg(std::string emoji, std::string type, std::string what, std::string msg) { + std::stringstream os; +#ifdef tty + if (what.empty()) + os << type << ":" << msg; + else + os << type << "(" << what << "):" << msg; +#else + if (what.empty()) + os << "「" << emoji << "」" << type << ":" << msg; + else + os << "「" << emoji << "」" << type << "(" << what << "):" << msg; +#endif + return os; + } + bool _ttyOnly; + std::ofstream _file; +}; + +}; // namespace webserv