」 merge(Cgi): added cgi handling

This commit is contained in:
Adam
2025-05-21 09:56:35 +02:00
committed by GitHub
8 changed files with 224 additions and 36 deletions

View File

@ -12,7 +12,7 @@ port = 8080
[server.location./] [server.location./]
methods = { "GET" } methods = { "GET" }
root = "/sgoinfre/goinfre/Perso/mmoussou" root = "/home/adjoly"
dirlist = true dirlist = true
client_max_body_size = "10M" client_max_body_size = "10M"
index = "banger.html" index = "banger.html"

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/09 11:06:38 by adjoly ### ########.fr */ /* Updated: 2025/05/16 12:08:03 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,7 @@
#include <node/default.hpp> #include <node/default.hpp>
#include <string> #include <string>
#include <tomlpp.hpp> #include <tomlpp.hpp>
#include <webserv.hpp>
namespace webserv { namespace webserv {
namespace config { namespace config {
@ -39,6 +40,23 @@ class Route {
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::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 *getMethods(void) { return _methods; }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */ /* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */
/* Updated: 2025/05/08 12:05:38 by adjoly ### ########.fr */ /* Updated: 2025/05/15 12:02:30 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,5 +18,6 @@
#define SAMPLE_CONF_PATH "/etc/webserv/default.conf" #define SAMPLE_CONF_PATH "/etc/webserv/default.conf"
#endif #endif
#define WEBSRV_VERSION "v0.2" #define WEBSRV_VERSION "v0.2"
#define WEBSRV_NAME "webserv"
bool help(int, char **); bool help(int, char **);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/05/02 13:58:52 by mmoussou ### ########.fr */ /* Updated: 2025/05/15 12:12:47 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -3,38 +3,93 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Cgi.hpp :+: :+: :+: */ /* Cgi.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/04/30 09:36:02 by adjoly ### ########.fr */ /* Updated: 2025/05/20 20:01:38 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <requests/ARequest.hpp> #include "server/AResource.hpp"
#include <config/Route.hpp>
#include <config/default.hpp> #include <config/default.hpp>
#include <cstdio>
#include <requests/ARequest.hpp>
#include <map> #include <map>
#include <string> #include <string>
#include <unistd.h>
namespace webserv { namespace webserv {
class Cgi { class Cgi : public server::AClientResource {
public: public:
Cgi(http::ARequest *, config::Server *); Cgi(http::ARequest *, config::Route *);
~Cgi(void); ~Cgi(void);
std::string getEnv(std::string &); int getFdOut(void) const { return _stdout_pipe[0]; }
void setEnv(std::string &, std::string);
/**
* @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
*/
void process(void); void process(void);
/**
* @brief Can be used to check if the Cgi have been executed
*
* @note Need to be used after the process() function and checked before
* using str()
*/
bool isProcessed(void) { return _executed; }
/**
* @brief Can be used to get the output of the Cgi
*
* @note Need to be used after the outfd is in POLLIN state
*/
std::string str(void);
protected: protected:
private: private:
bool _prepared;
bool _executed;
void _initEnvp(void); void _initEnvp(void);
std::map<std::string, std::string> _envp; /**
config::Server *_conf; * @brief Can be used to convert the _envp to a char** usable in execve
http::IMessage *_request; *
* @return A newly allocated char** with the env from _envp
*/
char **_genEnv(void);
std::string _getEnv(std::string &) const;
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
config::Route *_conf; // The configuration for the route used
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 _stdout_pipe[2]; // The pipefd for the stdout of the cgi
}; };
}; // namespace webserv }; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */ /* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
/* Updated: 2025/05/13 10:14:27 by adjoly ### ########.fr */ /* Updated: 2025/05/16 10:18:25 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,7 +77,7 @@ class ResourceManager {
delete (*it); delete (*it);
_res.erase(it); _res.erase(it);
} }
// TODO throw or not - to see // NOTE: throw or not - to see
} }
protected: protected:

View File

@ -3,26 +3,76 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* Cgi.cpp :+: :+: :+: */ /* Cgi.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by adjoly #+# #+# */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/04/30 09:49:32 by adjoly ### ########.fr */ /* Updated: 2025/05/21 09:50:23 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "requests/default.hpp" #include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <help.hpp>
#include <requests/Cgi.hpp>
#include <fcntl.h>
#include <sstream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace webserv; using namespace webserv;
Cgi::Cgi(http::ARequest *req, config::Server *conf) : _conf(conf), _request(req) { // WARN: construtor will probably be changed and practicly do nothing
Cgi::Cgi(http::ARequest *req, config::Route *conf)
: _prepared(false), _executed(false), _conf(conf), _request(req) {
_initEnvp(); _initEnvp();
_cgi_path = _conf->getCgiPath(req->getTarget());
if (_cgi_path == "") {
throw;
// TODO: need to make something probably will be checked before by
// client
}
} }
void Cgi::_initEnvp(void) { void Cgi::_initEnvp(void) {
//_envp[] = ""; std::stringstream str;
str << WEBSRV_NAME << "/" << WEBSRV_VERSION;
_setEnv("SERVER_SOFTWARE", str.str());
str.clear();
_setEnv("SERVER_NAME", _request->getHeader("Host"));
_setEnv("SERVER_PROTOCOL", _request->getProtocol());
// setEnv("SERVER_PORT", _request->get); // TODO: need to get the port by a
// way i dont know yet
_setEnv("GATEWAY_INTERFACE", "CGI/1.1");
// setEnv("PATH_TRANSLATED", ); // TODO: wtf should i put here i dont fcking
// know
// setEnv("PATH_INFO", ); // TODO: wut make no sense
str << _request->getBody().length();
_setEnv("CONTENT_LENGH", str.str());
str.clear();
_setEnv("CONTENT_TYPE", _request->getHeader("Content-Type"));
// setEnv("REMOTE_ADDR", _request->get) // TODO: don't have it yet need to
// be passed to the requset :sob:
_setEnv("HTTP_ACCEPT", _request->getHeader("Accept"));
_setEnv("HTTP_ACCEPT_LANGUAGE", _request->getHeader("Accept-Language"));
_setEnv("HTTP_COOKIE", _request->getHeader("Cookie"));
_setEnv("HTTP_HOST", _request->getHeader("Host"));
_setEnv("HTTP_REFERER", _request->getHeader("Referer"));
_setEnv("HTTP_USER_AGENT", _request->getHeader("User-Agent"));
_setEnv("SCRIPT_NAME", _request->getTarget());
_setEnv("QUERY_STRING", _request->getUrl().getQueryString());
} }
std::string Cgi::getEnv(std::string &key) { std::string Cgi::_getEnv(std::string &key) const {
auto it = _envp.find(key); auto it = _envp.find(key);
if (it != _envp.end()) { if (it != _envp.end()) {
return it->second; return it->second;
@ -30,20 +80,83 @@ std::string Cgi::getEnv(std::string &key) {
return ""; return "";
} }
void Cgi::setEnv(std::string &key, std::string value) { void Cgi::_setEnv(const std::string key, std::string value) {
_envp[key] = value; _envp[key] = value;
} }
void Cgi::process() { char **Cgi::_genEnv(void) {
int pipefd[2]; char **newEnv = new char *[_envp.size() + 1];
int forkPid; size_t i = 0;
if (pipe(pipefd) <= -1) { for (auto it = range(_envp), i++) {
//throw error std::string str = it->first + "=" + it->second;
char *tmp = new char[str.size() + 1];
std::strcpy(tmp, str.c_str());
newEnv[i] = tmp;
} }
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;
forkPid = fork(); forkPid = fork();
if (forkPid == 0) { if (forkPid < 0) {
// in fork throw;
// TODO: fork fail
} else if (forkPid == 0) {
dup2(_stdin_pipe[0], STDIN_FILENO);
close(_stdin_pipe[0]);
close(_stdin_pipe[1]);
dup2(_stdout_pipe[1], STDOUT_FILENO);
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 **env = _genEnv();
if (execve(_cgi_path.c_str(), argv, env) == -1) {
std::stringstream str;
str << "how did you do that ???? : ";
str << errno;
_log->error(str.str());
for (int i = 0; env[i] != NULL; i++)
delete env[i];
delete env;
exit(EXIT_FAILURE);
} }
}
close(_stdin_pipe[0]);
close(_stdout_pipe[1]);
waitpid(forkPid, NULL, 0);
_executed = true;
}
std::string Cgi::str(void) {
std::string str;
int max = _conf->getMaxBody();
char buffer[1024];
while (max) {
ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer));
if (count > 0)
str.append(buffer);
else
break;
}
str.append("\0");
return str;
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/05/02 13:30:50 by mmoussou ### ########.fr */ /* Updated: 2025/05/19 15:11:00 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -187,7 +187,8 @@ void Server::_run(void) {
_log->error("client does not exist"); _log->error("client does not exist");
continue; continue;
} }
if (client->requestParsed() == true && !client->isReadyToClose()) { if (client->requestParsed() == true &&
!client->isReadyToClose()) {
client->answer(); client->answer();
continue; continue;
} }