🏗️」 wip: started working on cgi finally

This commit is contained in:
2025-05-15 14:25:45 +02:00
parent 3300dcd057
commit 67cf7a1c3a
6 changed files with 94 additions and 29 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/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,29 +3,32 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* 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/15 13:45:20 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 <requests/ARequest.hpp>
#include <map> #include <map>
#include <string> #include <string>
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 &); std::string getEnv(std::string &);
void setEnv(std::string &, std::string); void setEnv(const std::string, std::string);
void process(void); void process(void);
@ -33,8 +36,11 @@ class Cgi {
private: private:
void _initEnvp(void); void _initEnvp(void);
char **_genEnv(void);
std::map<std::string, std::string> _envp; std::map<std::string, std::string> _envp;
config::Server *_conf; config::Route *_conf;
http::IMessage *_request; http::ARequest *_request;
}; };
}; // namespace webserv }; // namespace webserv

View File

@ -3,23 +3,54 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* 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/15 12:46:38 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "requests/default.hpp" #include "requests/Cgi.hpp"
#include "help.hpp"
#include <string>
using namespace webserv; 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(); _initEnvp();
} }
void Cgi::_initEnvp(void) { void Cgi::_initEnvp(void) {
//_envp[] = ""; 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) { std::string Cgi::getEnv(std::string &key) {
@ -30,7 +61,7 @@ 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;
} }
@ -44,6 +75,32 @@ void Cgi::process() {
forkPid = fork(); forkPid = fork();
if (forkPid == 0) { 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;
} }

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/14 23:58:55 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;
} }