From 81b837eceb9bb9a889615a378c29e7b19a42c752 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:25:20 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20w?= =?UTF-8?q?ip:=20new=20config=20should=20be=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 36 +++++++++++++++++++ includes/config/Server.hpp | 16 ++------- includes/config/default.hpp | 3 +- includes/log.hpp | 22 ++++++------ src/config/Config.cpp | 45 ++++++++++++++++++++++++ src/config/Server.cpp | 69 +++++-------------------------------- 6 files changed, 104 insertions(+), 87 deletions(-) create mode 100644 includes/config/Config.hpp create mode 100644 src/config/Config.cpp diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp new file mode 100644 index 0000000..a7b2b60 --- /dev/null +++ b/includes/config/Config.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Config.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ +/* Updated: 2025/04/14 12:55:46 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "config/default.hpp" + +namespace webserv { +namespace config { + +class Config { + public: + Config(std::string &); + ~Config(); + + Logger *getLogger(void) { return _log; } + + std::vector *getServers(void) { return _servers; } + + private: + toml::ANode *_table; + Logger *_log; + std::vector *_servers; +}; + +}; // namespace config +}; // namespace webserv diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 34c3964..41cf1ee 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/25 17:56:34 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:39:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ namespace config { class Server { public: - Server(std::string); + Server(toml::ANode *, Logger *); ~Server(); /** @@ -48,11 +48,6 @@ class Server { return not_nullptr; } - /** - * @brief Can be used to get the Logger pointer - */ - Logger *getLogger(void) { return _log; } - // @brief Can be used to get a server name std::vector *getServerNames(void) { return _server_names; } // @brief Can be used to get the host specified in the config file @@ -79,13 +74,6 @@ class Server { std::map * _parseErrPages(std::map *table); - - /** - * @brief Can be used to get the [server] table in _table - * - * @return A pointer to the [server] table as an ANode - */ - toml::ANode *_getServerTable(void); }; } // namespace config diff --git a/includes/config/default.hpp b/includes/config/default.hpp index 5ff94dc..7713797 100644 --- a/includes/config/default.hpp +++ b/includes/config/default.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ -/* Updated: 2025/03/26 08:39:08 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:54:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "Route.hpp" #include "Server.hpp" +#include "Config.hpp" #include "cppeleven.hpp" #include "node/Table.hpp" #include "node/default.hpp" diff --git a/includes/log.hpp b/includes/log.hpp index c678cb3..c973c46 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ -/* Updated: 2025/04/11 15:49:38 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:03:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,7 @@ namespace webserv { * * @note Only work if VERBOSE mode is active */ -static inline void log(std::string emoji, std::string who, - std::string str) { +static inline void log(std::string emoji, std::string who, std::string str) { #ifdef VERBOSE if (who.empty()) std::cout << "「" << emoji << "」debug: " << str << std::endl; @@ -40,18 +39,17 @@ static inline void log(std::string emoji, std::string who, class Logger { public: + Logger(void) : _ttyOnly(true) { + log("➕", "Logger", "default constructor called"); + } Logger(const std::string &fileName) : _fileName(fileName) { log("➕", "Logger", "filename constructor called"); - if (fileName.empty()) { + _file.open(fileName.c_str(), std::ios::app); + if (!_file.is_open() && !_ttyOnly) { _ttyOnly = true; - } else { - _file.open(fileName.c_str(), std::ios::app); - if (!_file.is_open() && !_ttyOnly) { - _ttyOnly = true; - warn("could not open log file, going tty only"); - } else - _ttyOnly = false; - } + warn("could not open log file, going tty only"); + } else + _ttyOnly = false; } Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { diff --git a/src/config/Config.cpp b/src/config/Config.cpp new file mode 100644 index 0000000..650d5b3 --- /dev/null +++ b/src/config/Config.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Config.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ +/* Updated: 2025/04/14 13:25:09 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cppeleven.hpp" +#include "node/ANode.hpp" +#include +#include + +using namespace webserv::config; + +Config::Config(std::string &filename) { + toml::Toml file(filename); + + try { + file.parse(); + } catch (std::exception &e) { + throw e; + } + + _table = file.getParsedFile(); + + bool found = false; + void *logFile = _table->access("log_file", toml::STRING, found); + if (found == true && logFile != not_nullptr) { + _log = new Logger(*static_cast(logFile)); + } else { + _log = new Logger(); + } + + std::map *node = _table->getTable(); + for (std::map::iterator it = node->begin(); + it != node->end(); it++) { + Server *srv = new Server(it->second, _log); + _servers->push_back(srv); + } +} diff --git a/src/config/Server.cpp b/src/config/Server.cpp index a26bd06..278ebd2 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,84 +6,33 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/11 15:44:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:57:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "config/Server.hpp" -#include "config/Route.hpp" -#include "cppeleven.hpp" -#include "log.hpp" -#include "node/ANode.hpp" -#include "node/Table.hpp" -#include "node/default.hpp" -#include "tomlpp.hpp" #include -#include -#include -#include -#include using namespace webserv::config; -toml::ANode *Server::_getServerTable(void) { - toml::ANode *serverT; - - std::map::iterator table = - _table->getTable()->find("server"); - if (table == _table->getTable()->end()) - throw std::runtime_error( - "could not find any [server] table in config file :("); - else - serverT = table->second; - return serverT; -} - -Server::Server(std::string file_name) { - toml::Toml *tomlFile = new toml::Toml(file_name); - - try { - tomlFile->parse(); - } catch (std::runtime_error &e) { - throw e; - } +Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { bool found; - std::map *map; - _table = tomlFile->getParsedFile(); - - try { - _table = _getServerTable(); - } catch(std::runtime_error &e) { - delete _table; - delete tomlFile; - delete _log; - throw e; - } - void *val = _table->access("log_file", toml::STRING, found); - std::string log_file = ""; - if (found == true && val != not_nullptr) { - log_file = *static_cast(val); - } - _log = new Logger(log_file); - - // host and port parsing + // host parsing void *host = accessValue("host", toml::STRING, _table, _log); if (host != not_nullptr) { _host = *static_cast(host); } else { delete _table; - delete tomlFile; delete _log; throw std::runtime_error( "no host specified - please specify one in server.host"); } + // port parsing void *port = accessValue("port", toml::INT, _table, _log); if (host != not_nullptr) { _port = *static_cast(port); } else { delete _table; - delete tomlFile; delete _log; throw std::runtime_error( "no port specified - please specify one in server.port"); @@ -102,13 +51,14 @@ Server::Server(std::string file_name) { } } else { _log->warn( - "no server_names all request will be accepted from any hostname"); + "no server_names all request will be accepted from any hostname"); _server_names = not_nullptr; } // error_pages parsing - map = static_cast *>( - accessValue("error_pages", toml::TABLE, _table, _log)); + std::map *map = + static_cast *>( + accessValue("error_pages", toml::TABLE, _table, _log)); if (map != not_nullptr) { _err_pages = _parseErrPages(map); } else @@ -126,8 +76,7 @@ Server::Server(std::string file_name) { (*_routes)[it->first] = new Route(it->second, _log); } } - delete tomlFile->getParsedFile(); - delete tomlFile; + delete _table; } Server::~Server(void) { From 5595a404d0eb9e8a9972ff9dfbfe56809491872e Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:33:07 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20w?= =?UTF-8?q?ip:=20added=20destructor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/Config.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 650d5b3..277313f 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,27 +6,19 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:25:09 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:32:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cppeleven.hpp" -#include "node/ANode.hpp" #include -#include using namespace webserv::config; Config::Config(std::string &filename) { - toml::Toml file(filename); + toml::Toml *file = new toml::Toml(filename); - try { - file.parse(); - } catch (std::exception &e) { - throw e; - } - - _table = file.getParsedFile(); + file->parse(); + _table = file->getParsedFile(); bool found = false; void *logFile = _table->access("log_file", toml::STRING, found); @@ -42,4 +34,14 @@ Config::Config(std::string &filename) { Server *srv = new Server(it->second, _log); _servers->push_back(srv); } + delete _table; + delete file; +} + +Config::~Config(void) { + std::vector::iterator it = _servers->begin(); + for (; it != _servers->end() ; it++) { + delete *it; + } + delete _servers; } From bff58bad9225f7784e43fc72e5a73495ede42f1b Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:37:25 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20rem?= =?UTF-8?q?ove=20useless=20=5Ftable=20in=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 3 +-- src/config/Config.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index a7b2b60..b1bbe47 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/14 12:55:46 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:36:42 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,6 @@ class Config { std::vector *getServers(void) { return _servers; } private: - toml::ANode *_table; Logger *_log; std::vector *_servers; }; diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 277313f..35eaec6 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:32:56 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:36:46 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "node/ANode.hpp" #include using namespace webserv::config; @@ -18,23 +19,23 @@ Config::Config(std::string &filename) { toml::Toml *file = new toml::Toml(filename); file->parse(); - _table = file->getParsedFile(); + toml::ANode *table = file->getParsedFile(); bool found = false; - void *logFile = _table->access("log_file", toml::STRING, found); + void *logFile = table->access("log_file", toml::STRING, found); if (found == true && logFile != not_nullptr) { _log = new Logger(*static_cast(logFile)); } else { _log = new Logger(); } - std::map *node = _table->getTable(); + std::map *node = table->getTable(); for (std::map::iterator it = node->begin(); it != node->end(); it++) { Server *srv = new Server(it->second, _log); _servers->push_back(srv); } - delete _table; + delete table; delete file; }