From f172987e63f9f690f4668f27f6ed6bea12e1a053 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 14 Oct 2025 22:11:34 +0200 Subject: [PATCH] feat(AConfig, URI): add getCGIPath method and implement CGI check in URI --- webserv/config/AConfig.cpp | 32 ++++++++++++++++++++++++++++++++ webserv/config/AConfig.hpp | 1 + webserv/handler/URI.cpp | 14 ++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/webserv/config/AConfig.cpp b/webserv/config/AConfig.cpp index 12c11a8..27b1935 100644 --- a/webserv/config/AConfig.cpp +++ b/webserv/config/AConfig.cpp @@ -8,6 +8,7 @@ #include // for basic_stringstream, stringstream #include // for pair, move +#include // for filter AConfig::AConfig(const AConfig *parent) : parent_(parent) {} @@ -112,4 +113,35 @@ std::string AConfig::getErrorPage(int statusCode) const return parent_->getErrorPage(statusCode); } return ""; // Return empty string if not found +} + +std::string AConfig::getCGIPath(const std::string &extension) const +{ + Log::trace(LOCATION); + for (const auto &directive : directives_ | std::views::filter([](const auto &d) { + return d->getName() == "cgi_ext"; + })) + { + + if (!directive->getValue().holds>()) + { + continue; + } + auto exts = directive->getValue().try_get>().value(); + { + continue; + } + auto cgiPath = exts.back(); + exts.pop_back(); // Last element is the CGI path + auto it = std::ranges::find(exts, extension); + if (it != exts.end()) + { + return cgiPath; + } + } + if (parent_ != nullptr) + { + return parent_->getCGIPath(extension); + } + return {}; // Return empty string if not found } \ No newline at end of file diff --git a/webserv/config/AConfig.hpp b/webserv/config/AConfig.hpp index 198f9a5..08ad6fc 100644 --- a/webserv/config/AConfig.hpp +++ b/webserv/config/AConfig.hpp @@ -23,6 +23,7 @@ class AConfig [[nodiscard]] virtual std::string getType() const = 0; void addDirective(const std::string &line); [[nodiscard]] std::string getErrorPage(int statusCode) const; + [[nodiscard]] std::string getCGIPath(const std::string &extension) const; [[nodiscard]] bool has(const std::string &name) const; [[nodiscard]] bool owns(const std::string &name) const; diff --git a/webserv/handler/URI.cpp b/webserv/handler/URI.cpp index 6dc89b7..8950e64 100644 --- a/webserv/handler/URI.cpp +++ b/webserv/handler/URI.cpp @@ -1,8 +1,7 @@ -#include - #include // for AConfig #include // for LocationConfig #include // for ServerConfig +#include #include // for HttpHeaders #include // for joinPath, getExtension, isDirectory, isFile, isValidPath #include // for trim, split @@ -147,6 +146,17 @@ bool URI::isValid() const return FileUtils::isValidPath(fullPath_); } +bool URI::isCgi() const +{ + if (isFile() || getExtension().empty() || !config_->get("cgi_enabled").has_value() + || config_->get("cgi_enabled").value() != "on") + { + return false; + } + auto cgiPath = config_->getCGIPath(getExtension()); + return !cgiPath.empty(); +} + const std::string &URI::getBaseName() const { return baseName_;