From f632866a0759fe58bf0adeb53a53fe7510a0edc6 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 8 May 2025 10:51:52 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8E=89=E3=80=8D=20init:=20starte?= =?UTF-8?q?d=20cgi=20handling=20(for=20real=20this=20time)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/Cgi.hpp | 6 ++-- src/requests_handling/Cgi.cpp | 54 +++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/includes/requests/Cgi.hpp b/includes/requests/Cgi.hpp index c50e473..82c022c 100644 --- a/includes/requests/Cgi.hpp +++ b/includes/requests/Cgi.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Cgi.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */ -/* Updated: 2025/04/30 09:36:02 by adjoly ### ########.fr */ +/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ +/* Updated: 2025/05/06 19:34:11 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 6ce13f8..02a8b47 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -3,14 +3,14 @@ /* ::: :::::::: */ /* Cgi.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/24 13:46:34 by adjoly #+# #+# */ -/* Updated: 2025/04/30 09:49:32 by adjoly ### ########.fr */ +/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ +/* Updated: 2025/05/06 19:36:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "requests/default.hpp" +#include using namespace webserv; @@ -18,11 +18,18 @@ Cgi::Cgi(http::ARequest *req, config::Server *conf) : _conf(conf), _request(req) _initEnvp(); } -void Cgi::_initEnvp(void) { - //_envp[] = ""; +void Cgi::_initEnvp(void) +{ + + setEnv("REQUEST_METHOD", _request->getMethod()); //url get ou pour post + setEnv("?", _request->getParamRequest()); //apres le ? + setEnv("TYPE", _request->getType()); //type de requete, genre html + setEnv("LENGTH", _request->getLength()); //taille pour les requetes post + setEnv("SCRIPT", _request->getScript()); //chemin relatif du serveur + setEnv("MORE_INFO", _request->getMoreInfo()); //chemin supplementaire } -std::string Cgi::getEnv(std::string &key) { +char *Cgi::getEnv(std::string &key) { auto it = _envp.find(key); if (it != _envp.end()) { return it->second; @@ -43,7 +50,36 @@ void Cgi::process() { } forkPid = fork(); - if (forkPid == 0) { - // in 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; }