」 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

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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::vector<std::string> *getCgi(void) { return _cgi; }
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:
private:

View File

@ -6,13 +6,17 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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>
namespace webserv {
namespace server {
class Cgi;
class CgiIn;
}; // namespace server
namespace http {
class Get : public ARequest {
@ -23,6 +27,9 @@ class Get : public ARequest {
void parse(std::string const &data);
Response execute(void);
// private:
// server::Cgi *_cgi;
};
class Post : public ARequest {
@ -37,9 +44,12 @@ class Post : public ARequest {
const std::string &boundary);
Response execute(void);
// private:
// server::Cgi *_cgi;
};
class Delete : public http::ARequest {
class Delete : public ARequest {
public:
Delete(void) {}
Delete(std::string &data);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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() {}
bool operator==(int i) const {
if (i == _fd->fd)
if (i == _fd)
return true;
return false;
}
void setFileDescriptor(struct pollfd *fd) { _fd = fd; }
struct pollfd getFileDescriptor(void) const { return *_fd; }
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:
struct pollfd *_fd;
int _fd;
bool _processed;
short _pfd_event;
};
} // namespace server

View File

@ -6,23 +6,26 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
#include <config/Route.hpp>
#include "server/AResource.hpp"
#include <config/default.hpp>
#include <cstdio>
#include <requests/default.hpp>
#include <server/AResource.hpp>
#include <map>
#include <string>
#include <unistd.h>
namespace webserv {
namespace http {
class Get;
class Post;
class ARequest;
}; // namespace http
namespace server {
#define PIPE_READ 0
@ -34,19 +37,13 @@ class Cgi : public server::AClientResource {
Cgi(webserv::http::Post *, webserv::config::Route *);
~Cgi(void);
clientResType type(void) const { return CGI; }
/**
* @brief Can be used to process the Cgi script
*/
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
*
@ -54,9 +51,10 @@ class Cgi : public server::AClientResource {
*/
std::string str(void);
short event(void) const { return POLLIN; }
protected:
private:
bool _executed;
bool _is_post;
void _initEnvp(void);
@ -77,8 +75,7 @@ class Cgi : public server::AClientResource {
std::map<std::string, std::string> _envp; // The envp filled with _initEnvp
webserv::config::Route *_conf; // The configuration for the route used
webserv::http::ARequest
*_request; // The requests that will be used for the cgi
http::ARequest *_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
// post

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <log.hpp>
#include <unistd.h>
namespace webserv {
namespace server {
@ -22,16 +23,23 @@ class CgiIn : public AClientResource {
public:
CgiIn(std::string body, int id) : _body(body) {
log("", "CgiIn", "constructor called");
_fd->fd = id;
_fd->events = POLLOUT;
_processed = false;
_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
}
clientResType type(void) const { return CGI_IN; }
short event(void) const { return POLLIN; }
protected:
private:
std::string _body;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 {
public:
Client(struct pollfd *, config::Server *);
Client(int, config::Server *);
Client(const Client &cpy);
virtual ~Client(void);
@ -33,15 +33,15 @@ class Client {
bool requestParsed(void);
struct pollfd *getPollfd(void) const { return _pfd; }
int getPollfd(void) const { return _fd; }
bool operator==(int fd) {
if (fd != _pfd->fd)
if (fd != _fd)
return false;
return true;
}
bool isReadyToClose() const;
bool isReadyToClose() const;
private:
void _getRequest(std::string);
@ -55,11 +55,11 @@ class Client {
return newStr;
}
struct pollfd *_pfd;
int _fd;
http::ARequest *_request;
http::Response _response;
config::Server *_conf;
config::Route *_route;
config::Route * _route;
size_t _bytes_sent;
std::string _response_str;
bool _response_done;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 {
public:
FileUpload(int id) {
_fd->fd = id;
_fd->events = POLLOUT;
_fd = id;
_pfd_event = POLLOUT;
}
~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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <sstream>
#include <stdexcept>
#include <sys/poll.h>
#include <vector>
#include <cppeleven.hpp>
@ -37,6 +38,7 @@ class CompareId {
int _id;
};
class ResourceManager {
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:
private:
static std::vector<AClientResource *> _res;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
Logger *_log; // Pointer to the log class
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
};