From 67cf7a1c3ab6693c5b32479e975a129663fd6a4e Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 15 May 2025 14:25:45 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20started=20working=20on=20cgi=20finally?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 2 +- includes/help.hpp | 3 +- includes/requests/ARequest.hpp | 2 +- includes/requests/Cgi.hpp | 24 ++++++---- src/requests_handling/Cgi.cpp | 85 ++++++++++++++++++++++++++++------ src/server/Server.cpp | 7 +-- 6 files changed, 94 insertions(+), 29 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index d031313..ac1d78c 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -12,7 +12,7 @@ port = 8080 [server.location./] methods = { "GET" } -root = "/sgoinfre/goinfre/Perso/mmoussou" +root = "/home/adjoly" dirlist = true client_max_body_size = "10M" index = "banger.html" diff --git a/includes/help.hpp b/includes/help.hpp index 6c8da1f..3ec8d7d 100644 --- a/includes/help.hpp +++ b/includes/help.hpp @@ -6,7 +6,7 @@ /* 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" #endif #define WEBSRV_VERSION "v0.2" +#define WEBSRV_NAME "webserv" bool help(int, char **); diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp index 925af3f..61ea316 100644 --- a/includes/requests/ARequest.hpp +++ b/includes/requests/ARequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ +/* 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/15 13:45:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include +#include "server/AResource.hpp" +#include #include +#include + #include #include namespace webserv { -class Cgi { +class Cgi : public server::AClientResource { public: - Cgi(http::ARequest *, config::Server *); + Cgi(http::ARequest *, config::Route *); ~Cgi(void); std::string getEnv(std::string &); - void setEnv(std::string &, std::string); + void setEnv(const std::string, std::string); void process(void); @@ -33,8 +36,11 @@ class Cgi { private: void _initEnvp(void); + char **_genEnv(void); + std::map _envp; - config::Server *_conf; - http::IMessage *_request; + config::Route *_conf; + http::ARequest *_request; }; + }; // namespace webserv diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 6ce13f8..488e705 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -3,23 +3,54 @@ /* ::: :::::::: */ /* 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/15 12:46:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "requests/default.hpp" +#include "requests/Cgi.hpp" +#include "help.hpp" +#include using namespace webserv; -Cgi::Cgi(http::ARequest *req, config::Server *conf) : _conf(conf), _request(req) { +Cgi::Cgi(http::ARequest *req, config::Route *conf) + : _conf(conf), _request(req) { _initEnvp(); } -void Cgi::_initEnvp(void) { - //_envp[] = ""; +void Cgi::_initEnvp(void) { + std::stringstream str; + str << WEBSRV_NAME << "/" << WEBSRV_VERSION; + setEnv("SERVER_SOFTWARE", str.str()); + 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.clear(); + str << _request->getBody().length(); + setEnv("CONTENT_LENGH", str.str()); + 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) { @@ -30,20 +61,46 @@ std::string Cgi::getEnv(std::string &key) { return ""; } -void Cgi::setEnv(std::string &key, std::string value) { +void Cgi::setEnv(const std::string key, std::string value) { _envp[key] = value; } -void Cgi::process() { - int pipefd[2]; - int forkPid; +void Cgi::process() { + int pipefd[2]; + int forkPid; if (pipe(pipefd) <= -1) { - //throw error + // throw error } forkPid = fork(); if (forkPid == 0) { - // in fork - } + 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; } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index a4c1508..0f4a611 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/05/02 13:30:50 by mmoussou ### ########.fr */ +/* Updated: 2025/05/14 23:58:55 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -187,14 +187,15 @@ void Server::_run(void) { _log->error("client does not exist"); continue; } - if (client->requestParsed() == true && !client->isReadyToClose()) { + if (client->requestParsed() == true && + !client->isReadyToClose()) { client->answer(); continue; } if (client->isReadyToClose()) { _client_data.erase(std::find(_client_data.begin(), - _client_data.end(), client)); + _client_data.end(), client)); delete client; for (auto it = range(_client_fds)) { if (_client_fds[i].fd == (*it).fd) {