From 9a28e15af006121a99e20068f592ea4045b2a0ab Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 1 May 2025 16:32:44 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20error=20with=20config=20and=20handled=20properly=20exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 5 ++- includes/config/Config.hpp | 17 +++++----- includes/config/Server.hpp | 3 +- includes/server/Client.hpp | 10 +++--- src/config/Config.cpp | 24 ++++++++++++--- src/config/Server.cpp | 63 ++++++++++++++++++++++++++++++++++---- src/main.cpp | 11 +++++-- src/server/Client.cpp | 4 +-- 8 files changed, 105 insertions(+), 32 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index 4dd9773..22c87b2 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -25,8 +25,7 @@ cgi.go = "/bin/go" [server.location./redir] redirect = "https://kanel.ovh" -[serverr] -server_names = { "ptnnnn.local"} +[default] host = "127.0.0.1" port = 9090 @@ -37,5 +36,5 @@ port = 9090 methods = { "GET", "DELETE" } root = "/var/lib/docker/volumes/test_data/_data" dirlist = false -cgi.py = "/bin/python +cgi.py = "/bin/python" diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index 10d5a2f..7ee6421 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/05/01 12:48:12 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 15:29:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,18 +28,21 @@ class Config { Logger *getLogger(void) { return _log; } + Server *getDefaultServer(void) const { return _default; } + std::vector getServers(void) { return _servers; } Server *getServer(size_t i) { - try { - Server *srv = _servers.at(i); - return srv; - } catch (std::out_of_range &e) { - return not_nullptr; - } + try { + Server *srv = _servers.at(i); + return srv; + } catch (std::out_of_range &e) { + return not_nullptr; + } } private: std::vector _servers; + Server *_default; }; }; // namespace config diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 747d0e9..b1aec94 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/04/30 17:10:53 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 15:32:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ namespace config { class Server { public: Server(toml::ANode *); + Server(toml::ANode *, void *); ~Server(); /** diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index f4e6ac3..ca56cda 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/05/01 13:22:48 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 15:25:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ class Client { void _getRequest(std::string); std::string _sanitizeStr(std::string &str) { - std::string newStr = str; + std::string newStr = str; if (str[str.size() - 1] == '\r') { newStr.erase(str.size() - 1); @@ -53,9 +53,9 @@ class Client { return newStr; } - struct pollfd *_pfd; - http::ARequest *_request; - http::Response _response; + struct pollfd *_pfd; + http::ARequest *_request; + http::Response _response; config::Server *_conf; config::Route *_route; }; diff --git a/src/config/Config.cpp b/src/config/Config.cpp index e856321..bb1d1ae 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/05/01 11:21:47 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 16:31:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "log.hpp" #include "node/default.hpp" #include "webserv.hpp" #include "cppeleven.hpp" @@ -22,7 +23,7 @@ using namespace webserv::config; Config::Config(std::string &filename) { toml::Toml *file = new toml::Toml(filename); - + try { file->parse(); } catch (std::runtime_error &e) { @@ -44,11 +45,24 @@ Config::Config(std::string &filename) { if (it->second->type() == toml::TABLE) { _log->info("taking server from table : " + it->first); try { - Server *srv = new Server(it->second); - _servers.push_back(srv); + if (it->first == "default") { + _default = new Server(it->second, not_nullptr); + } else { + Server *srv = new Server(it->second); + _servers.push_back(srv); + } } catch (std::runtime_error &e) { _log->error(e.what()); - break; + if (!_servers.empty()) { + for (auto it = range(_servers)) + delete (*it); + } + if (_default != not_nullptr) { + delete _default; + } + delete table; + delete file; + throw e; } } } diff --git a/src/config/Server.cpp b/src/config/Server.cpp index f76905a..0171082 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/05/01 10:08:15 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 16:30:28 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ using namespace webserv::config; -Server::Server(toml::ANode *node) : _table(node) { +Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { bool found; if (_table == not_nullptr) @@ -31,7 +31,7 @@ Server::Server(toml::ANode *node) : _table(node) { if (host != not_nullptr) { _host = *static_cast(host); } else { - delete _table; + //delete _table; throw std::runtime_error( "no host specified - please specify one in server.host"); } @@ -88,10 +88,11 @@ Server::Server(toml::ANode *node) : _table(node) { } Server::~Server(void) { - for (auto it = prange(_routes)) { - delete it->second; + if (_routes != not_nullptr && !_routes->empty()) { + for (auto it = prange(_routes)) + delete it->second; + delete _routes; } - delete _routes; delete _err_pages; if (_server_names != not_nullptr) delete _server_names; @@ -138,3 +139,53 @@ Route *Server::whatRoute(const URL &url) { } return not_nullptr; } + +Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { + bool found; + + if (_table == not_nullptr) + return; + + // host parsing + void *host = accessValue("host", toml::STRING, _table, _log); + if (host != not_nullptr) { + _host = *static_cast(host); + } else { + delete _table; + throw std::runtime_error( + "no host specified - please specify one"); + } + // port parsing + void *port = accessValue("port", toml::INT, _table, _log); + if (port != not_nullptr) { + _port = *static_cast(port); + } else { + delete _table; + throw std::runtime_error( + "no port specified - please specify one"); + } + + // error_pages parsing + std::map *map = + static_cast *>( + accessValue("error_pages", toml::TABLE, _table, _log)); + if (map != not_nullptr) { + _err_pages = _parseErrPages(map); + } else + _err_pages = not_nullptr; + + // location parsing + auto it = _table->accessIt("location", toml::TABLE, found); + if (found == true && it != _table->getTable()->end()) { + _routes = new std::map; + std::map *location_table = + it->second->getTable(); + for (it = location_table->begin(); it != location_table->end(); it++) { + if (_routes->find(URL(it->first)) != _routes->end()) { + continue; + } + _routes->insert(std::make_pair(URL(it->first), new Route(it->second))); + } + } + // delete _table; +} diff --git a/src/main.cpp b/src/main.cpp index 838f211..fe3cd6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,10 +6,11 @@ /* By: mmoussou #include #include @@ -39,6 +40,7 @@ void ft_sig(int sig) { } int main(int ac, char **av) { + _log = not_nullptr; if (help(ac, av)) { return EXIT_SUCCESS; } @@ -53,8 +55,11 @@ int main(int ac, char **av) { try { std::string str = av[1]; conf = new config::Config(str); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; + } catch (std::exception &) { + //std::cout << e.what() << std::endl; + //delete conf; + if (_log != not_nullptr) + delete _log; return 1; } if (signal(SIGINT, &ft_sig) == SIG_ERR) { diff --git a/src/server/Client.cpp b/src/server/Client.cpp index e035823..244c412 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou getTarget(); - str << " with code : "; + str << " with response code : "; str << _response.getStatusCode(); _log->info(str.str()); }