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/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/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/20 20:01:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include +#include "server/AResource.hpp" +#include #include +#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); + int getFdOut(void) const { return _stdout_pipe[0]; } + /** + * @brief Can be used to prepare the Cgi execution + * + * @note To be used after the main fd is in POLLOUT state + */ + void prepare(void); + + /** + * @brief Can be used to know if the prepare function has already been + * called + */ + bool isPrepared(void) { return _prepared; } + + /** + * @brief Can be used to process the Cgi script + */ void process(void); + /** + * @brief Can be used to check if the Cgi have been executed + * + * @note Need to be used after the process() function and checked before + * using str() + */ + bool isProcessed(void) { return _executed; } + + /** + * @brief Can be used to get the output of the Cgi + * + * @note Need to be used after the outfd is in POLLIN state + */ + std::string str(void); + protected: private: + bool _prepared; + bool _executed; + void _initEnvp(void); - std::map _envp; - config::Server *_conf; - http::IMessage *_request; + /** + * @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::string _getEnv(std::string &) const; + void _setEnv(const std::string, std::string); + + 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 + + int _stdin_pipe[2]; // The pipefd for the stdin of the cgi + int _stdout_pipe[2]; // The pipefd for the stdout of 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 6ce13f8..ea5c2be 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -3,26 +3,76 @@ /* ::: :::::::: */ /* 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/21 09:50:23 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "requests/default.hpp" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include using namespace webserv; -Cgi::Cgi(http::ARequest *req, config::Server *conf) : _conf(conf), _request(req) { +// WARN: construtor will probably be changed and practicly do nothing +Cgi::Cgi(http::ARequest *req, config::Route *conf) + : _prepared(false), _executed(false), _conf(conf), _request(req) { _initEnvp(); + _cgi_path = _conf->getCgiPath(req->getTarget()); + if (_cgi_path == "") { + throw; + // TODO: need to make something probably will be checked before by + // client + } } -void Cgi::_initEnvp(void) { - //_envp[] = ""; +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 + // 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 << _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("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) const { auto it = _envp.find(key); if (it != _envp.end()) { return it->second; @@ -30,20 +80,83 @@ 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; +char **Cgi::_genEnv(void) { + char **newEnv = new char *[_envp.size() + 1]; + size_t i = 0; - if (pipe(pipefd) <= -1) { - //throw error + 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::prepare(void) { + if (pipe(_stdin_pipe) == -1 && pipe(_stdout_pipe) == -1) { + throw; + // TODO: need to make a better throw + } + _fd->fd = _stdin_pipe[1]; + _fd->events = POLLOUT; + _prepared = true; +} + +void Cgi::process(void) { + pid_t forkPid; + forkPid = fork(); - if (forkPid == 0) { - // in fork + if (forkPid < 0) { + throw; + // TODO: fork fail + } else if (forkPid == 0) { + dup2(_stdin_pipe[0], STDIN_FILENO); + close(_stdin_pipe[0]); + close(_stdin_pipe[1]); + + dup2(_stdout_pipe[1], STDOUT_FILENO); + close(_stdout_pipe[0]); + close(_stdout_pipe[1]); + + char *argv[] = {const_cast(_cgi_path.c_str()), + const_cast(_script_path.c_str()), NULL}; + char **env = _genEnv(); + + if (execve(_cgi_path.c_str(), argv, env) == -1) { + std::stringstream str; + str << "how did you do that ???? : "; + str << errno; + _log->error(str.str()); + for (int i = 0; env[i] != NULL; i++) + delete env[i]; + delete env; + exit(EXIT_FAILURE); + } } + close(_stdin_pipe[0]); + close(_stdout_pipe[1]); + waitpid(forkPid, NULL, 0); + _executed = true; +} + +std::string Cgi::str(void) { + std::string str; + int max = _conf->getMaxBody(); + char buffer[1024]; + + while (max) { + ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer)); + if (count > 0) + str.append(buffer); + else + break; + } + str.append("\0"); + return str; } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index a4c1508..13e1c6a 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/19 15:11:00 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) {