mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🔨」 fix: fixed post request with cgi
This commit is contained in:
@ -6,13 +6,13 @@ host = "0.0.0.0"
|
|||||||
port = 8080
|
port = 8080
|
||||||
|
|
||||||
[server.error_pages]
|
[server.error_pages]
|
||||||
404 = "not_found.html"
|
404 = "exemples/err_pages/not_found.html"
|
||||||
401 = "unauthorized.html"
|
401 = "unauthorized.html"
|
||||||
402 = "uwu.html"
|
402 = "uwu.html"
|
||||||
|
|
||||||
[server.location./]
|
[server.location./]
|
||||||
methods = { "GET", "POST" }
|
methods = { "GET", "POST" }
|
||||||
root = "./exemples/webpage"
|
root = "exemples/webpage"
|
||||||
dirlist = true
|
dirlist = true
|
||||||
client_max_body_size = "10M"
|
client_max_body_size = "10M"
|
||||||
index = "index.html"
|
index = "index.html"
|
||||||
|
24
exemples/webpage/get_cgi.py
Executable file
24
exemples/webpage/get_cgi.py
Executable 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>")
|
@ -8,4 +8,10 @@ body {
|
|||||||
<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>
|
<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>
|
</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>
|
</html>
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Test POST Request</h1>
|
<h1>Test POST Request</h1>
|
||||||
<form action="/test.py" method="post">
|
<form action="/post_cgi.py" method="post">
|
||||||
<label for="name">Name:</label>
|
<label for="name">Name:</label>
|
||||||
<input type="text" id="name" name="name" required>
|
<input type="text" id="name" name="name" required>
|
||||||
<br>
|
<br>
|
@ -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/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;
|
webserv::config::Route *getRoute(void) const;
|
||||||
URL getUrl() const;
|
URL getUrl() const;
|
||||||
virtual server::Cgi * getCgi() const { return not_nullptr; }
|
virtual server::Cgi * getCgi() const { return not_nullptr; }
|
||||||
|
config::Server * getServer(void) const { return _srv; }
|
||||||
|
|
||||||
void setMethod(std::string const method);
|
void setMethod(std::string const method);
|
||||||
void setTarget(std::string const target);
|
void setTarget(std::string const target);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
/* 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 <log.hpp>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <server/PfdManager.hpp>
|
||||||
|
|
||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace server {
|
namespace server {
|
||||||
@ -27,17 +28,25 @@ class CgiIn : public AClientResource {
|
|||||||
_processed = false;
|
_processed = false;
|
||||||
_fd = id;
|
_fd = id;
|
||||||
_pfd_event = POLLOUT;
|
_pfd_event = POLLOUT;
|
||||||
|
|
||||||
|
_log->debug("post body : " + body);
|
||||||
}
|
}
|
||||||
~CgiIn(void) {
|
~CgiIn(void) { log("➖", "CgiIn", "destructor called"); }
|
||||||
log("➖", "CgiIn", "destructor called");
|
|
||||||
}
|
|
||||||
|
|
||||||
void process(void) {
|
void process(void) {
|
||||||
|
std::cout << "processing cginin" << std::endl;
|
||||||
_processed = true;
|
_processed = true;
|
||||||
ssize_t bytes_written = write(_fd, _body.c_str(), _body.size());
|
ssize_t bytes_written = write(_fd, _body.c_str(), _body.size());
|
||||||
if (bytes_written == -1) {
|
_log->debug("writting body : " + _body);
|
||||||
throw std::runtime_error("write error could not write body to cgi stdin");
|
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; }
|
clientResType type(void) const { return CGI_IN; }
|
||||||
short event(void) const { return POLLIN; }
|
short event(void) const { return POLLIN; }
|
||||||
|
@ -6,10 +6,11 @@
|
|||||||
/* 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/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 <help.hpp>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <requests/default.hpp>
|
#include <requests/default.hpp>
|
||||||
@ -30,7 +31,6 @@
|
|||||||
|
|
||||||
using namespace webserv::server;
|
using namespace webserv::server;
|
||||||
|
|
||||||
// WARN: construtor will probably be changed and practicly do nothing
|
|
||||||
Cgi::Cgi(http::Get *req, config::Route *conf)
|
Cgi::Cgi(http::Get *req, config::Route *conf)
|
||||||
: _is_post(false), _conf(conf), _request(req) {
|
: _is_post(false), _conf(conf), _request(req) {
|
||||||
_processed = false;
|
_processed = false;
|
||||||
@ -48,6 +48,11 @@ Cgi::Cgi(http::Post *req, config::Route *conf)
|
|||||||
AClientResource *in =
|
AClientResource *in =
|
||||||
new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
|
new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
|
||||||
ResourceManager::append(in);
|
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"); }
|
Cgi::~Cgi(void) { log("➖", "Cgi", "destructor called"); }
|
||||||
@ -73,21 +78,21 @@ void Cgi::_initEnvp(void) {
|
|||||||
str.clear();
|
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
|
str << _request->getServer()->getPort();
|
||||||
// way i dont know yet
|
_setEnv("SERVER_PORT", str.str());
|
||||||
|
str.clear();
|
||||||
|
|
||||||
|
_setEnv("REQUEST_METHOD", _request->getMethod());
|
||||||
|
|
||||||
_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", _request->getTarget());
|
||||||
// know
|
_setEnv("PATH_INFO", _request->getTarget());
|
||||||
// setEnv("PATH_INFO", ); // TODO: wut make no sense
|
|
||||||
|
|
||||||
str << _request->getBody().length();
|
str << _request->getBody().length();
|
||||||
_setEnv("CONTENT_LENGH", str.str());
|
_setEnv("CONTENT_LENGH", str.str());
|
||||||
str.clear();
|
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 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"));
|
||||||
@ -96,7 +101,7 @@ void Cgi::_initEnvp(void) {
|
|||||||
_setEnv("HTTP_USER_AGENT", _request->getHeader("User-Agent"));
|
_setEnv("HTTP_USER_AGENT", _request->getHeader("User-Agent"));
|
||||||
|
|
||||||
_setEnv("SCRIPT_NAME", _request->getTarget());
|
_setEnv("SCRIPT_NAME", _request->getTarget());
|
||||||
|
if (_is_post == false)
|
||||||
_setEnv("QUERY_STRING", _request->getUrl().getQueryString());
|
_setEnv("QUERY_STRING", _request->getUrl().getQueryString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +140,7 @@ void Cgi::process(void) {
|
|||||||
if (forkPid < 0)
|
if (forkPid < 0)
|
||||||
throw std::runtime_error("fork failed D:");
|
throw std::runtime_error("fork failed D:");
|
||||||
else if (forkPid == 0) {
|
else if (forkPid == 0) {
|
||||||
if (_is_post) {
|
if (_is_post == true) {
|
||||||
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
|
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
|
||||||
close(_stdin_pipe[PIPE_READ]);
|
close(_stdin_pipe[PIPE_READ]);
|
||||||
close(_stdin_pipe[PIPE_WRITE]);
|
close(_stdin_pipe[PIPE_WRITE]);
|
||||||
@ -178,11 +183,11 @@ std::string Cgi::str(void) {
|
|||||||
max -= count;
|
max -= count;
|
||||||
} else if (count == 0) {
|
} else if (count == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (_is_post)
|
// if (_is_post)
|
||||||
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
|
// ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
|
||||||
|
// FIX: whyyyyy
|
||||||
return str.str();
|
return str.str();
|
||||||
}
|
}
|
||||||
|
@ -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/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:
|
case RES:
|
||||||
_handle_resource(i);
|
_handle_resource(i);
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/27 18:22:48 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->isProcessed() && res->isReady()) {
|
||||||
if (res->type() == CGI) {
|
|
||||||
res->process();
|
res->process();
|
||||||
|
// if (res->type() == CGI) {
|
||||||
_log->info("processingggg");
|
_log->info("processingggg");
|
||||||
} else if (pfd.revents & res->event()) {
|
// } else if (pfd.revents & res->event()) {
|
||||||
res->process();
|
// res->process();
|
||||||
_log->info("processingggg");
|
// _log->info("processingggg");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user