」 feat: implemented succecfully that f*cking pfdmanager

This commit is contained in:
2025-05-27 18:04:21 +02:00
parent cccc25fba1
commit 050643814e
17 changed files with 294 additions and 109 deletions

View File

@ -39,7 +39,7 @@
fetchSubmodules = true; # need it for tomlpp fetchSubmodules = true; # need it for tomlpp
}; };
buildInputs = with pkgs; [ buildInputs = with pkgs; [
clang clang_12
]; ];
buildPhase = '' buildPhase = ''
PKGS=true make -j PKGS=true make -j

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
/* Updated: 2025/05/26 17:02:12 by adjoly ### ########.fr */ /* Updated: 2025/05/27 09:37:23 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,6 +38,14 @@ class Route {
std::string getIndex(void) { return _index; } std::string getIndex(void) { return _index; }
std::vector<std::string> *getCgi(void) { return _cgi; } std::vector<std::string> *getCgi(void) { return _cgi; }
bool * getMethods(void) { return _methods; } bool * getMethods(void) { return _methods; }
bool isCgi(std::string target) {
std::string target_ext = target.substr(target.find('.'));
for (auto it = prange(_cgi)) {
if (target_ext == *it)
return true;
}
return false;
}
protected: protected:
private: private:

View File

@ -6,13 +6,17 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */ /* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
/* Updated: 2025/05/03 12:07:57 by adjoly ### ########.fr */ /* Updated: 2025/05/27 16:49:00 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <requests/ARequest.hpp> #include <requests/ARequest.hpp>
namespace webserv { namespace webserv {
namespace server {
class Cgi;
class CgiIn;
}; // namespace server
namespace http { namespace http {
class Get : public ARequest { class Get : public ARequest {
@ -23,6 +27,9 @@ class Get : public ARequest {
void parse(std::string const &data); void parse(std::string const &data);
Response execute(void); Response execute(void);
// private:
// server::Cgi *_cgi;
}; };
class Post : public ARequest { class Post : public ARequest {
@ -37,9 +44,12 @@ class Post : public ARequest {
const std::string &boundary); const std::string &boundary);
Response execute(void); Response execute(void);
// private:
// server::Cgi *_cgi;
}; };
class Delete : public http::ARequest { class Delete : public ARequest {
public: public:
Delete(void) {} Delete(void) {}
Delete(std::string &data); Delete(std::string &data);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */ /* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
/* Updated: 2025/05/24 10:09:33 by adjoly ### ########.fr */ /* Updated: 2025/05/27 13:12:03 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,19 +25,22 @@ class AClientResource {
virtual ~AClientResource() {} virtual ~AClientResource() {}
bool operator==(int i) const { bool operator==(int i) const {
if (i == _fd->fd) if (i == _fd)
return true; return true;
return false; return false;
} }
void setFileDescriptor(struct pollfd *fd) { _fd = fd; }
struct pollfd getFileDescriptor(void) const { return *_fd; }
virtual clientResType type(void) const = 0; virtual clientResType type(void) const = 0;
int getId(void) const { return _fd->fd; } int getId(void) const { return _fd; }
virtual void process(void) = 0;
bool isProcessed(void) const { return _processed; }
virtual short event(void) const = 0;
protected: protected:
struct pollfd *_fd; int _fd;
bool _processed;
short _pfd_event;
}; };
} // namespace server } // namespace server

View File

@ -6,23 +6,26 @@
/* 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/26 17:19:01 by adjoly ### ########.fr */ /* Updated: 2025/05/27 16:16:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <config/Route.hpp> #include "server/AResource.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <cstdio> #include <cstdio>
#include <requests/default.hpp>
#include <server/AResource.hpp>
#include <map> #include <map>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
namespace webserv { namespace webserv {
namespace http {
class Get;
class Post;
class ARequest;
}; // namespace http
namespace server { namespace server {
#define PIPE_READ 0 #define PIPE_READ 0
@ -34,19 +37,13 @@ class Cgi : public server::AClientResource {
Cgi(webserv::http::Post *, webserv::config::Route *); Cgi(webserv::http::Post *, webserv::config::Route *);
~Cgi(void); ~Cgi(void);
clientResType type(void) const { return CGI; }
/** /**
* @brief Can be used to process the Cgi script * @brief Can be used to process the Cgi script
*/ */
void process(void); void process(void);
/**
* @brief Can be used to check if the Cgi have been executed
*
* @note Need to be used after the process() function and checked before
* using str()
*/
bool isProcessed(void) { return _executed; }
/** /**
* @brief Can be used to get the output of the Cgi * @brief Can be used to get the output of the Cgi
* *
@ -54,9 +51,10 @@ class Cgi : public server::AClientResource {
*/ */
std::string str(void); std::string str(void);
short event(void) const { return POLLIN; }
protected: protected:
private: private:
bool _executed;
bool _is_post; bool _is_post;
void _initEnvp(void); void _initEnvp(void);
@ -77,8 +75,7 @@ class Cgi : public server::AClientResource {
std::map<std::string, std::string> _envp; // The envp filled with _initEnvp std::map<std::string, std::string> _envp; // The envp filled with _initEnvp
webserv::config::Route *_conf; // The configuration for the route used webserv::config::Route *_conf; // The configuration for the route used
webserv::http::ARequest http::ARequest *_request; // The requests that will be used for the cgi
*_request; // The requests that will be used for the cgi
int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a
// post // post

View File

@ -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/24 11:15:25 by adjoly ### ########.fr */ /* Updated: 2025/05/27 13:08:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
#include "server/AResource.hpp" #include "server/AResource.hpp"
#include <log.hpp> #include <log.hpp>
#include <unistd.h>
namespace webserv { namespace webserv {
namespace server { namespace server {
@ -22,16 +23,23 @@ class CgiIn : public AClientResource {
public: public:
CgiIn(std::string body, int id) : _body(body) { CgiIn(std::string body, int id) : _body(body) {
log("", "CgiIn", "constructor called"); log("", "CgiIn", "constructor called");
_fd->fd = id; _processed = false;
_fd->events = POLLOUT; _fd = id;
_pfd_event = POLLOUT;
}
~CgiIn(void) {
log("", "CgiIn", "destructor called");
close(_fd);
} }
~CgiIn(void) { log("", "CgiIn", "destructor called"); }
void send(void) { void process(void) {
_processed = true;
// TODO: send the body // TODO: send the body
} }
clientResType type(void) const { return CGI_IN; } clientResType type(void) const { return CGI_IN; }
short event(void) const { return POLLIN; }
protected: protected:
private: private:
std::string _body; std::string _body;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/05/02 13:21:10 by mmoussou ### ########.fr */ /* Updated: 2025/05/27 16:47:20 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ namespace server {
class Client { class Client {
public: public:
Client(struct pollfd *, config::Server *); Client(int, config::Server *);
Client(const Client &cpy); Client(const Client &cpy);
virtual ~Client(void); virtual ~Client(void);
@ -33,10 +33,10 @@ class Client {
bool requestParsed(void); bool requestParsed(void);
struct pollfd *getPollfd(void) const { return _pfd; } int getPollfd(void) const { return _fd; }
bool operator==(int fd) { bool operator==(int fd) {
if (fd != _pfd->fd) if (fd != _fd)
return false; return false;
return true; return true;
} }
@ -55,11 +55,11 @@ class Client {
return newStr; return newStr;
} }
struct pollfd *_pfd; int _fd;
http::ARequest *_request; http::ARequest *_request;
http::Response _response; http::Response _response;
config::Server *_conf; config::Server *_conf;
config::Route *_route; config::Route * _route;
size_t _bytes_sent; size_t _bytes_sent;
std::string _response_str; std::string _response_str;
bool _response_done; bool _response_done;

View File

@ -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/24 11:06:33 by adjoly ### ########.fr */ /* Updated: 2025/05/27 13:07:50 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,8 +20,8 @@ namespace server {
class FileUpload : public AClientResource { class FileUpload : public AClientResource {
public: public:
FileUpload(int id) { FileUpload(int id) {
_fd->fd = id; _fd = id;
_fd->events = POLLOUT; _pfd_event = POLLOUT;
} }
~FileUpload(void) {} ~FileUpload(void) {}

View File

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PfdManager.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */
/* Updated: 2025/05/27 17:59:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <stdexcept>
#include <webserv.hpp>
#include <poll.h>
#include <vector>
namespace webserv {
namespace server {
enum pfd_type { SRV, CLIENT, RES };
class ComparePfd {
public:
ComparePfd(int id) : _id(id) {}
bool operator()(const struct pollfd res) const { return res.fd == _id; }
private:
int _id;
};
class PfdManager {
public:
static struct pollfd at(int i) { return _pfd_vec[i]; }
static struct pollfd *get(int id) {
auto it =
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
if (it != _pfd_vec.end())
return &*it;
throw std::out_of_range("pollfd does not exist for client D:");
}
static pfd_type getType(int id) {
auto it =
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
if (it != _pfd_vec.end()) {
size_t i = std::distance(_pfd_vec.begin(), it);
try {
auto it = _pfd_type.at(i);
return it;
} catch (std::out_of_range &) {
throw std::out_of_range("pollfd does not exist for client D:");
}
}
throw std::out_of_range("pollfd does not exist for client D:");
}
static void append(struct pollfd pfd, pfd_type type) {
_pfd_vec.push_back(pfd);
_pfd_type.push_back(type);
}
static void remove(int id) {
auto it =
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
if (it != _pfd_vec.end()) {
auto type = _pfd_type.begin() + std::distance(_pfd_vec.begin(), it);
_pfd_type.erase(type);
close(it->fd);
_pfd_vec.erase(it);
}
}
static struct pollfd *data(void) { return _pfd_vec.data(); }
static size_t size(void) { return _pfd_vec.size(); }
static void clear(void) {
for (auto it = range(_pfd_vec)) {
close(it->fd);
}
_pfd_vec.clear();
_pfd_type.clear();
}
protected:
private:
static std::vector<struct pollfd> _pfd_vec;
static std::vector<pfd_type> _pfd_type;
};
} // namespace server
} // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */ /* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
/* Updated: 2025/05/24 11:00:26 by adjoly ### ########.fr */ /* Updated: 2025/05/27 17:24:51 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <sys/poll.h>
#include <vector> #include <vector>
#include <cppeleven.hpp> #include <cppeleven.hpp>
@ -37,6 +38,7 @@ class CompareId {
int _id; int _id;
}; };
class ResourceManager { class ResourceManager {
public: public:
/** /**
@ -79,6 +81,13 @@ class ResourceManager {
} }
} }
static void process(void) {
for (auto it = range(_res)) {
(*it)->process();
// TODO: check for event and if isProcessed() and process
}
}
protected: protected:
private: private:
static std::vector<AClientResource *> _res; static std::vector<AClientResource *> _res;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/04/26 16:36:05 by adjoly ### ########.fr */ /* Updated: 2025/05/27 17:43:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,7 +77,7 @@ class Server {
*_conf; // Pointer to the configuration class (with all config in) *_conf; // Pointer to the configuration class (with all config in)
Logger *_log; // Pointer to the log class Logger *_log; // Pointer to the log class
std::vector<int> _fds_server; // The fds of the sockets std::vector<int> _fds_server; // The fds of the sockets
std::vector<struct pollfd> _client_fds; // A vector of all the poll fd // std::vector<struct pollfd> _client_fds; // A vector of all the poll fd
std::vector<Client *> _client_data; // vector of all the client sockaddr_in std::vector<Client *> _client_data; // vector of all the client sockaddr_in
}; };

View File

@ -6,11 +6,12 @@
/* 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/26 17:22:12 by adjoly ### ########.fr */ /* Updated: 2025/05/27 13:08:36 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <help.hpp> #include <help.hpp>
#include <ios>
#include <requests/default.hpp> #include <requests/default.hpp>
#include <server/default.hpp> #include <server/default.hpp>
@ -22,6 +23,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <sys/poll.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -30,18 +32,20 @@ using namespace webserv::server;
// WARN: construtor will probably be changed and practicly do nothing // 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)
: _executed(false), _is_post(false), _conf(conf), _request(req) { : _is_post(false), _conf(conf), _request(req) {
_processed = false;
log("", "Cgi", "GET constructor called"); log("", "Cgi", "GET constructor called");
_initEnvp(); _initEnvp();
_prep(); _prep();
} }
Cgi::Cgi(http::Post *req, config::Route *conf) Cgi::Cgi(http::Post *req, config::Route *conf)
: _executed(false), _is_post(true), _conf(conf), _request(req) { : _is_post(true), _conf(conf), _request(req) {
_processed = false;
log("", "Cgi", "POST constructor called"); log("", "Cgi", "POST constructor called");
_initEnvp(); _initEnvp();
_prep(); _prep();
CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]); AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
ResourceManager::append(in); ResourceManager::append(in);
} }
@ -53,8 +57,8 @@ void Cgi::_prep(void) {
throw std::runtime_error("stdin pipe failed for cgi D:"); throw std::runtime_error("stdin pipe failed for cgi D:");
if (pipe(_stdout_pipe) == -1) if (pipe(_stdout_pipe) == -1)
throw std::runtime_error("stdout pipe failed for cgi D:"); throw std::runtime_error("stdout pipe failed for cgi D:");
_fd->fd = _stdout_pipe[STDIN_FILENO]; _fd= _stdout_pipe[STDIN_FILENO];
_fd->events = POLLIN; _pfd_event = POLLIN;
} }
void Cgi::_initEnvp(void) { void Cgi::_initEnvp(void) {
@ -118,19 +122,23 @@ char **Cgi::_genEnv(void) {
} }
void Cgi::process(void) { void Cgi::process(void) {
_processed = true;
pid_t forkPid; pid_t forkPid;
if (access(_script_path.c_str(), X_OK))
throw std::runtime_error(
"script is not executable please run : chmod +x " + _script_path);
forkPid = fork(); forkPid = fork();
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) {
dup2(_stdin_pipe[0], STDIN_FILENO); dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
close(_stdin_pipe[0]); close(_stdin_pipe[PIPE_READ]);
close(_stdin_pipe[1]); close(_stdin_pipe[PIPE_WRITE]);
dup2(_stdout_pipe[1], STDOUT_FILENO); dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO);
close(_stdout_pipe[0]); close(_stdout_pipe[PIPE_READ]);
close(_stdout_pipe[1]); close(_stdout_pipe[PIPE_WRITE]);
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();
@ -146,10 +154,9 @@ void Cgi::process(void) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
close(_stdin_pipe[0]); close(_stdin_pipe[PIPE_READ]);
close(_stdout_pipe[1]); close(_stdout_pipe[PIPE_WRITE]);
waitpid(forkPid, NULL, 0); waitpid(forkPid, NULL, 0);
_executed = true;
} }
std::string Cgi::str(void) { std::string Cgi::str(void) {
@ -165,5 +172,6 @@ std::string Cgi::str(void) {
break; break;
} }
str.append("\0"); str.append("\0");
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
return str; return str;
} }

View File

@ -6,21 +6,26 @@
/* 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/04 12:25:43 by adjoly ### ########.fr */ /* Updated: 2025/05/27 16:49:05 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp"
#include <algorithm> #include <algorithm>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
#include <server/default.hpp>
#include <requests/default.hpp> #include <requests/default.hpp>
using namespace webserv::http; using namespace webserv::http;
Get::Get(std::string &data) { this->parse(data); } Get::Get(std::string &data) {
// _cgi = not_nullptr;
this->parse(data);
}
void Get::parse(std::string const &data) { void Get::parse(std::string const &data) {
std::istringstream stream(data); std::istringstream stream(data);
@ -50,8 +55,10 @@ void Get::parse(std::string const &data) {
_url = new URL("http://" + _headers["Host"] + _target); _url = new URL("http://" + _headers["Host"] + _target);
/*std::cout << "wtf = " << _headers["Host"] << std::endl; // if (_route->isCgi(_target)) {
std::cout << *_url << std::endl;*/ // _cgi = new server::Cgi(this, _route);
// server::ResourceManager::append(_cgi);
// }
/* /*
std::cout << "-- start-line --" << std::endl; std::cout << "-- start-line --" << std::endl;

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/05/03 09:44:41 by adjoly ### ########.fr */ /* Updated: 2025/05/27 09:23:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -84,7 +84,6 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
if (end != std::string::npos) { if (end != std::string::npos) {
std::string part_header = body.substr(start, end - start); std::string part_header = body.substr(start, end - start);
// std::cout << std::endl << std::endl << std::endl << std::endl;
std::string part_content = std::string part_content =
body.substr(end + 4, body.find(delim, end) - end - 4); body.substr(end + 4, body.find(delim, end) - end - 4);
@ -94,7 +93,7 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
outfile.write(part_content.c_str(), part_content.length()); outfile.write(part_content.c_str(), part_content.length());
outfile.close(); outfile.close();
} else { } else {
std::cerr << "open failed" << std::endl; _log->error("open failed D:");
} }
} }

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/05/22 17:36:36 by adjoly ### ########.fr */ /* Updated: 2025/05/27 16:48:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,8 +19,8 @@
using namespace webserv::server; using namespace webserv::server;
Client::Client(struct pollfd *pfd, config::Server *conf) Client::Client(int fd, config::Server *conf)
: _pfd(pfd), _conf(conf) { : _fd(fd), _conf(conf) {
_request = not_nullptr; _request = not_nullptr;
log("", "Client", "constructor called"); log("", "Client", "constructor called");
_response_done = false; _response_done = false;
@ -28,7 +28,7 @@ Client::Client(struct pollfd *pfd, config::Server *conf)
Client::Client(const Client &cpy) { Client::Client(const Client &cpy) {
if (this != &cpy) { if (this != &cpy) {
_pfd->fd = cpy._pfd->fd; _fd = cpy._fd;
} }
} }
@ -39,7 +39,7 @@ void Client::parse(void) {
do { do {
std::memset(buffer, 0, BUFFER_SIZE); std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0); bytes_received = recv(_fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1) { if (bytes_received == -1) {
_log->error("failed to receive request"); _log->error("failed to receive request");
throw std::runtime_error("failed to receive request"); throw std::runtime_error("failed to receive request");
@ -135,7 +135,7 @@ void Client::answer(void) {
_bytes_sent = 0; _bytes_sent = 0;
} }
ssize_t sent = send(_pfd->fd, _response_str.c_str() + _bytes_sent, ssize_t sent = send(_fd, _response_str.c_str() + _bytes_sent,
_response_str.length() - _bytes_sent, 0); _response_str.length() - _bytes_sent, 0);
if (sent == -1) { if (sent == -1) {

View File

@ -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/22 17:36:13 by adjoly ### ########.fr */ /* Updated: 2025/05/27 18:03:35 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h> #include <poll.h>
#include <requests/default.hpp> #include <requests/default.hpp>
#include <server/PfdManager.hpp>
#include <server/default.hpp> #include <server/default.hpp>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
@ -29,8 +30,12 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <webserv.hpp> #include <webserv.hpp>
using namespace webserv::server; using namespace webserv::server;
std::vector<struct pollfd> PfdManager::_pfd_vec;
std::vector<pfd_type> PfdManager::_pfd_type;
extern int _sig; extern int _sig;
Client *Server::_getClient(int fd) { Client *Server::_getClient(int fd) {
@ -62,7 +67,7 @@ std::string getMethod(std::string &data) {
} }
int Server::_fillHostsPorts(std::vector<std::string> &hosts, int Server::_fillHostsPorts(std::vector<std::string> &hosts,
std::vector<int> &ports) { std::vector<int> & ports) {
std::vector<config::Server *> config = _conf->getServers(); std::vector<config::Server *> config = _conf->getServers();
for (auto it = range(config)) { for (auto it = range(config)) {
@ -101,13 +106,13 @@ void Server::_run(void) {
it != _fds_server.end(); it++) { it != _fds_server.end(); it++) {
fd.fd = *it; fd.fd = *it;
fd.events = POLLIN; fd.events = POLLIN;
_client_fds.push_back(fd); PfdManager::append(fd, SRV);
_log->debug("new socket in poll"); _log->debug("new socket in poll");
} }
// to add signal instead of 727 // to add signal instead of 727
while (727 - sigHandling()) { while (727 - sigHandling()) {
if (poll(_client_fds.data(), _client_fds.size(), 5000) < 0) { if (poll(PfdManager::data(), PfdManager::size(), 5000) < 0) {
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} }
@ -120,7 +125,7 @@ void Server::_run(void) {
size_t i = 0; size_t i = 0;
for (auto it = range(_fds_server), i++) { for (auto it = range(_fds_server), i++) {
if (_client_fds[i].revents & POLLIN) { if (PfdManager::at(i).revents & POLLIN) {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
int client_fd = int client_fd =
@ -150,10 +155,8 @@ void Server::_run(void) {
pfd.fd = client_fd; pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT; pfd.events = POLLIN | POLLOUT;
pfd.revents = 0; pfd.revents = 0;
_client_fds.push_back(pfd); PfdManager::append(pfd, CLIENT);
struct pollfd *ppfd = Client *new_client = new Client(pfd.fd, conf_srv);
_client_fds.data() + _client_fds.size() - 1;
Client *new_client = new Client(ppfd, conf_srv);
if (new_client == NULL) { if (new_client == NULL) {
continue; continue;
} }
@ -162,16 +165,16 @@ void Server::_run(void) {
} }
} }
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) {
if (_client_fds[i].revents & POLLERR) { if (PfdManager::at(i).revents & POLLERR) {
_log->debug("pollerr"); _log->debug("pollerr");
close(_client_fds[i].fd); close(PfdManager::at(i).fd);
_client_fds.erase(_client_fds.begin() + i); PfdManager::remove(PfdManager::at(i).fd);
delete _client_data[i - _fds_server.size()]; delete _client_data[i - _fds_server.size()];
_client_data.erase(_client_data.begin() + i); _client_data.erase(_client_data.begin() + i);
} else if (_client_fds[i].revents & POLLIN) { } else if (PfdManager::at(i).revents & POLLIN) {
_log->debug("pollin"); _log->debug("pollin");
Client *client = _getClient(_client_fds[i].fd); Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) { if (client == not_nullptr) {
_log->error("client does not exist"); _log->error("client does not exist");
continue; continue;
@ -183,21 +186,24 @@ void Server::_run(void) {
_client_data.erase(std::find(_client_data.begin(), _client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client)); _client_data.end(), client));
delete client; delete client;
for (auto it = range(_client_fds)) { // for (auto it = range(_client_fds)) {
if (_client_fds[i].fd == (*it).fd) { // if (_client_fds[i].fd == (*it).fd) {
_log->debug("client fds erased"); // _log->debug("client fds erased");
close(it.base()->fd); // close(it.base()->fd);
_client_fds.erase(it); // _client_fds.erase(it);
break; // break;
} // }
} // }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--; i--;
} }
} else if (_client_fds[i].revents & POLLOUT) { } else if (PfdManager::at(i).revents & POLLOUT) {
std::stringstream str; std::stringstream str;
str << _client_fds[i].fd; str << PfdManager::at(i).fd;
_log->debug("pollout = " + str.str()); _log->debug("pollout = " + str.str());
Client *client = _getClient(_client_fds[i].fd); Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) { if (client == not_nullptr) {
_log->error("client does not exist"); _log->error("client does not exist");
@ -213,14 +219,18 @@ void Server::_run(void) {
_client_data.erase(std::find(_client_data.begin(), _client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client)); _client_data.end(), client));
delete client; delete client;
for (auto it = range(_client_fds)) { // for (auto it = range(_client_fds)) {
if (_client_fds[i].fd == (*it).fd) { // if (_client_fds[i].fd == (*it).fd) {
_log->debug("client fds erased"); // _log->debug("client fds erased");
close(it.base()->fd); // close(it.base()->fd);
_client_fds.erase(it); // _client_fds.erase(it);
break; // break;
} // }
} // }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--; i--;
} }
} }
@ -241,7 +251,5 @@ Server::Server(config::Config *conf) : _conf(conf) {
Server::~Server(void) { Server::~Server(void) {
log("", "Server::Server", "destructor called"); log("", "Server::Server", "destructor called");
for (auto it = range(_client_fds)) { PfdManager::clear();
close(it->fd);
}
} }

25
test.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env 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>")