refactor: has and owns

This commit is contained in:
whaffman 2025-10-08 17:20:40 +02:00
parent 9aaa085098
commit f222cce4ff
3 changed files with 22 additions and 11 deletions

View File

@ -1,5 +1,4 @@
#include <webserv/config/AConfig.hpp> // for AConfig #include <webserv/config/AConfig.hpp> // for AConfig
#include <webserv/config/directive/ADirective.hpp> // for ADirective #include <webserv/config/directive/ADirective.hpp> // for ADirective
#include <webserv/config/directive/DirectiveFactory.hpp> // for DirectiveFactory #include <webserv/config/directive/DirectiveFactory.hpp> // for DirectiveFactory
#include <webserv/config/directive/DirectiveValue.hpp> // for DirectiveValue #include <webserv/config/directive/DirectiveValue.hpp> // for DirectiveValue
@ -47,7 +46,21 @@ std::vector<const ADirective *> AConfig::getDirectives() const
return result; 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_) for (const auto &directive : directives_)
{ {
@ -56,10 +69,7 @@ bool AConfig::hasDirective(const std::string &name) const
return true; return true;
} }
} }
if (parent_ != nullptr)
{
return parent_->hasDirective(name);
}
return false; return false;
} }
@ -84,7 +94,7 @@ void AConfig::parseDirectives(const std::string &declarations)
std::string AConfig::getErrorPage(int statusCode) const std::string AConfig::getErrorPage(int statusCode) const
{ {
// TODO // TODO
Log::trace(LOCATION); Log::trace(LOCATION);
for (const auto &directive : directives_) for (const auto &directive : directives_)
{ {
if (directive->getName() == "error_page") if (directive->getName() == "error_page")

View File

@ -24,7 +24,9 @@ class AConfig
void addDirective(const std::string &line); void addDirective(const std::string &line);
[[nodiscard]] std::string getErrorPage(int statusCode) const; [[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]] const ADirective *getDirective(const std::string &name) const;
[[nodiscard]] std::vector<const ADirective *> getDirectives() const; [[nodiscard]] std::vector<const ADirective *> getDirectives() const;

View File

@ -1,9 +1,8 @@
#include <webserv/config/validation/ValidationEngine.hpp>
#include <webserv/config/AConfig.hpp> // for AConfig #include <webserv/config/AConfig.hpp> // for AConfig
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig #include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
#include <webserv/config/LocationConfig.hpp> // for LocationConfig #include <webserv/config/LocationConfig.hpp> // for LocationConfig
#include <webserv/config/ServerConfig.hpp> // for ServerConfig #include <webserv/config/ServerConfig.hpp> // for ServerConfig
#include <webserv/config/validation/ValidationEngine.hpp>
#include <webserv/config/validation/ValidationResult.hpp> // for ValidationResult #include <webserv/config/validation/ValidationResult.hpp> // for ValidationResult
#include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule #include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule
#include <webserv/log/Log.hpp> // for Log, LOCATION #include <webserv/log/Log.hpp> // for Log, LOCATION
@ -92,7 +91,7 @@ void ValidationEngine::validateConfig(RuleMap const &rulesMap, const AConfig *co
Log::trace(LOCATION); Log::trace(LOCATION);
for (const auto &[directiveName, rules] : rulesMap) for (const auto &[directiveName, rules] : rulesMap)
{ {
if (!config->hasDirective(directiveName)) if (!config->has(directiveName))
{ {
// Check if any rule requires the directive // Check if any rule requires the directive
for (const auto &rule : rules) for (const auto &rule : rules)