mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-07-20 10:36:33 +02:00
「🔀」 merge: a very cool feature has been merged ! :D
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
#!/nix/store/8w718rm43x7z73xhw9d6vh8s4snrq67h-python3-3.12.10/bin/python3
|
||||
# Import modules for CGI handling
|
||||
#!/usr/bin/env python3
|
||||
import cgi
|
||||
import cgitb
|
||||
import time
|
||||
import os
|
||||
import sys # Needed for sys.exit()
|
||||
|
||||
# Enable error reporting
|
||||
cgitb.enable()
|
||||
@ -9,6 +11,7 @@ cgitb.enable()
|
||||
# Create instance of FieldStorage
|
||||
form = cgi.FieldStorage()
|
||||
|
||||
try:
|
||||
# Set the content type to HTML
|
||||
print("Content-Type: text/html\n")
|
||||
|
||||
@ -20,5 +23,13 @@ print("</head>")
|
||||
print("<body>")
|
||||
print("<h1>CGI Script is Running</h1>")
|
||||
print("<p>Your web server is working correctly!</p>")
|
||||
files = os.listdir('.')
|
||||
for file in files:
|
||||
print(file)
|
||||
print("</body>")
|
||||
print("</html>")
|
||||
|
||||
except Exception:
|
||||
# Print error page and exit with error code
|
||||
cgitb.handler()
|
||||
sys.exit(1)
|
||||
|
@ -6,16 +6,18 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
||||
#include "log.hpp"
|
||||
#include "server/PfdManager.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <server/AResource.hpp>
|
||||
|
||||
@ -73,7 +75,7 @@ class Cgi : public server::AClientResource {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isTimedout(void) const {
|
||||
bool isTimedout(void) {
|
||||
if (!isProcessed() && isReady()) {
|
||||
return false;
|
||||
}
|
||||
@ -86,6 +88,56 @@ class Cgi : public server::AClientResource {
|
||||
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:
|
||||
private:
|
||||
bool _is_post;
|
||||
@ -116,6 +168,7 @@ class Cgi : public server::AClientResource {
|
||||
|
||||
std::time_t _start_time; // To use for timeout
|
||||
pid_t _forkPid; // Can be used to kill the process
|
||||
bool _err;
|
||||
};
|
||||
|
||||
}; // namespace server
|
||||
|
Submodule lib/tomlpp updated: 9930415a3f...a3af54e791
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
std::vector<server::AClientResource *> server::ResourceManager::_res;
|
||||
config::Config * config::_conf = not_nullptr;
|
||||
server::Server *_server_wtf;
|
||||
} // namespace webserv
|
||||
|
||||
int _sig = 0;
|
||||
@ -82,9 +83,9 @@ int main(int ac, char **av) {
|
||||
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 config::_conf;
|
||||
}
|
||||
|
@ -6,12 +6,13 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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/ResourceManager.hpp"
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <help.hpp>
|
||||
#include <ios>
|
||||
@ -31,10 +32,14 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
webserv::Logger _log;
|
||||
// webserv::config::Config *config::_conf;
|
||||
webserv::server::Server *_server_wtf;
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
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;
|
||||
log("➕", "Cgi", "GET constructor called");
|
||||
_initEnvp();
|
||||
@ -42,7 +47,7 @@ Cgi::Cgi(http::Get *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;
|
||||
log("➕", "Cgi", "POST constructor called");
|
||||
_initEnvp();
|
||||
@ -158,17 +163,35 @@ void Cgi::process(void) {
|
||||
close(_stdout_pipe[PIPE_READ]);
|
||||
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 **env = _genEnv();
|
||||
_script_path = target.substr(pos + 1);
|
||||
|
||||
if (execve(_script_path.c_str(), argv, env) == -1) {
|
||||
std::stringstream str;
|
||||
str << "execve failed D:";
|
||||
str << errno;
|
||||
_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++)
|
||||
delete env[i];
|
||||
delete env;
|
||||
delete _server_wtf;
|
||||
delete _log;
|
||||
delete config::_conf;
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (_cgi != not_nullptr) {
|
||||
if (_method == "500" || _cgi->isTimedout()) {
|
||||
if (_method == "500" || _cgi->isTimedout() || _cgi->isErr()) {
|
||||
response.setStatusCode(500);
|
||||
response.setProtocol(_protocol);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (_cgi != not_nullptr) {
|
||||
if (_method == "500" || _cgi->isTimedout()) {
|
||||
if (_method == "500" || _cgi->isTimedout() || _cgi->isErr()) {
|
||||
response.setStatusCode(500);
|
||||
response.setProtocol(_protocol);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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()->isProcessed())
|
||||
return false;
|
||||
else if (_request->getCgi()->isTimedout())
|
||||
else if (_request->getCgi()->isTimedout() || _request->getCgi()->isErr())
|
||||
return true;
|
||||
else if (!(PfdManager::get(_request->getCgi()->getId())->revents &
|
||||
_request->getCgi()->event()))
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
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;
|
||||
}
|
||||
if (!res->isProcessed() && res->isReady()) {
|
||||
|
Reference in New Issue
Block a user