mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-07-15 13:46:32 +02:00
「✨」 feat: added timeout to cgi
This commit is contained in:
@ -6,14 +6,17 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
||||
#include "server/PfdManager.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <server/AResource.hpp>
|
||||
|
||||
#include <exception>
|
||||
@ -23,6 +26,8 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <wait.h>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
class Get;
|
||||
@ -62,16 +67,25 @@ class Cgi : public server::AClientResource {
|
||||
}
|
||||
if (static_cast<CgiIn *>(ResourceManager::get(_stdin_pipe[PIPE_WRITE]))
|
||||
->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");
|
||||
return true;
|
||||
}
|
||||
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:
|
||||
private:
|
||||
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
|
||||
// post
|
||||
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
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"); }
|
||||
|
||||
void process(void) {
|
||||
std::cout << "process" << std::endl;
|
||||
_processed = true;
|
||||
ssize_t bytes_written = write(_fd, _body.c_str(), _body.size());
|
||||
_log->debug("writting body : " + _body);
|
||||
|
@ -6,12 +6,13 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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/ResourceManager.hpp"
|
||||
#include <ctime>
|
||||
#include <help.hpp>
|
||||
#include <ios>
|
||||
#include <requests/default.hpp>
|
||||
@ -53,12 +54,11 @@ Cgi::Cgi(http::Post *req, config::Route *conf)
|
||||
pfd.events = in->event();
|
||||
pfd.revents = 0;
|
||||
pfd.fd = in->getId();
|
||||
std::cout << pfd.fd << std::endl;
|
||||
server::PfdManager::append(pfd, server::RES);
|
||||
}
|
||||
|
||||
Cgi::~Cgi(void) {
|
||||
log("➖", "Cgi", "destructor called");
|
||||
Cgi::~Cgi(void) {
|
||||
log("➖", "Cgi", "destructor called");
|
||||
if (_is_post) {
|
||||
PfdManager::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:");
|
||||
_script_path = _conf->getRootDir() + _request->getTarget();
|
||||
_fd = _stdout_pipe[PIPE_READ];
|
||||
std::cout << "sus = " << _fd << std::endl;
|
||||
_pfd_event = POLLIN;
|
||||
if (access(_script_path.c_str(), X_OK))
|
||||
throw std::runtime_error(
|
||||
@ -143,12 +142,12 @@ char **Cgi::_genEnv(void) {
|
||||
|
||||
void Cgi::process(void) {
|
||||
_processed = true;
|
||||
pid_t forkPid;
|
||||
_start_time = std::time(NULL);
|
||||
|
||||
forkPid = fork();
|
||||
if (forkPid < 0)
|
||||
_forkPid = fork();
|
||||
if (_forkPid < 0)
|
||||
throw std::runtime_error("fork failed D:");
|
||||
else if (forkPid == 0) {
|
||||
else if (_forkPid == 0) {
|
||||
if (_is_post == true) {
|
||||
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
@ -164,7 +163,7 @@ void Cgi::process(void) {
|
||||
|
||||
if (execve(_script_path.c_str(), argv, env) == -1) {
|
||||
std::stringstream str;
|
||||
str << "how did you do that ???? : ";
|
||||
str << "execve failed D:";
|
||||
str << errno;
|
||||
_log->error(str.str());
|
||||
for (int i = 0; env[i] != NULL; i++)
|
||||
@ -176,7 +175,7 @@ void Cgi::process(void) {
|
||||
if (_is_post)
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdout_pipe[PIPE_WRITE]);
|
||||
waitpid(forkPid, NULL, 0);
|
||||
// waitpid(_forkPid, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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();
|
||||
|
||||
_url = new URL(_target);
|
||||
std::cout << *_url << std::endl;
|
||||
// std::cout << *_url << std::endl;
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (_cgi != not_nullptr) {
|
||||
if (_method == "500") {
|
||||
if (_method == "500" || _cgi->isTimedout()) {
|
||||
response.setStatusCode(500);
|
||||
response.setProtocol(_protocol);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(
|
||||
response.getStatusCode(),
|
||||
@ -139,13 +140,11 @@ Response Get::execute(void) {
|
||||
server::PfdManager::remove(_cgi->getId());
|
||||
server::ResourceManager::remove(_cgi->getId());
|
||||
_cgi = not_nullptr;
|
||||
// if (_url != not_nullptr)
|
||||
// delete _url;
|
||||
return response;
|
||||
}
|
||||
std::string str = static_cast<server::Cgi *>(_cgi)->str();
|
||||
response = parseCgiOut(str);
|
||||
std::cout << response.str();
|
||||
// std::cout << response.str();
|
||||
response.setProtocol(_protocol);
|
||||
server::PfdManager::remove(_cgi->getId());
|
||||
server::ResourceManager::remove(_cgi->getId());
|
||||
@ -158,9 +157,17 @@ Response Get::execute(void) {
|
||||
this->_target = this->_route->getRootDir() + this->_target;
|
||||
|
||||
try {
|
||||
if (!access((this->_target + (this->_target[this->_target.length() - 1] != '/' ? std::string("/") : "") + _route->getIndex()).c_str(), R_OK))
|
||||
{
|
||||
this->_target += (this->_target[this->_target.length() - 1] != '/' ? std::string("/") : "") + _route->getIndex();
|
||||
if (!access((this->_target +
|
||||
(this->_target[this->_target.length() - 1] != '/'
|
||||
? 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 (!access((this->_target + this->_route->getIndex()).c_str(),
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (_cgi != not_nullptr) {
|
||||
if (_method == "500") {
|
||||
if (_method == "500" || _cgi->isTimedout()) {
|
||||
response.setStatusCode(500);
|
||||
response.setProtocol(_protocol);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(
|
||||
response.getStatusCode(),
|
||||
|
@ -6,13 +6,15 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "requests/RedirectResp.hpp"
|
||||
#include "requests/default.hpp"
|
||||
#include "server/PfdManager.hpp"
|
||||
#include "server/ResourceManager.hpp"
|
||||
#include <cstddef>
|
||||
#include <log.hpp>
|
||||
#include <server/Cgi.hpp>
|
||||
@ -64,7 +66,6 @@ void Client::parse(void) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!_route || _route == not_nullptr) {
|
||||
_request->setMethod("404");
|
||||
return;
|
||||
@ -83,9 +84,15 @@ void Client::parse(void) {
|
||||
bool Client::requestParsed(void) {
|
||||
if (_request == not_nullptr)
|
||||
return false;
|
||||
if (_request->getCgi() != not_nullptr)
|
||||
if (_request->getCgi() != not_nullptr) {
|
||||
if (!_request->getCgi()->isProcessed())
|
||||
return false;
|
||||
else if (_request->getCgi()->isTimedout())
|
||||
return true;
|
||||
else if (!(PfdManager::get(_request->getCgi()->getId())->revents &
|
||||
_request->getCgi()->event()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -6,11 +6,12 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "server/AResource.hpp"
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <server/default.hpp>
|
||||
@ -129,6 +130,9 @@ void Server::_handle_resource(size_t i) {
|
||||
if (res == not_nullptr)
|
||||
return;
|
||||
|
||||
if (res->type() == CGI && static_cast<Cgi *>(res)->isTimedout()) {
|
||||
return;
|
||||
}
|
||||
if (!res->isProcessed() && res->isReady()) {
|
||||
_log->debug("processing resource");
|
||||
res->process();
|
||||
|
Reference in New Issue
Block a user