diff --git a/webserv/config/ConfigManager.cpp b/webserv/config/ConfigManager.cpp index b73ce15..12563f1 100644 --- a/webserv/config/ConfigManager.cpp +++ b/webserv/config/ConfigManager.cpp @@ -9,6 +9,7 @@ #include // for basic_stringstream #include // for runtime_error #include // for basic_string, char_traits, operator+, string, to_string, operator==, stoi +#include #include // for size_t @@ -87,10 +88,10 @@ ServerConfig *ConfigManager::getMatchingServerConfig(const std::string &host, in std::vector serverConfigs = globalConfig_->getServerConfigs(); for (ServerConfig *serverConfig : serverConfigs) { - auto serverName = serverConfig->get("server_name").value_or(""); + auto serverNames = serverConfig->get>("server_name").value_or(std::vector()); auto listenPorts = serverConfig->get("listen").value_or(80); - Log::debug("Checking server config: " + serverName + " on port " + std::to_string(listenPorts)); - if ((serverName == host) && (listenPorts == port)) + // Log::debug("Checking server config: " + serverName + " on port " + std::to_string(listenPorts)); + if ((std::find(serverNames.begin(), serverNames.end(), host) != serverNames.end()) && (listenPorts == port)) { Log::info("Found matching server config for host: " + host + " and port: " + std::to_string(port)); return serverConfig; diff --git a/webserv/config/directive/DirectiveFactory.hpp b/webserv/config/directive/DirectiveFactory.hpp index 966b695..5a17f0c 100644 --- a/webserv/config/directive/DirectiveFactory.hpp +++ b/webserv/config/directive/DirectiveFactory.hpp @@ -23,7 +23,7 @@ class DirectiveFactory constexpr static std::array supportedDirectives = {{ {.name = "listen", .type = "IntDirective", .context = "S"}, {.name = "host", .type = "StringDirective", .context = "S"}, - {.name = "server_name", .type = "StringDirective", .context = "S"}, + {.name = "server_name", .type = "VectorDirective", .context = "S"}, {.name = "root", .type = "StringDirective", .context = "Sl"}, {.name = "index", .type = "VectorDirective", .context = "sl"}, {.name = "error_page", .type = "IntStringDirective", .context = "gsl"}, diff --git a/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp b/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp index 19c5fbd..4ec4950 100644 --- a/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp +++ b/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp @@ -35,19 +35,30 @@ ValidationResult UniqueServerNamesRule::validateGlobal(const GlobalConfig *confi continue; } - auto serverNameOpt = server->get("server_name"); + auto serverNameOpt = server->get>("server_name"); auto listenOpt = server->get("listen"); if (serverNameOpt.has_value() && listenOpt.has_value()) { - const std::string &serverName = serverNameOpt.value(); + const auto &serverNameCandidates = serverNameOpt.value(); int listenPort = listenOpt.value(); - if (serverNames.contains(serverName + ":" + std::to_string(listenPort))) + for (const auto &name : serverNameCandidates) { - return ValidationResult::error("Duplicate server name '" + serverName + "' found in configuration"); + std::string uniqueKey = name + ":" + std::to_string(listenPort); + if (serverNames.contains(uniqueKey)) + { + return ValidationResult::error("Duplicate server name '" + name + + "' found in configuration on port " + + std::to_string(listenPort)); + } + serverNames.insert(uniqueKey); } + // if (serverNames.contains(serverName + ":" + std::to_string(listenPort))) + // { + // return ValidationResult::error("Duplicate server name '" + serverName + "' found in configuration"); + // } - serverNames.insert(serverName + ":" + std::to_string(listenPort)); + // serverNames.insert(serverName + ":" + std::to_string(listenPort)); } }