diff --git a/includes/server/Cgi.hpp b/includes/server/Cgi.hpp index 33c32d3..61d1f1d 100644 --- a/includes/server/Cgi.hpp +++ b/includes/server/Cgi.hpp @@ -6,14 +6,17 @@ /* 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 +#include "server/PfdManager.hpp" #include +#include #include +#include #include #include @@ -23,6 +26,8 @@ #include #include #include +#include + namespace webserv { namespace http { class Get; @@ -62,16 +67,25 @@ class Cgi : public server::AClientResource { } if (static_cast(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 diff --git a/includes/server/CgiIn.hpp b/includes/server/CgiIn.hpp index c30cbe9..9b10ccf 100644 --- a/includes/server/CgiIn.hpp +++ b/includes/server/CgiIn.hpp @@ -6,7 +6,7 @@ /* 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"); } 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); diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 8f0021c..fc234dc 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,12 +6,13 @@ /* 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/ResourceManager.hpp" +#include #include #include #include @@ -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); } } diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index c4e6f20..1aab39a 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,7 +6,7 @@ /* 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(); _url = new URL(_target); - std::cout << *_url << std::endl; + // std::cout << *_url << std::endl; /* std::cout << "-- start-line --" << std::endl; diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index 3fb0fee..f59e687 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,7 +6,7 @@ /* 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; 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(_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(), diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 8423da9..47226e2 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* 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; 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(), diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 9e742bb..0cd49d5 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,13 +6,15 @@ /* By: mmoussou #include #include @@ -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; } diff --git a/src/server/ServerHandle.cpp b/src/server/ServerHandle.cpp index b0988c2..110cd3c 100644 --- a/src/server/ServerHandle.cpp +++ b/src/server/ServerHandle.cpp @@ -6,11 +6,12 @@ /* 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 "server/AResource.hpp" #include #include #include @@ -129,6 +130,9 @@ void Server::_handle_resource(size_t i) { if (res == not_nullptr) return; + if (res->type() == CGI && static_cast(res)->isTimedout()) { + return; + } if (!res->isProcessed() && res->isReady()) { _log->debug("processing resource"); res->process();