From f222cce4ff981380912fb7caff3e750481ad7475 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 8 Oct 2025 17:20:40 +0200 Subject: [PATCH] refactor: has and owns --- webserv/config/AConfig.cpp | 24 +++++++++++++------ webserv/config/AConfig.hpp | 4 +++- .../config/validation/ValidationEngine.cpp | 5 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/webserv/config/AConfig.cpp b/webserv/config/AConfig.cpp index d29e219..1f0fc00 100644 --- a/webserv/config/AConfig.cpp +++ b/webserv/config/AConfig.cpp @@ -1,5 +1,4 @@ #include // for AConfig - #include // for ADirective #include // for DirectiveFactory #include // for DirectiveValue @@ -47,7 +46,21 @@ std::vector AConfig::getDirectives() const return result; } -bool AConfig::hasDirective(const std::string &name) const +bool AConfig::has(const std::string &name) const +{ + if (owns(name)) + { + return true; + } + + if (parent_ != nullptr) + { + return parent_->has(name); + } + return false; +} + +bool AConfig::owns(const std::string &name) const { for (const auto &directive : directives_) { @@ -56,10 +69,7 @@ bool AConfig::hasDirective(const std::string &name) const return true; } } - if (parent_ != nullptr) - { - return parent_->hasDirective(name); - } + return false; } @@ -84,7 +94,7 @@ void AConfig::parseDirectives(const std::string &declarations) std::string AConfig::getErrorPage(int statusCode) const { // TODO - Log::trace(LOCATION); + Log::trace(LOCATION); for (const auto &directive : directives_) { if (directive->getName() == "error_page") diff --git a/webserv/config/AConfig.hpp b/webserv/config/AConfig.hpp index d57213b..198f9a5 100644 --- a/webserv/config/AConfig.hpp +++ b/webserv/config/AConfig.hpp @@ -24,7 +24,9 @@ class AConfig void addDirective(const std::string &line); [[nodiscard]] std::string getErrorPage(int statusCode) const; - [[nodiscard]] bool hasDirective(const std::string &name) const; + [[nodiscard]] bool has(const std::string &name) const; + [[nodiscard]] bool owns(const std::string &name) const; + [[nodiscard]] const ADirective *getDirective(const std::string &name) const; [[nodiscard]] std::vector getDirectives() const; diff --git a/webserv/config/validation/ValidationEngine.cpp b/webserv/config/validation/ValidationEngine.cpp index c562a7f..429b06e 100644 --- a/webserv/config/validation/ValidationEngine.cpp +++ b/webserv/config/validation/ValidationEngine.cpp @@ -1,9 +1,8 @@ -#include - #include // for AConfig #include // for GlobalConfig #include // for LocationConfig #include // for ServerConfig +#include #include // for ValidationResult #include // for AValidationRule #include // for Log, LOCATION @@ -92,7 +91,7 @@ void ValidationEngine::validateConfig(RuleMap const &rulesMap, const AConfig *co Log::trace(LOCATION); for (const auto &[directiveName, rules] : rulesMap) { - if (!config->hasDirective(directiveName)) + if (!config->has(directiveName)) { // Check if any rule requires the directive for (const auto &rule : rules)