feat: allow multiple server names

This commit is contained in:
Quinten 2025-10-29 16:54:14 +01:00
parent c09e5b6579
commit 1ef4adc3d8
3 changed files with 21 additions and 9 deletions

View File

@ -9,6 +9,7 @@
#include <sstream> // for basic_stringstream #include <sstream> // for basic_stringstream
#include <stdexcept> // for runtime_error #include <stdexcept> // for runtime_error
#include <string> // for basic_string, char_traits, operator+, string, to_string, operator==, stoi #include <string> // for basic_string, char_traits, operator+, string, to_string, operator==, stoi
#include <vector>
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
@ -87,10 +88,10 @@ ServerConfig *ConfigManager::getMatchingServerConfig(const std::string &host, in
std::vector<ServerConfig *> serverConfigs = globalConfig_->getServerConfigs(); std::vector<ServerConfig *> serverConfigs = globalConfig_->getServerConfigs();
for (ServerConfig *serverConfig : serverConfigs) for (ServerConfig *serverConfig : serverConfigs)
{ {
auto serverName = serverConfig->get<std::string>("server_name").value_or(""); auto serverNames = serverConfig->get<std::vector<std::string>>("server_name").value_or(std::vector<std::string>());
auto listenPorts = serverConfig->get<int>("listen").value_or(80); auto listenPorts = serverConfig->get<int>("listen").value_or(80);
Log::debug("Checking server config: " + serverName + " on port " + std::to_string(listenPorts)); // Log::debug("Checking server config: " + serverName + " on port " + std::to_string(listenPorts));
if ((serverName == host) && (listenPorts == port)) 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)); Log::info("Found matching server config for host: " + host + " and port: " + std::to_string(port));
return serverConfig; return serverConfig;

View File

@ -23,7 +23,7 @@ class DirectiveFactory
constexpr static std::array<DirectiveInfo, 16> supportedDirectives = {{ constexpr static std::array<DirectiveInfo, 16> supportedDirectives = {{
{.name = "listen", .type = "IntDirective", .context = "S"}, {.name = "listen", .type = "IntDirective", .context = "S"},
{.name = "host", .type = "StringDirective", .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 = "root", .type = "StringDirective", .context = "Sl"},
{.name = "index", .type = "VectorDirective", .context = "sl"}, {.name = "index", .type = "VectorDirective", .context = "sl"},
{.name = "error_page", .type = "IntStringDirective", .context = "gsl"}, {.name = "error_page", .type = "IntStringDirective", .context = "gsl"},

View File

@ -35,19 +35,30 @@ ValidationResult UniqueServerNamesRule::validateGlobal(const GlobalConfig *confi
continue; continue;
} }
auto serverNameOpt = server->get<std::string>("server_name"); auto serverNameOpt = server->get<std::vector<std::string>>("server_name");
auto listenOpt = server->get<int>("listen"); auto listenOpt = server->get<int>("listen");
if (serverNameOpt.has_value() && listenOpt.has_value()) if (serverNameOpt.has_value() && listenOpt.has_value())
{ {
const std::string &serverName = serverNameOpt.value(); const auto &serverNameCandidates = serverNameOpt.value();
int listenPort = listenOpt.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));
} }
} }