」 feat: added timeout to cgi

This commit is contained in:
2025-07-02 13:04:00 +02:00
parent f1a1001846
commit 954574b43a
8 changed files with 68 additions and 34 deletions

View File

@ -6,14 +6,17 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/07/01 11:21:31 by adjoly ### ########.fr */ /* Updated: 2025/07/02 12:50:20 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include "server/PfdManager.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <ctime>
#include <server/AResource.hpp> #include <server/AResource.hpp>
#include <exception> #include <exception>
@ -23,6 +26,8 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include <wait.h>
namespace webserv { namespace webserv {
namespace http { namespace http {
class Get; class Get;
@ -62,16 +67,25 @@ class Cgi : public server::AClientResource {
} }
if (static_cast<CgiIn *>(ResourceManager::get(_stdin_pipe[PIPE_WRITE])) if (static_cast<CgiIn *>(ResourceManager::get(_stdin_pipe[PIPE_WRITE]))
->isProcessed() == true) { ->isProcessed() == true) {
std::cout << "in "
<< ResourceManager::get(_stdin_pipe[PIPE_WRITE])->getId()
<< std::endl;
std::cout << ResourceManager::get(_stdin_pipe[PIPE_WRITE])->type() << std::endl;
_log->debug("CGIIII post readyyy"); _log->debug("CGIIII post readyyy");
return true; return true;
} }
return false; return false;
} }
bool isTimedout(void) const {
if (!isProcessed() && isReady()) {
return false;
}
if (std::difftime(std::time(NULL), _start_time) >= 1) {
kill(_forkPid, SIGKILL);
waitpid(_forkPid, NULL, 0);
_log->warn("Cgi close due to timeout >= 1s");
return true;
}
return false;
}
protected: protected:
private: private:
bool _is_post; bool _is_post;
@ -99,6 +113,9 @@ class Cgi : public server::AClientResource {
int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a
// post // post
int _stdout_pipe[2]; // The pipefd for the stdout of the cgi int _stdout_pipe[2]; // The pipefd for the stdout of the cgi
std::time_t _start_time; // To use for timeout
pid_t _forkPid; // Can be used to kill the process
}; };
}; // namespace server }; // namespace server

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ /* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
/* Updated: 2025/07/02 11:35:04 by adjoly ### ########.fr */ /* Updated: 2025/07/02 11:56:36 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,7 +35,6 @@ class CgiIn : public AClientResource {
~CgiIn(void) { log("", "CgiIn", "destructor called"); } ~CgiIn(void) { log("", "CgiIn", "destructor called"); }
void process(void) { void process(void) {
std::cout << "process" << std::endl;
_processed = true; _processed = true;
ssize_t bytes_written = write(_fd, _body.c_str(), _body.size()); ssize_t bytes_written = write(_fd, _body.c_str(), _body.size());
_log->debug("writting body : " + _body); _log->debug("writting body : " + _body);

View File

@ -6,12 +6,13 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/07/02 11:37:44 by adjoly ### ########.fr */ /* Updated: 2025/07/02 12:36:59 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "server/PfdManager.hpp" #include "server/PfdManager.hpp"
#include "server/ResourceManager.hpp" #include "server/ResourceManager.hpp"
#include <ctime>
#include <help.hpp> #include <help.hpp>
#include <ios> #include <ios>
#include <requests/default.hpp> #include <requests/default.hpp>
@ -53,12 +54,11 @@ Cgi::Cgi(http::Post *req, config::Route *conf)
pfd.events = in->event(); pfd.events = in->event();
pfd.revents = 0; pfd.revents = 0;
pfd.fd = in->getId(); pfd.fd = in->getId();
std::cout << pfd.fd << std::endl;
server::PfdManager::append(pfd, server::RES); server::PfdManager::append(pfd, server::RES);
} }
Cgi::~Cgi(void) { Cgi::~Cgi(void) {
log("", "Cgi", "destructor called"); log("", "Cgi", "destructor called");
if (_is_post) { if (_is_post) {
PfdManager::remove(_stdin_pipe[PIPE_WRITE]); PfdManager::remove(_stdin_pipe[PIPE_WRITE]);
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]); ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
@ -73,7 +73,6 @@ void Cgi::_prep(void) {
throw std::runtime_error("stdout pipe failed for cgi D:"); throw std::runtime_error("stdout pipe failed for cgi D:");
_script_path = _conf->getRootDir() + _request->getTarget(); _script_path = _conf->getRootDir() + _request->getTarget();
_fd = _stdout_pipe[PIPE_READ]; _fd = _stdout_pipe[PIPE_READ];
std::cout << "sus = " << _fd << std::endl;
_pfd_event = POLLIN; _pfd_event = POLLIN;
if (access(_script_path.c_str(), X_OK)) if (access(_script_path.c_str(), X_OK))
throw std::runtime_error( throw std::runtime_error(
@ -143,12 +142,12 @@ char **Cgi::_genEnv(void) {
void Cgi::process(void) { void Cgi::process(void) {
_processed = true; _processed = true;
pid_t forkPid; _start_time = std::time(NULL);
forkPid = fork(); _forkPid = fork();
if (forkPid < 0) if (_forkPid < 0)
throw std::runtime_error("fork failed D:"); throw std::runtime_error("fork failed D:");
else if (forkPid == 0) { else if (_forkPid == 0) {
if (_is_post == true) { if (_is_post == true) {
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO); dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
close(_stdin_pipe[PIPE_READ]); close(_stdin_pipe[PIPE_READ]);
@ -164,7 +163,7 @@ void Cgi::process(void) {
if (execve(_script_path.c_str(), argv, env) == -1) { if (execve(_script_path.c_str(), argv, env) == -1) {
std::stringstream str; std::stringstream str;
str << "how did you do that ???? : "; str << "execve failed D:";
str << errno; str << errno;
_log->error(str.str()); _log->error(str.str());
for (int i = 0; env[i] != NULL; i++) for (int i = 0; env[i] != NULL; i++)
@ -176,7 +175,7 @@ void Cgi::process(void) {
if (_is_post) if (_is_post)
close(_stdin_pipe[PIPE_READ]); close(_stdin_pipe[PIPE_READ]);
close(_stdout_pipe[PIPE_WRITE]); close(_stdout_pipe[PIPE_WRITE]);
waitpid(forkPid, NULL, 0); // waitpid(_forkPid, NULL, 0);
} }
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/06/24 18:02:42 by adjoly ### ########.fr */ /* Updated: 2025/07/02 11:57:06 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -54,7 +54,7 @@ void Delete::parse(std::string const &data) {
this->_body = body_stream.str(); this->_body = body_stream.str();
_url = new URL(_target); _url = new URL(_target);
std::cout << *_url << std::endl; // std::cout << *_url << std::endl;
/* /*
std::cout << "-- start-line --" << std::endl; std::cout << "-- start-line --" << std::endl;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/06/30 15:03:52 by mmoussou ### ########.fr */ /* Updated: 2025/07/02 13:02:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -130,8 +130,9 @@ Response Get::execute(void) {
http::Response response; http::Response response;
if (_cgi != not_nullptr) { if (_cgi != not_nullptr) {
if (_method == "500") { if (_method == "500" || _cgi->isTimedout()) {
response.setStatusCode(500); response.setStatusCode(500);
response.setProtocol(_protocol);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody(http::Errors::getResponseBody( response.setBody(http::Errors::getResponseBody(
response.getStatusCode(), response.getStatusCode(),
@ -139,13 +140,11 @@ Response Get::execute(void) {
server::PfdManager::remove(_cgi->getId()); server::PfdManager::remove(_cgi->getId());
server::ResourceManager::remove(_cgi->getId()); server::ResourceManager::remove(_cgi->getId());
_cgi = not_nullptr; _cgi = not_nullptr;
// if (_url != not_nullptr)
// delete _url;
return response; return response;
} }
std::string str = static_cast<server::Cgi *>(_cgi)->str(); std::string str = static_cast<server::Cgi *>(_cgi)->str();
response = parseCgiOut(str); response = parseCgiOut(str);
std::cout << response.str(); // std::cout << response.str();
response.setProtocol(_protocol); response.setProtocol(_protocol);
server::PfdManager::remove(_cgi->getId()); server::PfdManager::remove(_cgi->getId());
server::ResourceManager::remove(_cgi->getId()); server::ResourceManager::remove(_cgi->getId());
@ -158,9 +157,17 @@ Response Get::execute(void) {
this->_target = this->_route->getRootDir() + this->_target; this->_target = this->_route->getRootDir() + this->_target;
try { try {
if (!access((this->_target + (this->_target[this->_target.length() - 1] != '/' ? std::string("/") : "") + _route->getIndex()).c_str(), R_OK)) if (!access((this->_target +
{ (this->_target[this->_target.length() - 1] != '/'
this->_target += (this->_target[this->_target.length() - 1] != '/' ? std::string("/") : "") + _route->getIndex(); ? std::string("/")
: "") +
_route->getIndex())
.c_str(),
R_OK)) {
this->_target += (this->_target[this->_target.length() - 1] != '/'
? std::string("/")
: "") +
_route->getIndex();
} }
if (isDirectory(this->_target)) { if (isDirectory(this->_target)) {
if (!access((this->_target + this->_route->getIndex()).c_str(), if (!access((this->_target + this->_route->getIndex()).c_str(),

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/06/23 21:25:57 by adjoly ### ########.fr */ /* Updated: 2025/07/02 13:01:28 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -126,8 +126,9 @@ Response Post::execute(void) {
http::Response response; http::Response response;
if (_cgi != not_nullptr) { if (_cgi != not_nullptr) {
if (_method == "500") { if (_method == "500" || _cgi->isTimedout()) {
response.setStatusCode(500); response.setStatusCode(500);
response.setProtocol(_protocol);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody(http::Errors::getResponseBody( response.setBody(http::Errors::getResponseBody(
response.getStatusCode(), response.getStatusCode(),

View File

@ -6,13 +6,15 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/06/23 21:14:20 by adjoly ### ########.fr */ /* Updated: 2025/07/02 12:58:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "requests/RedirectResp.hpp" #include "requests/RedirectResp.hpp"
#include "requests/default.hpp" #include "requests/default.hpp"
#include "server/PfdManager.hpp"
#include "server/ResourceManager.hpp"
#include <cstddef> #include <cstddef>
#include <log.hpp> #include <log.hpp>
#include <server/Cgi.hpp> #include <server/Cgi.hpp>
@ -64,7 +66,6 @@ void Client::parse(void) {
} }
} }
if (!_route || _route == not_nullptr) { if (!_route || _route == not_nullptr) {
_request->setMethod("404"); _request->setMethod("404");
return; return;
@ -83,9 +84,15 @@ void Client::parse(void) {
bool Client::requestParsed(void) { bool Client::requestParsed(void) {
if (_request == not_nullptr) if (_request == not_nullptr)
return false; return false;
if (_request->getCgi() != not_nullptr) if (_request->getCgi() != not_nullptr) {
if (!_request->getCgi()->isProcessed()) if (!_request->getCgi()->isProcessed())
return false; return false;
else if (_request->getCgi()->isTimedout())
return true;
else if (!(PfdManager::get(_request->getCgi()->getId())->revents &
_request->getCgi()->event()))
return false;
}
return true; return true;
} }

View File

@ -6,11 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */ /* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */
/* Updated: 2025/07/01 10:47:05 by adjoly ### ########.fr */ /* Updated: 2025/07/02 12:45:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "server/AResource.hpp"
#include <cstddef> #include <cstddef>
#include <iterator> #include <iterator>
#include <server/default.hpp> #include <server/default.hpp>
@ -129,6 +130,9 @@ void Server::_handle_resource(size_t i) {
if (res == not_nullptr) if (res == not_nullptr)
return; return;
if (res->type() == CGI && static_cast<Cgi *>(res)->isTimedout()) {
return;
}
if (!res->isProcessed() && res->isReady()) { if (!res->isProcessed() && res->isReady()) {
_log->debug("processing resource"); _log->debug("processing resource");
res->process(); res->process();