🔀」 merge: a very cool feature has been merged ! :D

This commit is contained in:
2025-07-15 20:28:48 +02:00
parent 9ce4eb8306
commit b9a0714925
9 changed files with 120 additions and 32 deletions

View File

@ -1,7 +1,9 @@
#!/nix/store/8w718rm43x7z73xhw9d6vh8s4snrq67h-python3-3.12.10/bin/python3 #!/usr/bin/env python3
# Import modules for CGI handling
import cgi import cgi
import cgitb import cgitb
import time
import os
import sys # Needed for sys.exit()
# Enable error reporting # Enable error reporting
cgitb.enable() cgitb.enable()
@ -9,6 +11,7 @@ cgitb.enable()
# Create instance of FieldStorage # Create instance of FieldStorage
form = cgi.FieldStorage() form = cgi.FieldStorage()
try:
# Set the content type to HTML # Set the content type to HTML
print("Content-Type: text/html\n") print("Content-Type: text/html\n")
@ -20,5 +23,13 @@ print("</head>")
print("<body>") print("<body>")
print("<h1>CGI Script is Running</h1>") print("<h1>CGI Script is Running</h1>")
print("<p>Your web server is working correctly!</p>") print("<p>Your web server is working correctly!</p>")
files = os.listdir('.')
for file in files:
print(file)
print("</body>") print("</body>")
print("</html>") print("</html>")
except Exception:
# Print error page and exit with error code
cgitb.handler()
sys.exit(1)

View File

