diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index d49e5ff..a59e6e0 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/05/16 12:08:03 by adjoly ### ########.fr */ +/* Updated: 2025/05/26 17:02:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,48 +30,26 @@ class Route { Route(toml::ANode *); ~Route(void); - bool getDirList(void) { return _dirlist; } - /* bool getCookies(void) { return _cookies; } */ - bool getRedirect(void) { return _redirect; } - - int32_t getMaxBody(void) { return _max_body; } - - std::string getRootDir(void) { return _root; } - std::string getUpRoot(void) { return _up_root; } - std::string getIndex(void) { return _index; } - std::map *getCgi(void) { return _cgi; } - std::string getCgiPath(const std::string file) { - if (_cgi == not_nullptr) - return ""; - size_t pos = file.find_last_of("."); - - if (pos == file.length()) - return ""; - std::string ext = file.substr(pos + 1); - - for (auto it = prange(_cgi)) { - if (ext == it->first) { - return it->second; - } - } - return ""; - } - - - bool *getMethods(void) { return _methods; } + bool getDirList(void) { return _dirlist; } + bool getRedirect(void) { return _redirect; } + int32_t getMaxBody(void) { return _max_body; } + std::string getRootDir(void) { return _root; } + std::string getUpRoot(void) { return _up_root; } + std::string getIndex(void) { return _index; } + std::vector *getCgi(void) { return _cgi; } + bool * getMethods(void) { return _methods; } protected: private: bool _dirlist; - /* bool _cookies; */ bool _redirect; int32_t _max_body; - std::string _root; - std::string _up_root; - std::string _index; - std::map *_cgi; + std::string _root; + std::string _up_root; + std::string _index; + std::vector *_cgi; bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, /// 1: POST, 2: DELETE @@ -84,7 +62,7 @@ class Route { * * @return A pointer to a map of cgi */ - std::map *_parseCGI(toml::ANode *); + std::vector *_parseCGI(toml::ANode *); /** * @brief Can be used to parse a table of error pages diff --git a/includes/server/Cgi.hpp b/includes/server/Cgi.hpp index ea6ae17..4914112 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/24 11:18:49 by adjoly ### ########.fr */ +/* Updated: 2025/05/26 17:19:01 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,7 +74,6 @@ class Cgi : public server::AClientResource { void _setEnv(const std::string, std::string); std::string _script_path; // The full path of the script to be executed - std::string _cgi_path; std::map _envp; // The envp filled with _initEnvp webserv::config::Route *_conf; // The configuration for the route used diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 1462958..ebfeb1f 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#include "node/ANode.hpp" #include "node/default.hpp" #include +#include #include +#include #include - using namespace webserv::config; -std::map *Route::_parseCGI(toml::ANode *table) { - std::map *cgi = - new std::map; - void *val; +std::vector *Route::_parseCGI(toml::ANode *table) { + std::vector *cgi = new std::vector; - for (std::map::iterator it = - table->getTable()->begin(); - it != table->getTable()->end(); it++) { - val = accessValue(it->first, toml::STRING, table, _log); - if (val != not_nullptr) { - if (cgi->find(it->first) != cgi->end()) - continue; - else - (*cgi)[it->first] = *static_cast(val); + for (auto it = prange(table->getArray())) { + if ((*it)->type() == toml::STRING) + cgi->push_back(*static_cast((*it)->getValue())); + else { + std::stringstream str; + str << "Was expecting a : " << toml::nodeTypeToStr(toml::STRING); + str << " but got a : " << toml::nodeTypeToStr((*it)->type()); + _log->warn(str.str()); } } - return cgi; } @@ -58,8 +55,7 @@ void Route::_parseMethods(std::vector *table) { } } -Route::Route(toml::ANode *table) - : _max_body(10485760) { +Route::Route(toml::ANode *table) : _max_body(10485760) { void *val; bool found; @@ -105,12 +101,11 @@ Route::Route(toml::ANode *table) #else _root = "./html"; #endif - val = - accessValue("client_max_body_size", toml::STRING, _table, _log); + val = accessValue("client_max_body_size", toml::STRING, _table, _log); if (val != not_nullptr) _max_body = _parseSize(*static_cast(val)); std::map::iterator it = - _table->accessIt("cgi", toml::TABLE, found); + _table->accessIt("cgi", toml::ARRAY, found); if (found == true && it != _table->getTable()->end()) _cgi = _parseCGI(it->second); else diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 2367744..403363b 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/24 11:17:22 by adjoly ### ########.fr */ +/* Updated: 2025/05/26 17:22:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,11 +33,6 @@ Cgi::Cgi(http::Get *req, config::Route *conf) : _executed(false), _is_post(false), _conf(conf), _request(req) { log("➕", "Cgi", "GET constructor called"); _initEnvp(); - _cgi_path = _conf->getCgiPath(req->getTarget()); - 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(); } @@ -45,16 +40,9 @@ Cgi::Cgi(http::Post *req, config::Route *conf) : _executed(false), _is_post(true), _conf(conf), _request(req) { log("➕", "Cgi", "POST constructor called"); _initEnvp(); - _cgi_path = _conf->getCgiPath(req->getTarget()); - 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) { log("➖", "Cgi", "destructor called"); } @@ -65,6 +53,8 @@ void Cgi::_prep(void) { 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:"); + _fd->fd = _stdout_pipe[STDIN_FILENO]; + _fd->events = POLLIN; } void Cgi::_initEnvp(void) { @@ -119,7 +109,7 @@ char **Cgi::_genEnv(void) { for (auto it = range(_envp), i++) { std::string str = it->first + "=" + it->second; - char *tmp = new char[str.size() + 1]; + char * tmp = new char[str.size() + 1]; std::strcpy(tmp, str.c_str()); newEnv[i] = tmp; } @@ -131,10 +121,9 @@ void Cgi::process(void) { pid_t forkPid; forkPid = fork(); - if (forkPid < 0) { - throw; - // TODO: fork fail - } else if (forkPid == 0) { + if (forkPid < 0) + throw std::runtime_error("fork failed D:"); + else if (forkPid == 0) { dup2(_stdin_pipe[0], STDIN_FILENO); close(_stdin_pipe[0]); close(_stdin_pipe[1]); @@ -143,11 +132,10 @@ void Cgi::process(void) { close(_stdout_pipe[0]); close(_stdout_pipe[1]); - char *argv[] = {const_cast(_cgi_path.c_str()), - const_cast(_script_path.c_str()), NULL}; + char * argv[] = {const_cast(_script_path.c_str()), NULL}; char **env = _genEnv(); - if (execve(_cgi_path.c_str(), argv, env) == -1) { + if (execve(_script_path.c_str(), argv, env) == -1) { std::stringstream str; str << "how did you do that ???? : "; str << errno;