🔨」 fix: fixed cgi config handling to match subjet

also changed cgi behavior to match the new config
This commit is contained in:
2025-05-26 17:26:25 +02:00
parent bebdb4e95b
commit e02b475a73
5 changed files with 41 additions and 81 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,39 +31,17 @@ class Route {
~Route(void); ~Route(void);
bool getDirList(void) { return _dirlist; } bool getDirList(void) { return _dirlist; }
/* bool getCookies(void) { return _cookies; } */
bool getRedirect(void) { return _redirect; } bool getRedirect(void) { return _redirect; }
int32_t getMaxBody(void) { return _max_body; } int32_t getMaxBody(void) { return _max_body; }
std::string getRootDir(void) { return _root; } std::string getRootDir(void) { return _root; }
std::string getUpRoot(void) { return _up_root; } std::string getUpRoot(void) { return _up_root; }
std::string getIndex(void) { return _index; } std::string getIndex(void) { return _index; }
std::map<std::string, std::string> *getCgi(void) { return _cgi; } std::vector<std::string> *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 * getMethods(void) { return _methods; }
protected: protected:
private: private:
bool _dirlist; bool _dirlist;
/* bool _cookies; */
bool _redirect; bool _redirect;
int32_t _max_body; int32_t _max_body;
@ -71,7 +49,7 @@ class Route {
std::string _root; std::string _root;
std::string _up_root; std::string _up_root;
std::string _index; std::string _index;
std::map<std::string, std::string> *_cgi; std::vector<std::string> *_cgi;
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET,
/// 1: POST, 2: DELETE /// 1: POST, 2: DELETE
@ -84,7 +62,7 @@ class Route {
* *
* @return A pointer to a map of cgi * @return A pointer to a map of cgi
*/ */
std::map<std::string, std::string> *_parseCGI(toml::ANode *); std::vector<std::string> *_parseCGI(toml::ANode *);
/** /**
* @brief Can be used to parse a table of error pages * @brief Can be used to parse a table of error pages

View File

@ -6,7 +6,7 @@
/* 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/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); void _setEnv(const std::string, std::string);
std::string _script_path; // The full path of the script to be executed std::string _script_path; // The full path of the script to be executed
std::string _cgi_path;
std::map<std::string, std::string> _envp; // The envp filled with _initEnvp std::map<std::string, std::string> _envp; // The envp filled with _initEnvp
webserv::config::Route *_conf; // The configuration for the route used webserv::config::Route *_conf; // The configuration for the route used

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */ /* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */
/* Updated: 2025/04/22 14:27:31 by adjoly ### ########.fr */ /* Updated: 2025/05/26 10:16:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -9,32 +9,29 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include <log.hpp> #include "node/ANode.hpp"
#include "node/default.hpp" #include "node/default.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <log.hpp>
#include <map> #include <map>
#include <sstream>
#include <string> #include <string>
using namespace webserv::config; using namespace webserv::config;
std::map<std::string, std::string> *Route::_parseCGI(toml::ANode *table) { std::vector<std::string> *Route::_parseCGI(toml::ANode *table) {
std::map<std::string, std::string> *cgi = std::vector<std::string> *cgi = new std::vector<std::string>;
new std::map<std::string, std::string>;
void *val;
for (std::map<std::string, toml::ANode *>::iterator it = for (auto it = prange(table->getArray())) {
table->getTable()->begin(); if ((*it)->type() == toml::STRING)
it != table->getTable()->end(); it++) { cgi->push_back(*static_cast<std::string *>((*it)->getValue()));
val = accessValue(it->first, toml::STRING, table, _log); else {
if (val != not_nullptr) { std::stringstream str;
if (cgi->find(it->first) != cgi->end()) str << "Was expecting a : " << toml::nodeTypeToStr(toml::STRING);
continue; str << " but got a : " << toml::nodeTypeToStr((*it)->type());
else _log->warn(str.str());
(*cgi)[it->first] = *static_cast<std::string *>(val);
} }
} }
return cgi; return cgi;
} }
@ -58,8 +55,7 @@ void Route::_parseMethods(std::vector<toml::ANode *> *table) {
} }
} }
Route::Route(toml::ANode *table) Route::Route(toml::ANode *table) : _max_body(10485760) {
: _max_body(10485760) {
void *val; void *val;
bool found; bool found;
@ -105,12 +101,11 @@ Route::Route(toml::ANode *table)
#else #else
_root = "./html"; _root = "./html";
#endif #endif
val = val = accessValue("client_max_body_size", toml::STRING, _table, _log);
accessValue("client_max_body_size", toml::STRING, _table, _log);
if (val != not_nullptr) if (val != not_nullptr)
_max_body = _parseSize(*static_cast<std::string *>(val)); _max_body = _parseSize(*static_cast<std::string *>(val));
std::map<std::string, toml::ANode *>::iterator it = std::map<std::string, toml::ANode *>::iterator it =
_table->accessIt("cgi", toml::TABLE, found); _table->accessIt("cgi", toml::ARRAY, found);
if (found == true && it != _table->getTable()->end()) if (found == true && it != _table->getTable()->end())
_cgi = _parseCGI(it->second); _cgi = _parseCGI(it->second);
else else

View File

@ -6,7 +6,7 @@
/* 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/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) { : _executed(false), _is_post(false), _conf(conf), _request(req) {
log("", "Cgi", "GET constructor called"); log("", "Cgi", "GET constructor called");
_initEnvp(); _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(); _prep();
} }
@ -45,16 +40,9 @@ Cgi::Cgi(http::Post *req, config::Route *conf)
: _executed(false), _is_post(true), _conf(conf), _request(req) { : _executed(false), _is_post(true), _conf(conf), _request(req) {
log("", "Cgi", "POST constructor called"); log("", "Cgi", "POST constructor called");
_initEnvp(); _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(); _prep();
CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]); CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]);
ResourceManager::append(in); ResourceManager::append(in);
_fd->fd = _stdout_pipe[STDIN_FILENO];
_fd->events = POLLIN;
} }
Cgi::~Cgi(void) { log("", "Cgi", "destructor called"); } 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:"); throw std::runtime_error("stdin pipe failed for cgi D:");
if (pipe(_stdout_pipe) == -1) if (pipe(_stdout_pipe) == -1)
throw std::runtime_error("stdout pipe failed for cgi D:"); throw std::runtime_error("stdout pipe failed for cgi D:");
_fd->fd = _stdout_pipe[STDIN_FILENO];
_fd->events = POLLIN;
} }
void Cgi::_initEnvp(void) { void Cgi::_initEnvp(void) {
@ -131,10 +121,9 @@ void Cgi::process(void) {
pid_t forkPid; pid_t forkPid;
forkPid = fork(); forkPid = fork();
if (forkPid < 0) { if (forkPid < 0)
throw; throw std::runtime_error("fork failed D:");
// TODO: fork fail else if (forkPid == 0) {
} else if (forkPid == 0) {
dup2(_stdin_pipe[0], STDIN_FILENO); dup2(_stdin_pipe[0], STDIN_FILENO);
close(_stdin_pipe[0]); close(_stdin_pipe[0]);
close(_stdin_pipe[1]); close(_stdin_pipe[1]);
@ -143,11 +132,10 @@ void Cgi::process(void) {
close(_stdout_pipe[0]); close(_stdout_pipe[0]);
close(_stdout_pipe[1]); close(_stdout_pipe[1]);
char *argv[] = {const_cast<char *>(_cgi_path.c_str()), char * argv[] = {const_cast<char *>(_script_path.c_str()), NULL};
const_cast<char *>(_script_path.c_str()), NULL};
char **env = _genEnv(); char **env = _genEnv();
if (execve(_cgi_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 << "how did you do that ???? : ";
str << errno; str << errno;