diff --git a/htdocs/site-3/index.php b/htdocs/site-3/index.php index 0b68556..3448fdb 100644 --- a/htdocs/site-3/index.php +++ b/htdocs/site-3/index.php @@ -4,6 +4,15 @@ header("Cache-Control: no-cache, no-store, must-revalidate"); header("Pragma: no-cache"); header("Expires: 0"); header("X-Content-Type-Options: nosniff"); +session_start(); +// Initialize counter if not set +if (!isset($_SESSION['request_count'])) { + $_SESSION['request_count'] = 0; +} + +// Increment on each request +$_SESSION['request_count']++; + ?> @@ -43,7 +52,8 @@ header("X-Content-Type-Options: nosniff");
CGI Status: ✅ PHP CGI is working!
Execution Time:
- PHP Version: + PHP Version:
+ Session Request Count:
diff --git a/webserv/handler/CgiEnvironment.cpp b/webserv/handler/CgiEnvironment.cpp index b606900..8415385 100644 --- a/webserv/handler/CgiEnvironment.cpp +++ b/webserv/handler/CgiEnvironment.cpp @@ -36,6 +36,17 @@ CgiEnvironment::CgiEnvironment(const URI &uri, const HttpRequest &request) env_["SERVER_PORT"] = std::to_string(port); env_["REMOTE_ADDR"] = ""; // Placeholder, should be set to actual remote address env_["REDIRECT_STATUS"] = "200"; // Required by PHP with force-cgi-redirect enabled + env_["SERVER_SOFTWARE"] = "Webserv/1.0"; + env_["REQUEST_SCHEME"] = "HTTP"; + env_["HTTP_VERSION"] = "1.1"; + // Add HTTP_ headers + const HttpHeaders &headers = request.getHeaders(); + env_["HTTP_COOKIE"] = headers.get("Cookie"); + env_["HTTP_USER_AGENT"] = headers.get("User-Agent"); + env_["HTTP_ACCEPT"] = headers.get("Accept"); + env_["HTTP_ACCEPT_LANGUAGE"] = headers.get("Accept-Language"); + env_["HTTP_ACCEPT_ENCODING"] = headers.get("Accept-Encoding"); + } diff --git a/webserv/handler/CgiHandler.cpp b/webserv/handler/CgiHandler.cpp index 3b781a6..835cd5c 100644 --- a/webserv/handler/CgiHandler.cpp +++ b/webserv/handler/CgiHandler.cpp @@ -164,21 +164,21 @@ void CgiHandler::parseCgiHeaders(std::string &headers) end = headers.find("\r\n", start); } - // // Handle the last header (might not have trailing \r\n) - // std::string lastHeader = headers.substr(start); - // if (!lastHeader.empty()) - // { - // Log::debug("Last CGI header: [" + lastHeader + "]"); - // size_t colonPos = lastHeader.find(':'); - // if (colonPos != std::string::npos) - // { - // std::string name = lastHeader.substr(0, colonPos); - // std::string value = lastHeader.substr(colonPos + 1); - // name = utils::trim(name); - // value = utils::trim(value); - // response_.addHeader(name, value); - // } - // } + // Handle the last header (might not have trailing \r\n) + std::string lastHeader = headers.substr(start); + if (!lastHeader.empty()) + { + Log::debug("Last CGI header: [" + lastHeader + "]"); + size_t colonPos = lastHeader.find(':'); + if (colonPos != std::string::npos) + { + std::string name = lastHeader.substr(0, colonPos); + std::string value = lastHeader.substr(colonPos + 1); + name = utils::trim(name); + value = utils::trim(value); + response_.addHeader(name, value); + } + } } void CgiHandler::parseCgiBody() diff --git a/webserv/handler/CgiProcess.cpp b/webserv/handler/CgiProcess.cpp index 3cdead7..343b11f 100644 --- a/webserv/handler/CgiProcess.cpp +++ b/webserv/handler/CgiProcess.cpp @@ -6,14 +6,14 @@ #include +#include #include #include #include #include -#include #include -#include +#include CgiProcess::CgiProcess(const HttpRequest &request, CgiHandler &handler) : request_(request), handler_(handler), _pid(-1) { @@ -39,7 +39,7 @@ void CgiProcess::spawn() { throw std::runtime_error("Failed to create pipes"); } - + CgiEnvironment cgiEnv(uri, request_); _pid = fork(); if (_pid < 0) { @@ -63,7 +63,7 @@ void CgiProcess::spawn() // Prepare arguments std::string fullPath = uri.getFullPath(); - CgiEnvironment cgiEnv(uri, request_); + char *args[] = {const_cast(cgiPath.c_str()), const_cast(fullPath.c_str()), nullptr}; // Log::debug("With args:", {args[0], args[1]}); @@ -102,7 +102,7 @@ void CgiProcess::wait() noexcept if (_pid > 0) { int status; - int waitResult =::waitpid(_pid, &status, WNOHANG); + int waitResult = ::waitpid(_pid, &status, WNOHANG); if (waitResult == -1) { Log::error("Error while waiting for CGI process with PID: " + std::to_string(_pid));