mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: genEnv done and process in progress
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:59:41 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 <node/default.hpp>
|
#include <node/default.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tomlpp.hpp>
|
#include <tomlpp.hpp>
|
||||||
|
#include <webserv.hpp>
|
||||||
|
|
||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace config {
|
namespace config {
|
||||||
@ -39,6 +40,23 @@ class Route {
|
|||||||
std::string getUpRoot(void) { return _up_root; }
|
std::string getUpRoot(void) { return _up_root; }
|
||||||
std::string getIndex(void) { return _index; }
|
std::string getIndex(void) { return _index; }
|
||||||
std::map<std::string, std::string> *getCgi(void) { return _cgi; }
|
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; }
|
bool *getMethods(void) { return _methods; }
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 13:46:34 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:
|
private:
|
||||||
void _initEnvp(void);
|
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);
|
char **_genEnv(void);
|
||||||
|
|
||||||
std::map<std::string, std::string> _envp;
|
std::string _script_path; // The full path of the script to be executed
|
||||||
config::Route *_conf;
|
std::string _cgi_path;
|
||||||
http::ARequest *_request;
|
|
||||||
|
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
|
}; // namespace webserv
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/12 17:13:39 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);
|
delete (*it);
|
||||||
_res.erase(it);
|
_res.erase(it);
|
||||||
}
|
}
|
||||||
// TODO throw or not - to see
|
// NOTE: throw or not - to see
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -6,41 +6,52 @@
|
|||||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 13:46:34 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 <cstddef>
|
||||||
#include "help.hpp"
|
#include <cstring>
|
||||||
|
#include <help.hpp>
|
||||||
|
#include <requests/Cgi.hpp>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace webserv;
|
using namespace webserv;
|
||||||
|
|
||||||
|
// WARN: construtor will probably be changed and practicly do nothing
|
||||||
Cgi::Cgi(http::ARequest *req, config::Route *conf)
|
Cgi::Cgi(http::ARequest *req, config::Route *conf)
|
||||||
: _conf(conf), _request(req) {
|
: _conf(conf), _request(req) {
|
||||||
_initEnvp();
|
_initEnvp();
|
||||||
|
_cgi_path = _conf->getCgiPath(req->getTarget());
|
||||||
|
if (_cgi_path == "") {
|
||||||
|
// TODO: need to make something
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cgi::_initEnvp(void) {
|
void Cgi::_initEnvp(void) {
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
str << WEBSRV_NAME << "/" << WEBSRV_VERSION;
|
str << WEBSRV_NAME << "/" << WEBSRV_VERSION;
|
||||||
setEnv("SERVER_SOFTWARE", str.str());
|
setEnv("SERVER_SOFTWARE", str.str());
|
||||||
|
str.clear();
|
||||||
setEnv("SERVER_NAME", _request->getHeader("Host"));
|
setEnv("SERVER_NAME", _request->getHeader("Host"));
|
||||||
setEnv("SERVER_PROTOCOL", _request->getProtocol());
|
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
|
// way i dont know yet
|
||||||
|
|
||||||
setEnv("GATEWAY_INTERFACE", "CGI/1.1");
|
setEnv("GATEWAY_INTERFACE", "CGI/1.1");
|
||||||
|
|
||||||
// setEnv("PATH_TRANSLATED", ); // TODO wtf should i put here i dont fcking
|
// setEnv("PATH_TRANSLATED", ); // TODO: wtf should i put here i dont fcking
|
||||||
// know setEnv("PATH_INFO", ); // TODO wut make no sense
|
// know
|
||||||
|
// setEnv("PATH_INFO", ); // TODO: wut make no sense
|
||||||
|
|
||||||
str.clear();
|
|
||||||
str << _request->getBody().length();
|
str << _request->getBody().length();
|
||||||
setEnv("CONTENT_LENGH", str.str());
|
setEnv("CONTENT_LENGH", str.str());
|
||||||
|
str.clear();
|
||||||
setEnv("CONTENT_TYPE", _request->getHeader("Content-Type"));
|
setEnv("CONTENT_TYPE", _request->getHeader("Content-Type"));
|
||||||
// setEnv("REMOTE_ADDR", _request->get) // TODO don't have it yet need to be
|
// setEnv("REMOTE_ADDR", _request->get) // TODO: don't have it yet need to
|
||||||
// passed to the requset :sob:
|
// be passed to the requset :sob:
|
||||||
setEnv("HTTP_ACCEPT", _request->getHeader("Accept"));
|
setEnv("HTTP_ACCEPT", _request->getHeader("Accept"));
|
||||||
setEnv("HTTP_ACCEPT_LANGUAGE", _request->getHeader("Accept-Language"));
|
setEnv("HTTP_ACCEPT_LANGUAGE", _request->getHeader("Accept-Language"));
|
||||||
setEnv("HTTP_COOKIE", _request->getHeader("Cookie"));
|
setEnv("HTTP_COOKIE", _request->getHeader("Cookie"));
|
||||||
@ -65,42 +76,27 @@ void Cgi::setEnv(const std::string key, std::string value) {
|
|||||||
_envp[key] = 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() {
|
void Cgi::process() {
|
||||||
int pipefd[2];
|
int pipefd[2];
|
||||||
int forkPid;
|
int forkPid;
|
||||||
|
|
||||||
if (pipe(pipefd) <= -1) {
|
if (pipe(pipefd) <= -1) {
|
||||||
// throw error
|
// TODO: error handling
|
||||||
}
|
}
|
||||||
|
|
||||||
forkPid = fork();
|
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;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user