@ -6,16 +6,18 @@
/* 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/07/11 17:02:37 by adjoly ### ########.fr */ /* Updated: 2025/07/15 20:27:43 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include "log.hpp"
#include "server/PfdManager.hpp" #include "server/PfdManager.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <ctime> #include <ctime>
#include <server/AResource.hpp> #include <server/AResource.hpp>
@ -73,7 +75,7 @@ class Cgi : public server::AClientResource {
return false; return false;
} }
bool isTimedout(void) const { bool isTimedout(void) {
if (!isProcessed() && isReady()) { if (!isProcessed() && isReady()) {
return false; return false;
} }
@ -86,6 +88,56 @@ class Cgi : public server::AClientResource {
return false; return false;
} }
bool isErr(void) {
if (_err == true) {
return true;
}
if (_forkPid == -1) {
return false;
}
int status = 0;
pid_t result = waitpid(_forkPid, &status, WNOHANG);
if (result == 0 ) {
return false;
} else if (result == -1) {
if (errno == ECHILD) {
_log->warn("CGI child already reaped");
_err = true;
return true;
}
_log->error("waitpid failed: " + std::string(std::strerror(errno)));
_err = true;
return true;
}
if (WIFEXITED(status)) {
int code = WEXITSTATUS(status);
if (code != 0) {
std::stringstream str;
str << "CGI exited with status code " << code;
_log->error(str.str());
_err = true;
return true;
}
return false;
}
if (WIFSIGNALED(status)) {
std::stringstream str;
str << "CGI terminated by signal: ";
str << WTERMSIG(status);
_log->error(str.str());
_err = true;
return true;
}
_log->error("CGI ended abnormally");
_err = true;
return true;
}
protected: protected:
private: private:
bool _is_post; bool _is_post;
@ -116,6 +168,7 @@ class Cgi : public server::AClientResource {
std::time_t _start_time; // To use for timeout std::time_t _start_time; // To use for timeout
pid_t _forkPid; // Can be used to kill the process pid_t _forkPid; // Can be used to kill the process
bool _err;
}; };
}; // namespace server }; // namespace server

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/07/12 21:38:36 by adjoly ### ########.fr */ /* Updated: 2025/07/15 20:28:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,6 +29,7 @@ namespace webserv {
Logger * _log = not_nullptr; Logger * _log = not_nullptr;
std::vector<server::AClientResource *> server::ResourceManager::_res; std::vector<server::AClientResource *> server::ResourceManager::_res;
config::Config * config::_conf = not_nullptr; config::Config * config::_conf = not_nullptr;
server::Server *_server_wtf;
} // namespace webserv } // namespace webserv
int _sig = 0; int _sig = 0;
@ -82,9 +83,9 @@ int main(int ac, char **av) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
webserv::server::Server *serv = new webserv::server::Server(); _server_wtf = new webserv::server::Server();
delete serv; delete _server_wtf;
delete _log; delete _log;
delete config::_conf; delete config::_conf;
} }

View File

@ -6,12 +6,13 @@
/* 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/07/12 13:22:20 by adjoly ### ########.fr */ /* Updated: 2025/07/15 20:28:18 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "server/PfdManager.hpp" #include "server/PfdManager.hpp"
#include "server/ResourceManager.hpp" #include "server/ResourceManager.hpp"
#include <cmath>
#include <ctime> #include <ctime>
#include <help.hpp> #include <help.hpp>
#include <ios> #include <ios>
@ -31,10 +32,14 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
webserv::Logger _log;
// webserv::config::Config *config::_conf;
webserv::server::Server *_server_wtf;
using namespace webserv::server; using namespace webserv::server;
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), _forkPid(-1), _err(false) {
_processed = false; _processed = false;
log("", "Cgi", "GET constructor called"); log("", "Cgi", "GET constructor called");
_initEnvp(); _initEnvp();
@ -42,7 +47,7 @@ Cgi::Cgi(http::Get *req, config::Route *conf)
} }
Cgi::Cgi(http::Post *req, config::Route *conf) Cgi::Cgi(http::Post *req, config::Route *conf)
: _is_post(true), _conf(conf), _request(req) { : _is_post(true), _conf(conf), _request(req), _forkPid(-1), _err(false) {
_processed = false; _processed = false;
log("", "Cgi", "POST constructor called"); log("", "Cgi", "POST constructor called");
_initEnvp(); _initEnvp();
@ -158,17 +163,35 @@ void Cgi::process(void) {
close(_stdout_pipe[PIPE_READ]); close(_stdout_pipe[PIPE_READ]);
close(_stdout_pipe[PIPE_WRITE]); close(_stdout_pipe[PIPE_WRITE]);
std::string target = _request->getTarget();
std::string dir = "/";
std::size_t pos = target.find_last_of('/');
if (pos != std::string::npos)
dir = target.substr(0, pos + 1);
chdir((_request->getRoute()->getRootDir() + dir).c_str());
std::cerr << _request->getRoute()->getRootDir() + dir << std::endl;
std::cerr << _script_path << std::endl;
char * argv[] = {const_cast<char *>(_script_path.c_str()), NULL}; char * argv[] = {const_cast<char *>(_script_path.c_str()), NULL};
char **env = _genEnv(); char **env = _genEnv();
_script_path = target.substr(pos + 1);
if (execve(_script_path.c_str(), argv, env) == -1) { if (execve(_script_path.c_str(), argv, env) == -1) {
std::stringstream str; std::stringstream str;
str << "execve failed D:"; str << "execve failed D:";
str << errno; str << errno;
_log->error(str.str()); _log->error(str.str());
if (_is_post) {
PfdManager::remove(_stdin_pipe[PIPE_WRITE]);
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
}
for (int i = 0; env[i] != NULL; i++) for (int i = 0; env[i] != NULL; i++)
delete env[i]; delete env[i];
delete env; delete env;
delete _server_wtf;
delete _log;
delete config::_conf;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {

View File

@ -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/07/15 19:16:46 by mmoussou ### ########.fr */ /* Updated: 2025/07/15 20:28:28 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -146,7 +146,7 @@ Response Get::execute(void) {
http::Response response; http::Response response;
if (_cgi != not_nullptr) { if (_cgi != not_nullptr) {
if (_method == "500" || _cgi->isTimedout()) { if (_method == "500" || _cgi->isTimedout() || _cgi->isErr()) {
response.setStatusCode(500); response.setStatusCode(500);
response.setProtocol(_protocol); response.setProtocol(_protocol);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");

View File

@ -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/07/15 19:17:08 by mmoussou ### ########.fr */ /* Updated: 2025/07/15 20:28:41 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -166,7 +166,7 @@ Response Post::execute(void) {
http::Response response; http::Response response;
if (_cgi != not_nullptr) { if (_cgi != not_nullptr) {
if (_method == "500" || _cgi->isTimedout()) { if (_method == "500" || _cgi->isTimedout() || _cgi->isErr()) {
response.setStatusCode(500); response.setStatusCode(500);
response.setProtocol(_protocol); response.setProtocol(_protocol);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/07/12 20:11:17 by mmoussou ### ########.fr */ /* Updated: 2025/07/15 20:27:20 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -128,7 +128,7 @@ bool Client::requestParsed(void) {
if (_request->getCgi() != not_nullptr) { if (_request->getCgi() != not_nullptr) {
if (!_request->getCgi()->isProcessed()) if (!_request->getCgi()->isProcessed())
return false; return false;
else if (_request->getCgi()->isTimedout()) else if (_request->getCgi()->isTimedout() || _request->getCgi()->isErr())
return true; return true;
else if (!(PfdManager::get(_request->getCgi()->getId())->revents & else if (!(PfdManager::get(_request->getCgi()->getId())->revents &
_request->getCgi()->event())) _request->getCgi()->event()))

View File

@ -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/07/02 12:45:52 by adjoly ### ########.fr */ /* Updated: 2025/07/15 19:17:25 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -130,7 +130,7 @@ void Server::_handle_resource(size_t i) {
if (res == not_nullptr) if (res == not_nullptr)
return; return;
if (res->type() == CGI && static_cast<Cgi *>(res)->isTimedout()) { if (res->type() == CGI && (static_cast<Cgi *>(res)->isTimedout() || static_cast<Cgi *>(res)->isErr())) {
return; return;
} }
if (!res->isProcessed() && res->isReady()) { if (!res->isProcessed() && res->isReady()) {