From 8762963315a8c4e55d58c7bf0f747d4fe4a93b57 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 16 May 2025 16:59:31 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20genEnv=20done=20and=20process=20in=20progress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Route.hpp | 20 +++++++- includes/requests/Cgi.hpp | 16 +++++-- includes/server/ResourceManager.hpp | 4 +- src/requests_handling/Cgi.cpp | 74 ++++++++++++++--------------- 4 files changed, 68 insertions(+), 46 deletions(-) diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 9bb02fe..d49e5ff 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/09 11:06:38 by adjoly ### ########.fr */ +/* Updated: 2025/05/16 12:08:03 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ #include #include #include +#include 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 *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; } diff --git a/includes/requests/Cgi.hpp b/includes/requests/Cgi.hpp index 8feb745..d78490b 100644 --- a/includes/requests/Cgi.hpp +++ b/includes/requests/Cgi.hpp @@ -6,7 +6,7 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 _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 _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 diff --git a/includes/server/ResourceManager.hpp b/includes/server/ResourceManager.hpp index ccc6c15..d9db7c7 100644 --- a/includes/server/ResourceManager.hpp +++ b/includes/server/ResourceManager.hpp @@ -6,7 +6,7 @@ /* 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); _res.erase(it); } - // TODO throw or not - to see + // NOTE: throw or not - to see } protected: diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 488e705..ca48219 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,41 +6,52 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include +#include +#include + +#include #include 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; }