feat(AConfig, URI): add getCGIPath method and implement CGI check in URI
This commit is contained in:
parent
42daf68ee2
commit
f172987e63
@ -8,6 +8,7 @@
|
||||
|
||||
#include <sstream> // for basic_stringstream, stringstream
|
||||
#include <utility> // for pair, move
|
||||
#include <ranges> // 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<std::vector<std::string>>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto exts = directive->getValue().try_get<std::vector<std::string>>().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
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
#include <webserv/handler/URI.hpp>
|
||||
|
||||
#include <webserv/config/AConfig.hpp> // for AConfig
|
||||
#include <webserv/config/LocationConfig.hpp> // for LocationConfig
|
||||
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
|
||||
#include <webserv/handler/URI.hpp>
|
||||
#include <webserv/http/HttpHeaders.hpp> // for HttpHeaders
|
||||
#include <webserv/utils/FileUtils.hpp> // for joinPath, getExtension, isDirectory, isFile, isValidPath
|
||||
#include <webserv/utils/utils.hpp> // 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<std::string>("cgi_enabled").has_value()
|
||||
|| config_->get<std::string>("cgi_enabled").value() != "on")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto cgiPath = config_->getCGIPath(getExtension());
|
||||
return !cgiPath.empty();
|
||||
}
|
||||
|
||||
const std::string &URI::getBaseName() const
|
||||
{
|
||||
return baseName_;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user