From 8ff043802cf562667d4a82a74d5f2c2e54bc8e77 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 11:17:05 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20added=20g?= =?UTF-8?q?etserver=20in=20config=20to=20get=20a=20server=20from=20a=20ser?= =?UTF-8?q?ver=5Fname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 7 ++++--- includes/config/Server.hpp | 13 ++++++++++++- src/config/Config.cpp | 22 +++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index 8b2e413..52280d6 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/18 10:03:09 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:57:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,11 @@ class Config { Logger *getLogger(void) { return _log; } - std::vector *getServers(void) { return _servers; } + std::vector getServers(void) { return _servers; } + Server *getServer(const std::string &); private: - std::vector *_servers; + std::vector _servers; }; }; // namespace config diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 38db1ea..8bdc37d 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/18 10:08:33 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:59:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "config/default.hpp" #include "cppeleven.hpp" #include "node/ANode.hpp" +#include "webserv.hpp" namespace webserv { namespace config { @@ -55,6 +56,16 @@ class Server { // @brief Can be used to get the port specified in the config file int getPort(void) { return _port; } + // @brief Can be used to check if a servername is present in this config + bool isServerName(const std::string &server_name) { + for (auto it = prange(_server_names)) { + if (*it == server_name) { + return true; + } + } + return false; + } + protected: private: std::map diff --git a/src/config/Config.cpp b/src/config/Config.cpp index e0b8fbf..7ab3369 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,12 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:21:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:11:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "cppeleven.hpp" #include "node/ANode.hpp" #include +#include using namespace webserv::config; @@ -30,19 +32,25 @@ Config::Config(std::string &filename) { } std::map *node = table->getTable(); - for (std::map::iterator it = node->begin(); - it != node->end(); it++) { + for (auto it = prange(node)) { Server *srv = new Server(it->second); - _servers->push_back(srv); + _servers.push_back(srv); } delete table; delete file; } Config::~Config(void) { - std::vector::iterator it = _servers->begin(); - for (; it != _servers->end() ; it++) { + for (auto it = range(_servers)) { delete *it; } - delete _servers; +} + +Server *Config::getServer(const std::string &server_name) { + for (auto it = range(_servers)) { + if ((*it)->isServerName(server_name)) { + return (*it); + } + } + return (not_nullptr); }