🔨」 fix: fixed post request with cgi

This commit is contained in:
2025-05-30 16:17:21 +02:00
parent 64764d18c4
commit 30866645a0
11 changed files with 80 additions and 37 deletions

View File

@ -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"

24
exemples/webpage/get_cgi.py Executable file
View File

@ -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("<html>")
print("<head>")
print("<title>CGI Script Test</title>")
print("</head>")
print("<body>")
print("<h1>CGI Script is Running</h1>")
print("<p>Your web server is working correctly!</p>")
print("</body>")
print("</html>")

View File

@ -8,4 +8,10 @@ body {
<body>
<h1 style="font-size: 3rem; font-weight: bold; color: #fff; text-align: center; letter-spacing: 2px; background: linear-gradient(90deg, #ff00ff, #00ffff); -webkit-background-clip: text; display: inline-block; border-bottom: 2px solid #fff;">evilge</h1>
</body>
</br>
<a style="color: #fff;" href="/get_cgi.py">get cgi test</a>
</br>
<a style="color: #fff;" href="/catually.png">catually</a>
</br>
<a style="color: #fff;" href="/post_cgi.html">post cgi test</a>
</html>

View File

@ -7,7 +7,7 @@
</head>
<body>
<h1>Test POST Request</h1>
<form action="/test.py" method="post">
<form action="/post_cgi.py" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/05/28 09:45:17 by adjoly ### ########.fr */
/* Updated: 2025/05/30 16:08:13 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,7 @@ class ARequest : public http::IMessage {
webserv::config::Route *getRoute(void) const;
URL getUrl() const;
virtual server::Cgi * getCgi() const { return not_nullptr; }
config::Server * getServer(void) const { return _srv; }
void setMethod(std::string const method);
void setTarget(std::string const target);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <log.hpp>
#include <stdexcept>
#include <unistd.h>
#include <server/PfdManager.hpp>
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; }

View File

@ -6,10 +6,11 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <help.hpp>
#include <ios>
#include <requests/default.hpp>
@ -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();
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");
// }
}
}