feat: add getCGIEnvironment method to retrieve CGI environment variables

This commit is contained in:
Quinten 2025-10-09 18:06:05 +02:00
parent 35d17d8fa9
commit 20ae0e03dd
2 changed files with 30 additions and 2 deletions

View File

@ -104,6 +104,29 @@ void URI::parseFullpath()
fullPath_ = FileUtils::joinPath(dir_, baseName_);
}
std::map<std::string, std::string> URI::getCGIEnvironment() const
{
std::map<std::string, std::string> env;
// URI components
env["REQUEST_URI"] = uriTrimmed_;
env["SCRIPT_NAME"] = getFullPath();
env["PATH_INFO"] = getPathInfo();
env["QUERY_STRING"] = getQuery();
// Authority components
env["SERVER_NAME"] = config_->get<std::string>("server_name").value_or("");
env["SERVER_PORT"] = std::to_string(config_->get<int>("listen").value_or(-1));
env["REQUEST_SCHEME"] = "HTTP";
// HTTP context
// env["REQUEST_METHOD"] = requestMethod_;
// env["CONTENT_TYPE"] = contentType_;
// env["CONTENT_LENGTH"] = contentLength_;
return env;
}
const AConfig *URI::getConfig() const
{
return config_;
@ -158,3 +181,8 @@ const std::string &URI::getFragment() const
{
return fragment_;
}
const std::string &URI::getAuthority() const
{
return authority_;
}

View File

@ -17,6 +17,8 @@ class URI
public:
URI(const HttpRequest &request, const ServerConfig &serverConfig);
[[nodiscard]] std::map<std::string, std::string> getCGIEnvironment() const;
[[nodiscard]] bool isFile() const;
[[nodiscard]] bool isDirectory() const;
[[nodiscard]] bool isValid() const;
@ -31,7 +33,6 @@ class URI
[[nodiscard]] const std::string &getQuery() const;
[[nodiscard]] const std::string &getFragment() const;
[[nodiscard]] const std::string &getAuthority() const;
[[nodiscard]] const std::string &getScheme() const;
private:
void parseUri(const std::string &uri);
@ -46,7 +47,6 @@ class URI
std::string query_;
std::string fragment_;
std::string authority_;
std::string scheme_;
static const AConfig *matchConfig(const std::string &uri, const ServerConfig &serverConfig);
};