cookies
This commit is contained in:
parent
d1b392d2a2
commit
6cf0f73ee4
@ -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']++;
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@ -43,7 +52,8 @@ header("X-Content-Type-Options: nosniff");
|
||||
<div class="info">
|
||||
<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>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 class="container">
|
||||
|
||||
@ -36,6 +36,17 @@ CgiEnvironment::CgiEnvironment(const URI &uri, const HttpRequest &request)
|
||||
env_["SERVER_PORT"] = std::to_string(port);
|
||||
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_["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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -6,14 +6,14 @@
|
||||
|
||||
#include <webserv/handler/URI.hpp>
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <csignal>
|
||||
#include <unistd.h>
|
||||
|
||||
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<char *>(cgiPath.c_str()), const_cast<char *>(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));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user