🏗️」 wip: genEnv done and process in progress

This commit is contained in:
2025-05-16 16:59:31 +02:00
parent 67cf7a1c3a
commit 8762963315
4 changed files with 68 additions and 46 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <tomlpp.hpp>
#include <webserv.hpp>
namespace webserv {
namespace config {
@ -39,6 +40,23 @@ class Route {
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; }

View File

@ -6,7 +6,7 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/05/15 13:45:20 by adjoly ### ########.fr */
/* Updated: 2025/05/16 12:26:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,11 +36,19 @@ class Cgi : public server::AClientResource {
private:
void _initEnvp(void);
/**
* @brief Can be used to convert the _envp to a char** usable in execve
*
* @return A newly allocated char** with the env from _envp
*/
char **_genEnv(void);
std::map<std::string, std::string> _envp;
config::Route *_conf;
http::ARequest *_request;
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
};
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
_res.erase(it);
}
// TODO throw or not - to see
// NOTE: throw or not - to see
}
protected:

View File

@ -6,41 +6,52 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/05/15 12:46:38 by adjoly ### ########.fr */
/* Updated: 2025/05/16 12:30:10 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "requests/Cgi.hpp"
#include "help.hpp"
#include <cstddef>
#include <cstring>
#include <help.hpp>
#include <requests/Cgi.hpp>
#include <fcntl.h>
#include <string>
using namespace webserv;
// WARN: construtor will probably be changed and practicly do nothing
Cgi::Cgi(http::ARequest *req, config::Route *conf)
: _conf(conf), _request(req) {
_initEnvp();
_cgi_path = _conf->getCgiPath(req->getTarget());
if (_cgi_path == "") {
// TODO: need to make something
}
}
void Cgi::_initEnvp(void) {
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
// 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
// setEnv("PATH_TRANSLATED", ); // TODO: wtf should i put here i dont fcking
// know
// setEnv("PATH_INFO", ); // TODO: wut make no sense
str.clear();
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("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"));
@ -65,42 +76,27 @@ void Cgi::setEnv(const std::string key, std::string value) {
_envp[key] = value;
}
char **Cgi::_genEnv(void) {
char **newEnv = new char *[_envp.size() + 1];
size_t i = 0;
for (auto it = range(_envp), i++) {
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::process() {
int pipefd[2];
int forkPid;
if (pipe(pipefd) <= -1) {
// throw error
// TODO: error handling
}
forkPid = fork();
if (forkPid == 0) {
int fd = open("kanel/cheminfichier",
O_RDONLY); // chemin vers ce que je dois ouvrir
if (fd == -1)
// throw erreur
dup2(fd, 0);
dup2(pipefd[1], 1);
close(fd);
close(pipefd[0]);
close(pipefd[1]);
std::string cgipath = "kanel/chemincgi"; // chemin du cgi
char *envp[] = {getEnv("REQUEST_METHOD"), getEnv("?"), getEnv("TYPE"),
getEnv("LENGTH"), NULL};
execve(cgipath, NULL, env);
// throw si execve echou
} else if (forkPid >= 1) {
close(pipefd[1]);
int nb;
waitpid(forkPid, &nb, 0);
char buffer[4096] // jsp quoi mettre le temps
while (read(pipefd[0], buffer, sizeof(buffer))) {
// tranfert donnees
}
close(pipefd[0]);
} else
throw error;
}