mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: post kinda working
This commit is contained in:
@ -11,8 +11,8 @@ port = 8080
|
|||||||
402 = "uwu.html"
|
402 = "uwu.html"
|
||||||
|
|
||||||
[server.location./]
|
[server.location./]
|
||||||
methods = { "GET" }
|
methods = { "GET", "POST" }
|
||||||
root = "/home/adjoly"
|
root = "./exemples/webpage"
|
||||||
dirlist = true
|
dirlist = true
|
||||||
client_max_body_size = "10M"
|
client_max_body_size = "10M"
|
||||||
index = "index.html"
|
index = "index.html"
|
||||||
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
20
exemples/webpage/omg.html
Normal file
20
exemples/webpage/omg.html
Normal 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
30
exemples/webpage/test.py
Executable 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>
|
||||||
|
""")
|
@ -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/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 "server/AResource.hpp"
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
namespace webserv {
|
namespace webserv {
|
||||||
@ -34,7 +35,10 @@ class CgiIn : public AClientResource {
|
|||||||
|
|
||||||
void process(void) {
|
void process(void) {
|
||||||
_processed = true;
|
_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; }
|
clientResType type(void) const { return CGI_IN; }
|
||||||
short event(void) const { return POLLIN; }
|
short event(void) const { return POLLIN; }
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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/27 22:26:52 by adjoly ### ########.fr */
|
/* Updated: 2025/05/29 11:30:07 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
|
/* 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();
|
pfd.fd = _cgi->getId();
|
||||||
server::PfdManager::append(pfd, server::RES);
|
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 can’t 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) {
|
char isDirectory(const std::string &path) {
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
|
/* 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 <config/URL.hpp>
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
#include <requests/default.hpp>
|
#include <requests/default.hpp>
|
||||||
|
#include <server/default.hpp>
|
||||||
|
|
||||||
using namespace webserv::http;
|
using namespace webserv::http;
|
||||||
|
|
||||||
@ -57,20 +58,29 @@ void Post::parse(std::string const &data) {
|
|||||||
this->_body = body_stream.str();
|
this->_body = body_stream.str();
|
||||||
|
|
||||||
_url = new URL(_target);
|
_url = new URL(_target);
|
||||||
std::cout << *_url << std::endl;
|
|
||||||
|
|
||||||
/*
|
std::string targ = _target;
|
||||||
std::cout << "-- start-line --" << std::endl;
|
|
||||||
std::cout << "method: " << this->_method << std::endl;
|
if (targ[targ.length() - 1] == '/') {
|
||||||
std::cout << "target: " << this->_target << std::endl;
|
targ += _route->getIndex();
|
||||||
std::cout << "protocol: " << this->_protocol << std::endl;
|
}
|
||||||
std::cout << std::endl;
|
|
||||||
std::cout << "-- headers --" << std::endl;
|
if (_route->isCgi(targ)) {
|
||||||
for (std::map<std::string, std::string>::const_iterator it =
|
_log->info("cgi added");
|
||||||
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout <<
|
try {
|
||||||
it->first << ": " << it->second << std::endl; std::cout << std::endl;
|
_cgi = new server::Cgi(this, _route);
|
||||||
//std::cout << "-- body --" << std::endl << this->_body << std::endl;
|
} 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) {
|
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) {
|
Response Post::execute(void) {
|
||||||
http::Response response;
|
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 {
|
try {
|
||||||
handleMultipartData(
|
handleMultipartData(
|
||||||
this->_body,
|
this->_body,
|
||||||
|
27
test.py
27
test.py
@ -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>")
|
|
Reference in New Issue
Block a user