From 75eab5ed52a5f888381dd539c7c5174786901321 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 24 May 2025 11:22:43 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20cgi=20should=20be=20able=20to=20be=20implemented?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/AResource.hpp | 8 ++-- includes/server/Cgi.hpp | 40 +++++++------------- includes/server/CgiIn.hpp | 17 +++++++-- includes/server/FileUpload.hpp | 7 +++- includes/server/ResourceManager.hpp | 3 +- src/main.cpp | 11 +++--- src/requests_handling/Cgi.cpp | 57 +++++++++++++++-------------- 7 files changed, 71 insertions(+), 72 deletions(-) diff --git a/includes/server/AResource.hpp b/includes/server/AResource.hpp index 4e94124..8bfd395 100644 --- a/includes/server/AResource.hpp +++ b/includes/server/AResource.hpp @@ -6,7 +6,7 @@ /* By: mmoussou fd) return true; return false; } @@ -34,12 +34,10 @@ class AClientResource { struct pollfd getFileDescriptor(void) const { return *_fd; } virtual clientResType type(void) const = 0; - int getId(void) const { return _res_id; } + int getId(void) const { return _fd->fd; } protected: struct pollfd *_fd; - - int _res_id; }; } // namespace server diff --git a/includes/server/Cgi.hpp b/includes/server/Cgi.hpp index a40745a..ea6ae17 100644 --- a/includes/server/Cgi.hpp +++ b/includes/server/Cgi.hpp @@ -6,7 +6,7 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/05/23 18:24:29 by adjoly ### ########.fr */ +/* Updated: 2025/05/24 11:18:49 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,27 +25,15 @@ namespace webserv { namespace server { +#define PIPE_READ 0 +#define PIPE_WRITE 1 + class Cgi : public server::AClientResource { public: - Cgi(webserv::http::Get *, webserv::config::Route *, int); - Cgi(webserv::http::Post *, webserv::config::Route *, int); + Cgi(webserv::http::Get *, webserv::config::Route *); + Cgi(webserv::http::Post *, webserv::config::Route *); ~Cgi(void); - int getFdOut(void) const { return _stdout_pipe[0]; } - - /** - * @brief Can be used to prepare the Cgi execution - * - * @note To be used after the main fd is in POLLOUT state - */ - void prepare(void); - - /** - * @brief Can be used to know if the prepare function has already been - * called - */ - bool isPrepared(void) { return _prepared; } - /** * @brief Can be used to process the Cgi script */ @@ -68,12 +56,12 @@ class Cgi : public server::AClientResource { protected: private: - bool _prepared; bool _executed; + bool _is_post; void _initEnvp(void); - void _prepPost(void); + void _prep(void); /** * @brief Can be used to convert the _envp to a char** usable in execve @@ -89,14 +77,14 @@ class Cgi : public server::AClientResource { std::string _cgi_path; std::map _envp; // The envp filled with _initEnvp - webserv::config::Route *_conf; // The configuration for the route used - webserv::http::ARequest *_request; // The requests that will be used for the cgi + webserv::config::Route *_conf; // The configuration for the route used + webserv::http::ARequest + *_request; // The requests that will be used for the cgi - int _stdin_pipe[2]; // The pipefd for the stdin of the cgi + 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 - - int _in_res; // The id of the stdin resource }; -}; // namespace http +}; // namespace server }; // namespace webserv diff --git a/includes/server/CgiIn.hpp b/includes/server/CgiIn.hpp index cfe0218..a0b0685 100644 --- a/includes/server/CgiIn.hpp +++ b/includes/server/CgiIn.hpp @@ -6,26 +6,35 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ -/* Updated: 2025/05/23 12:51:22 by adjoly ### ########.fr */ +/* Updated: 2025/05/24 11:15:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include "server/AResource.hpp" +#include namespace webserv { namespace server { -class CgiIn: public AClientResource { +class CgiIn : public AClientResource { public: - CgiIn(int id) { _res_id = id; } - ~CgiIn(void) {} + CgiIn(std::string body, int id) : _body(body) { + log("➕", "CgiIn", "constructor called"); + _fd->fd = id; + _fd->events = POLLOUT; + } + ~CgiIn(void) { log("➖", "CgiIn", "destructor called"); } + void send(void) { + // TODO: send the body + } clientResType type(void) const { return CGI_IN; } protected: private: + std::string _body; }; } // namespace server diff --git a/includes/server/FileUpload.hpp b/includes/server/FileUpload.hpp index fe593f9..019daed 100644 --- a/includes/server/FileUpload.hpp +++ b/includes/server/FileUpload.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ -/* Updated: 2025/05/13 18:56:25 by adjoly ### ########.fr */ +/* Updated: 2025/05/24 11:06:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,10 @@ namespace server { class FileUpload : public AClientResource { public: - FileUpload(int id) { _res_id = id; } + FileUpload(int id) { + _fd->fd = id; + _fd->events = POLLOUT; + } ~FileUpload(void) {} clientResType type(void) const { return UP_FILE; } diff --git a/includes/server/ResourceManager.hpp b/includes/server/ResourceManager.hpp index d9db7c7..d5280a2 100644 --- a/includes/server/ResourceManager.hpp +++ b/includes/server/ResourceManager.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */ -/* Updated: 2025/05/16 10:18:25 by adjoly ### ########.fr */ +/* Updated: 2025/05/24 11:00:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,6 @@ class ResourceManager { delete (*it); _res.erase(it); } - // NOTE: throw or not - to see } protected: diff --git a/src/main.cpp b/src/main.cpp index 9204f96..814b1fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include +#include #include #include #include #include #include -#include namespace webserv { -Logger *_log = not_nullptr; -} +Logger *_log = not_nullptr; +std::vector server::ResourceManager::_res; +} // namespace webserv int _sig = 0; @@ -66,7 +67,7 @@ int main(int ac, char **av) { std::string str; if (ac < 2) { str = SAMPLE_CONF_PATH; - } else { + } else { str = av[1]; } conf = new config::Config(str); diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index ee220ab..2367744 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,7 +6,7 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/05/23 18:26:45 by adjoly ### ########.fr */ +/* Updated: 2025/05/24 11:17:22 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -28,32 +29,42 @@ using namespace webserv::server; // WARN: construtor will probably be changed and practicly do nothing -Cgi::Cgi(http::Get *req, config::Route *conf, int id) - : _prepared(false), _executed(false), _conf(conf), _request(req) { +Cgi::Cgi(http::Get *req, config::Route *conf) + : _executed(false), _is_post(false), _conf(conf), _request(req) { + log("➕", "Cgi", "GET constructor called"); _initEnvp(); - _res_id = id; _cgi_path = _conf->getCgiPath(req->getTarget()); - if (_cgi_path == "") { - throw; - // TODO: need to make something probably will be checked before by - // client - } + if (_cgi_path == "") + throw std::runtime_error("Executable path does not exist"); + if (!access(_cgi_path.c_str(), X_OK)) + throw std::runtime_error("Executable is not executable " + _cgi_path); + _prep(); } -Cgi::Cgi(http::Post *req, config::Route *conf, int id) - : _prepared(false), _executed(false), _conf(conf), _request(req) { +Cgi::Cgi(http::Post *req, config::Route *conf) + : _executed(false), _is_post(true), _conf(conf), _request(req) { + log("➕", "Cgi", "POST constructor called"); _initEnvp(); - _res_id = id; _cgi_path = _conf->getCgiPath(req->getTarget()); - if (_cgi_path == "") { - throw; - // TODO: need to make something probably will be checked before by - // client - } + if (_cgi_path == "") + throw std::runtime_error(""); + if (!access(_cgi_path.c_str(), X_OK)) + throw std::runtime_error("Executable is not executable " + _cgi_path); + _prep(); + CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]); + ResourceManager::append(in); + _fd->fd = _stdout_pipe[STDIN_FILENO]; + _fd->events = POLLIN; } -Cgi::~Cgi(void) { +Cgi::~Cgi(void) { log("➖", "Cgi", "destructor called"); } +void Cgi::_prep(void) { + if (_is_post) + if (pipe(_stdin_pipe) == -1) + throw std::runtime_error("stdin pipe failed for cgi D:"); + if (pipe(_stdout_pipe) == -1) + throw std::runtime_error("stdout pipe failed for cgi D:"); } void Cgi::_initEnvp(void) { @@ -116,16 +127,6 @@ char **Cgi::_genEnv(void) { return newEnv; } -void Cgi::prepare(void) { - if (pipe(_stdin_pipe) == -1 && pipe(_stdout_pipe) == -1) { - throw; - // TODO: need to make a better throw - } - _fd->fd = _stdin_pipe[1]; - _fd->events = POLLOUT; - _prepared = true; -} - void Cgi::process(void) { pid_t forkPid;