mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🔨」 fix: fixed cgi config handling to match subjet
also changed cgi behavior to match the new config
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<std::string, 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 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<std::string> *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<std::string, std::string> *_cgi;
|
||||
std::string _root;
|
||||
std::string _up_root;
|
||||
std::string _index;
|
||||
std::vector<std::string> *_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<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
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<std::string, std::string> _envp; // The envp filled with _initEnvp
|
||||
webserv::config::Route *_conf; // The configuration for the route used
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -9,32 +9,29 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <log.hpp>
|
||||
#include "node/ANode.hpp"
|
||||
#include "node/default.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <log.hpp>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
|
||||
using namespace webserv::config;
|
||||
|
||||
std::map<std::string, std::string> *Route::_parseCGI(toml::ANode *table) {
|
||||
std::map<std::string, std::string> *cgi =
|
||||
new std::map<std::string, std::string>;
|
||||
void *val;
|
||||
std::vector<std::string> *Route::_parseCGI(toml::ANode *table) {
|
||||
std::vector<std::string> *cgi = new std::vector<std::string>;
|
||||
|
||||
for (std::map<std::string, toml::ANode *>::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<std::string *>(val);
|
||||
for (auto it = prange(table->getArray())) {
|
||||
if ((*it)->type() == toml::STRING)
|
||||
cgi->push_back(*static_cast<std::string *>((*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<toml::ANode *> *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<std::string *>(val));
|
||||
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())
|
||||
_cgi = _parseCGI(it->second);
|
||||
else
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<char *>(_cgi_path.c_str()),
|
||||
const_cast<char *>(_script_path.c_str()), NULL};
|
||||
char * argv[] = {const_cast<char *>(_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;
|
||||
|
Reference in New Issue
Block a user