/* ::: :::::::: */ /* Route.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/21 20:37:02 by adjoly #+# #+# */ /* Updated: 2025/03/26 08:19:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "cppeleven.hpp" #include "log.hpp" #include "node/default.hpp" #include #include #include using namespace webserv::config; std::map *Route::_parseCGI(toml::ANode *table) { std::map *cgi = new std::map; void *val; for (std::map::iterator it = table->getTable()->begin(); it != table->getTable()->end(); it++) { val = accessValue(it->first, toml::STRING, table, _log); if (val != not_nullptr) { if (cgi->find(it->first) != cgi->end()) continue; else (*cgi)[it->first] = *static_cast(val); } } return cgi; } void Route::_parseMethods(std::vector *table) { std::string val; _methods[0] = false; _methods[1] = false; _methods[2] = false; for (std::vector::iterator it = table->begin(); it != table->end(); it++) { if ((*it)->type() == toml::STRING) { val = *static_cast((*it)->getValue()); if (val == "GET") _methods[0] = true; if (val == "POST") _methods[1] = true; if (val == "DELETE") _methods[2] = true; } } } Route::Route(toml::ANode *table, Logger *logger) : _max_body(10485760), _log(logger) { void *val; bool found; _log = logger; _table = table; if (_table->type() != toml::TABLE) { _log->warn("location need to be a table and not a :" + toml::nodeTypeToStr(_table->type())); return; } val = accessValue("redirect", toml::STRING, _table, _log); if (val != not_nullptr) { _root = *static_cast(val); _redirect = true; return; } else _redirect = false; val = accessValue("dirlist", toml::BOOL, _table, _log); if (val != not_nullptr) _dirlist = *static_cast(val); else _dirlist = true; val = accessValue("cookies", toml::BOOL, _table, _log); if (val != not_nullptr) _cookies = *static_cast(val); else _cookies = false; val = accessValue("upload_path", toml::STRING, _table, _log); if (val != not_nullptr) _up_root = *static_cast(val); else _up_root = ""; val = accessValue("index", toml::STRING, _table, _log); if (val != not_nullptr) _index = *static_cast(val); else _index = "index.html"; val = accessValue("root", toml::STRING, _table, _log); if (val != not_nullptr) _root = *static_cast(val); else #ifdef PKGS _root = "/var/www/html" #else _root = "./html"; #endif val = accessValue("client_max_body_size", toml::STRING, _table, _log); if (val != not_nullptr) _max_body = _parseSize(*static_cast(val)); std::map::iterator it = _table->accessIt("cgi", toml::TABLE, found); if (found == true && it != _table->getTable()->end()) _cgi = _parseCGI(it->second); else _cgi = not_nullptr; val = accessValue("methods", toml::ARRAY, _table, _log); if (val != not_nullptr) _parseMethods(static_cast *>(val)); else { _methods[0] = true; _methods[1] = false; _methods[2] = false; } } Route::~Route(void) { if (_redirect == false) if (_cgi != not_nullptr) delete _cgi; }