diff --git a/exemples/webpage/not_found.html b/exemples/err_pages/not_found.html similarity index 100% rename from exemples/webpage/not_found.html rename to exemples/err_pages/not_found.html diff --git a/exemples/test.toml b/exemples/test.toml index 760746f..3dcb166 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -6,13 +6,13 @@ host = "0.0.0.0" port = 8080 [server.error_pages] -404 = "not_found.html" +404 = "exemples/err_pages/not_found.html" 401 = "unauthorized.html" 402 = "uwu.html" [server.location./] methods = { "GET", "POST" } -root = "./exemples/webpage" +root = "exemples/webpage" dirlist = true client_max_body_size = "10M" index = "index.html" diff --git a/exemples/webpage/get_cgi.py b/exemples/webpage/get_cgi.py new file mode 100755 index 0000000..d754653 --- /dev/null +++ b/exemples/webpage/get_cgi.py @@ -0,0 +1,24 @@ +#!/nix/store/8w718rm43x7z73xhw9d6vh8s4snrq67h-python3-3.12.10/bin/python3 +# Import modules for CGI handling +import cgi +import cgitb + +# Enable error reporting +cgitb.enable() + +# Create instance of FieldStorage +form = cgi.FieldStorage() + +# Set the content type to HTML +print("Content-Type: text/html\n") + +# Output a simple HTML page +print("") +print("") +print("CGI Script Test") +print("") +print("") +print("

CGI Script is Running

") +print("

Your web server is working correctly!

") +print("") +print("") diff --git a/exemples/webpage/index.html b/exemples/webpage/index.html index 89abca8..0986ee8 100644 --- a/exemples/webpage/index.html +++ b/exemples/webpage/index.html @@ -8,4 +8,10 @@ body {

evilge

+
+ get cgi test +
+ catually +
+ post cgi test diff --git a/exemples/webpage/omg.html b/exemples/webpage/post_cgi.html similarity index 91% rename from exemples/webpage/omg.html rename to exemples/webpage/post_cgi.html index 820a075..9f9d80d 100644 --- a/exemples/webpage/omg.html +++ b/exemples/webpage/post_cgi.html @@ -7,7 +7,7 @@

Test POST Request

-
+
diff --git a/exemples/webpage/test.py b/exemples/webpage/post_cgi.py similarity index 100% rename from exemples/webpage/test.py rename to exemples/webpage/post_cgi.py diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp index e05f7be..c479a25 100644 --- a/includes/requests/ARequest.hpp +++ b/includes/requests/ARequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ -/* Updated: 2025/05/29 12:07:56 by adjoly ### ########.fr */ +/* Updated: 2025/05/30 16:16:28 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include #include #include +#include namespace webserv { namespace server { @@ -27,17 +28,25 @@ class CgiIn : public AClientResource { _processed = false; _fd = id; _pfd_event = POLLOUT; + + _log->debug("post body : " + body); } - ~CgiIn(void) { - log("➖", "CgiIn", "destructor called"); - } + ~CgiIn(void) { log("➖", "CgiIn", "destructor called"); } void process(void) { + std::cout << "processing cginin" << std::endl; _processed = true; ssize_t bytes_written = write(_fd, _body.c_str(), _body.size()); - if (bytes_written == -1) { - throw std::runtime_error("write error could not write body to cgi stdin"); - } + _log->debug("writting body : " + _body); + if (bytes_written == -1) + throw std::runtime_error( + "write error could not write body to cgi stdin"); + bytes_written = write(_fd, "\0", 1); + _log->debug("writting end bytes"); + if (bytes_written == -1) + throw std::runtime_error( + "write error could not write end byte to cgi stdin"); + PfdManager::remove(_fd); } clientResType type(void) const { return CGI_IN; } short event(void) const { return POLLIN; } diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 34d329a..f48bed0 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,10 +6,11 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/05/29 11:30:07 by adjoly ### ########.fr */ +/* Updated: 2025/05/30 16:13:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "server/PfdManager.hpp" #include #include #include @@ -30,7 +31,6 @@ using namespace webserv::server; -// WARN: construtor will probably be changed and practicly do nothing Cgi::Cgi(http::Get *req, config::Route *conf) : _is_post(false), _conf(conf), _request(req) { _processed = false; @@ -48,6 +48,11 @@ Cgi::Cgi(http::Post *req, config::Route *conf) AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]); ResourceManager::append(in); + struct pollfd pfd; + pfd.events = in->event(); + pfd.revents = 0; + pfd.fd = in->getId(); + server::PfdManager::append(pfd, server::RES); } Cgi::~Cgi(void) { log("➖", "Cgi", "destructor called"); } @@ -73,21 +78,21 @@ void Cgi::_initEnvp(void) { 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 + str << _request->getServer()->getPort(); + _setEnv("SERVER_PORT", str.str()); + str.clear(); + + _setEnv("REQUEST_METHOD", _request->getMethod()); _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 + _setEnv("PATH_TRANSLATED", _request->getTarget()); + _setEnv("PATH_INFO", _request->getTarget()); 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")); @@ -96,8 +101,8 @@ void Cgi::_initEnvp(void) { _setEnv("HTTP_USER_AGENT", _request->getHeader("User-Agent")); _setEnv("SCRIPT_NAME", _request->getTarget()); - - _setEnv("QUERY_STRING", _request->getUrl().getQueryString()); + if (_is_post == false) + _setEnv("QUERY_STRING", _request->getUrl().getQueryString()); } std::string Cgi::_getEnv(std::string &key) const { @@ -135,7 +140,7 @@ void Cgi::process(void) { if (forkPid < 0) throw std::runtime_error("fork failed D:"); else if (forkPid == 0) { - if (_is_post) { + if (_is_post == true) { dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO); close(_stdin_pipe[PIPE_READ]); close(_stdin_pipe[PIPE_WRITE]); @@ -178,11 +183,11 @@ std::string Cgi::str(void) { max -= count; } else if (count == 0) { break; - } - else + } else break; } - if (_is_post) - ResourceManager::remove(_stdin_pipe[PIPE_WRITE]); + // if (_is_post) + // ResourceManager::remove(_stdin_pipe[PIPE_WRITE]); + // FIX: whyyyyy return str.str(); } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 6b6e496..b31fb37 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/29 12:07:43 by adjoly ### ########.fr */ +/* Updated: 2025/05/29 12:10:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -136,8 +136,6 @@ void Server::_run(void) { case RES: _handle_resource(i); break; - default: - break; } } } diff --git a/src/server/ServerHandle.cpp b/src/server/ServerHandle.cpp index 6373c4c..9d5bbfe 100644 --- a/src/server/ServerHandle.cpp +++ b/src/server/ServerHandle.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */ -/* Updated: 2025/05/28 10:49:47 by adjoly ### ########.fr */ +/* Updated: 2025/05/30 15:48:16 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -131,12 +131,12 @@ void Server::_handle_resource(size_t i) { } if (!res->isProcessed() && res->isReady()) { - if (res->type() == CGI) { - res->process(); + res->process(); + // if (res->type() == CGI) { _log->info("processingggg"); - } else if (pfd.revents & res->event()) { - res->process(); - _log->info("processingggg"); - } + // } else if (pfd.revents & res->event()) { + // res->process(); + // _log->info("processingggg"); + // } } }