🏗️」 wip: post kinda working

This commit is contained in:
2025-05-29 11:51:40 +02:00
parent e8261b0d97
commit 43ac0792c0
11 changed files with 133 additions and 69 deletions

View File

@ -11,8 +11,8 @@ port = 8080
402 = "uwu.html"
[server.location./]
methods = { "GET" }
root = "/home/adjoly"
methods = { "GET", "POST" }
root = "./exemples/webpage"
dirlist = true
client_max_body_size = "10M"
index = "index.html"

View File

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

20
exemples/webpage/omg.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>POST Request Test</title>
</head>
<body>
<h1>Test POST Request</h1>
<form action="/test.py" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>

30
exemples/webpage/test.py Executable file
View File

@ -0,0 +1,30 @@
#!/nix/store/kjvgj2n3yn70hmjifg6y0bk9m4rf7jba-python3-3.12.10/bin/python3
import cgi
import cgitb
# Enable error reporting
cgitb.enable()
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
form = cgi.FieldStorage()
# Get data from fields
name = form.getvalue("name")
email = form.getvalue("email")
print(f"""
<!DOCTYPE html>
<html>
<head>
<title>POST Request Received</title>
</head>
<body>
<h1>POST Request Received</h1>
<p>Name: {name}</p>
<p>Email: {email}</p>
</body>
</html>
""")

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
/* Updated: 2025/05/27 18:59:29 by adjoly ### ########.fr */
/* Updated: 2025/05/29 11:34:40 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "server/AResource.hpp"
#include <log.hpp>
#include <stdexcept>
#include <unistd.h>
namespace webserv {
@ -34,7 +35,10 @@ class CgiIn : public AClientResource {
void process(void) {
_processed = true;
// TODO: send the body
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");
}
}
clientResType type(void) const { return CGI_IN; }
short event(void) const { return POLLIN; }

View File

@ -6,7 +6,7 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
/* Updated: 2025/05/27 22:26:52 by adjoly ### ########.fr */
/* Updated: 2025/05/29 11:30:07 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/05/28 11:28:01 by adjoly ### ########.fr */
/* Updated: 2025/05/29 11:44:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -91,28 +91,6 @@ void Get::parse(std::string const &data) {
pfd.fd = _cgi->getId();
server::PfdManager::append(pfd, server::RES);
}
/*
std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::You can use every macro and
define like FD_SET, FD_CLR, FD_ISSET and, FD_ZERO (understanding what they do
and how they work is very useful). • A request to your server should never hang
indefinitely. • Your server must be compatible with standard web browsers of
your choice. • We will consider that NGINX is HTTP 1.1 compliant and may be used
to compare headers and answer behaviors. • Your HTTP response status codes must
be accurate. • Your server must have default error pages if none are provided.
• You cant use fork for anything other than CGI (like PHP, or Python, and so
forth). • You must be able to serve a fully static website. • Clients must be
able to upload files. • You need at least the GET, POST, and DELETE methodendl;
std::cout << "protocol: " << this->_protocol << std::endl;
std::cout << std::endl;
std::cout << "-- headers --" << std::endl;
for (std::map<std::string, std::string>::const_iterator it =
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout << it->first
<< ": " << it->second << std::endl; std::cout << std::endl; std::cout << "--
body --" << std::endl << this->_body << std::endl;
*/
}
char isDirectory(const std::string &path) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/05/28 11:28:35 by adjoly ### ########.fr */
/* Updated: 2025/05/29 11:44:46 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
#include <config/URL.hpp>
#include <log.hpp>
#include <requests/default.hpp>
#include <server/default.hpp>
using namespace webserv::http;
@ -57,20 +58,29 @@ void Post::parse(std::string const &data) {
this->_body = body_stream.str();
_url = new URL(_target);
std::cout << *_url << std::endl;
/*
std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::endl;
std::cout << "protocol: " << this->_protocol << std::endl;
std::cout << std::endl;
std::cout << "-- headers --" << std::endl;
for (std::map<std::string, std::string>::const_iterator it =
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout <<
it->first << ": " << it->second << std::endl; std::cout << std::endl;
//std::cout << "-- body --" << std::endl << this->_body << std::endl;
*/
std::string targ = _target;
if (targ[targ.length() - 1] == '/') {
targ += _route->getIndex();
}
if (_route->isCgi(targ)) {
_log->info("cgi added");
try {
_cgi = new server::Cgi(this, _route);
} catch (std::exception &e) {
_log->error(e.what());
_method = "500";
return;
}
server::ResourceManager::append(_cgi);
struct pollfd pfd;
pfd.events = _cgi->event();
pfd.revents = 0;
pfd.fd = _cgi->getId();
server::PfdManager::append(pfd, server::RES);
}
}
std::string Post::extractFilename(const std::string &header) {
@ -108,9 +118,58 @@ void Post::handleMultipartData(const std::string &body,
}
}
Response parseCgiOut(std::string cgi_str) {
Response response;
std::istringstream stream(cgi_str);
std::string line;
response.setStatusCode(200);
while (std::getline(stream, line) && line != "") {
size_t delimiter_index = line.find(':');
if (delimiter_index != std::string::npos) {
std::string key = line.substr(0, delimiter_index);
std::string value = line.substr(delimiter_index + 2);
response.addHeader(key, value);
}
}
std::ostringstream body_stream;
while (std::getline(stream, line))
body_stream << line << "\n";
response.setBody(body_stream.str());
if (response.getHeader("Content-Length") == "") {
std::stringstream length;
length << response.getBody().length();
response.addHeader("Content-Length", length.str());
}
return response;
}
Response Post::execute(void) {
http::Response response;
if (_cgi != not_nullptr) {
if (_method == "500") {
response.setStatusCode(500);
response.addHeader("Content-Type", "text/html");
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
server::PfdManager::remove(_cgi->getId());
server::ResourceManager::remove(_cgi->getId());
_cgi = not_nullptr;
return response;
}
std::string str = static_cast<server::Cgi *>(_cgi)->str();
response = parseCgiOut(str);
response.setProtocol(_protocol);
server::PfdManager::remove(_cgi->getId());
server::ResourceManager::remove(_cgi->getId());
_cgi = not_nullptr;
return response;
}
try {
handleMultipartData(
this->_body,

27
test.py
View File

@ -1,27 +0,0 @@
#!/nix/store/kjvgj2n3yn70hmjifg6y0bk9m4rf7jba-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")
print("")
# 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>")