This commit is contained in:
whaffman 2025-10-20 16:15:07 +02:00
parent d1b392d2a2
commit 6cf0f73ee4
4 changed files with 42 additions and 21 deletions

View File

@ -4,6 +4,15 @@ header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache"); header("Pragma: no-cache");
header("Expires: 0"); header("Expires: 0");
header("X-Content-Type-Options: nosniff"); 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']++;
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -43,7 +52,8 @@ header("X-Content-Type-Options: nosniff");
<div class="info"> <div class="info">
<strong>CGI Status:</strong> <span class="success"> PHP CGI is working!</span><br> <strong>CGI Status:</strong> <span class="success"> PHP CGI is working!</span><br>
<strong>Execution Time:</strong> <?php echo date('Y-m-d H:i:s'); ?><br> <strong>Execution Time:</strong> <?php echo date('Y-m-d H:i:s'); ?><br>
<strong>PHP Version:</strong> <?php echo phpversion(); ?> <strong>PHP Version:</strong> <?php echo phpversion(); ?><br>
<strong>Session Request Count:</strong> <?php echo $_SESSION['request_count']; ?>
</div> </div>
<div class="container"> <div class="container">

View File

@ -36,6 +36,17 @@ CgiEnvironment::CgiEnvironment(const URI &uri, const HttpRequest &request)
env_["SERVER_PORT"] = std::to_string(port); env_["SERVER_PORT"] = std::to_string(port);
env_["REMOTE_ADDR"] = "<REMOTE_ADDR>"; // Placeholder, should be set to actual remote address env_["REMOTE_ADDR"] = "<REMOTE_ADDR>"; // Placeholder, should be set to actual remote address
env_["REDIRECT_STATUS"] = "200"; // Required by PHP with force-cgi-redirect enabled 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");
} }

View File

@ -164,21 +164,21 @@ void CgiHandler::parseCgiHeaders(std::string &headers)
end = headers.find("\r\n", start); end = headers.find("\r\n", start);
} }
// // Handle the last header (might not have trailing \r\n) // Handle the last header (might not have trailing \r\n)
// std::string lastHeader = headers.substr(start); std::string lastHeader = headers.substr(start);
// if (!lastHeader.empty()) if (!lastHeader.empty())
// { {
// Log::debug("Last CGI header: [" + lastHeader + "]"); Log::debug("Last CGI header: [" + lastHeader + "]");
// size_t colonPos = lastHeader.find(':'); size_t colonPos = lastHeader.find(':');
// if (colonPos != std::string::npos) if (colonPos != std::string::npos)
// { {
// std::string name = lastHeader.substr(0, colonPos); std::string name = lastHeader.substr(0, colonPos);
// std::string value = lastHeader.substr(colonPos + 1); std::string value = lastHeader.substr(colonPos + 1);
// name = utils::trim(name); name = utils::trim(name);
// value = utils::trim(value); value = utils::trim(value);
// response_.addHeader(name, value); response_.addHeader(name, value);
// } }
// } }
} }
void CgiHandler::parseCgiBody() void CgiHandler::parseCgiBody()

View File

@ -6,14 +6,14 @@
#include <webserv/handler/URI.hpp> #include <webserv/handler/URI.hpp>
#include <csignal>
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <string> #include <string>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <csignal> #include <unistd.h>
CgiProcess::CgiProcess(const HttpRequest &request, CgiHandler &handler) : request_(request), handler_(handler), _pid(-1) 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"); throw std::runtime_error("Failed to create pipes");
} }
CgiEnvironment cgiEnv(uri, request_);
_pid = fork(); _pid = fork();
if (_pid < 0) if (_pid < 0)
{ {
@ -63,7 +63,7 @@ void CgiProcess::spawn()
// Prepare arguments // Prepare arguments
std::string fullPath = uri.getFullPath(); std::string fullPath = uri.getFullPath();
CgiEnvironment cgiEnv(uri, request_);
char *args[] = {const_cast<char *>(cgiPath.c_str()), const_cast<char *>(fullPath.c_str()), nullptr}; char *args[] = {const_cast<char *>(cgiPath.c_str()), const_cast<char *>(fullPath.c_str()), nullptr};
// Log::debug("With args:", {args[0], args[1]}); // Log::debug("With args:", {args[0], args[1]});
@ -102,7 +102,7 @@ void CgiProcess::wait() noexcept
if (_pid > 0) if (_pid > 0)
{ {
int status; int status;
int waitResult =::waitpid(_pid, &status, WNOHANG); int waitResult = ::waitpid(_pid, &status, WNOHANG);
if (waitResult == -1) if (waitResult == -1)
{ {
Log::error("Error while waiting for CGI process with PID: " + std::to_string(_pid)); Log::error("Error while waiting for CGI process with PID: " + std::to_string(_pid));