diff --git a/flake.nix b/flake.nix index e2eef70..95a7555 100644 --- a/flake.nix +++ b/flake.nix @@ -39,7 +39,7 @@ fetchSubmodules = true; # need it for tomlpp }; buildInputs = with pkgs; [ - clang + clang_12 ]; buildPhase = '' PKGS=true make -j diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index a59e6e0..4f42a3e 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* 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::vector *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: diff --git a/includes/requests/RequestImplement.hpp b/includes/requests/RequestImplement.hpp index efb4d2e..97751e5 100644 --- a/includes/requests/RequestImplement.hpp +++ b/includes/requests/RequestImplement.hpp @@ -6,13 +6,17 @@ /* 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 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); diff --git a/includes/server/AResource.hpp b/includes/server/AResource.hpp index 8bfd395..5d307d2 100644 --- a/includes/server/AResource.hpp +++ b/includes/server/AResource.hpp @@ -6,7 +6,7 @@ /* By: mmoussou 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 diff --git a/includes/server/Cgi.hpp b/includes/server/Cgi.hpp index 4914112..8815145 100644 --- a/includes/server/Cgi.hpp +++ b/includes/server/Cgi.hpp @@ -6,23 +6,26 @@ /* 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 -#include +#include "server/AResource.hpp" #include #include -#include -#include #include #include #include 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 _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 diff --git a/includes/server/CgiIn.hpp b/includes/server/CgiIn.hpp index a0b0685..ceb67f4 100644 --- a/includes/server/CgiIn.hpp +++ b/includes/server/CgiIn.hpp @@ -6,7 +6,7 @@ /* 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 +#include 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; diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index c6570af..1e312be 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,7 +6,7 @@ /* 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 { 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; diff --git a/includes/server/FileUpload.hpp b/includes/server/FileUpload.hpp index 019daed..1c5f82f 100644 --- a/includes/server/FileUpload.hpp +++ b/includes/server/FileUpload.hpp @@ -6,7 +6,7 @@ /* 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 { public: FileUpload(int id) { - _fd->fd = id; - _fd->events = POLLOUT; + _fd = id; + _pfd_event = POLLOUT; } ~FileUpload(void) {} diff --git a/includes/server/PfdManager.hpp b/includes/server/PfdManager.hpp new file mode 100644 index 0000000..aeb2af9 --- /dev/null +++ b/includes/server/PfdManager.hpp @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PfdManager.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */ +/* Updated: 2025/05/27 17:59:37 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +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 _pfd_vec; + static std::vector _pfd_type; +}; + +} // namespace server +} // namespace webserv diff --git a/includes/server/ResourceManager.hpp b/includes/server/ResourceManager.hpp index d5280a2..4183bac 100644 --- a/includes/server/ResourceManager.hpp +++ b/includes/server/ResourceManager.hpp @@ -6,7 +6,7 @@ /* 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 #include #include +#include #include #include @@ -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 _res; diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index 6ca0a76..1bef48a 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* 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) Logger *_log; // Pointer to the log class std::vector _fds_server; // The fds of the sockets - std::vector _client_fds; // A vector of all the poll fd + // std::vector _client_fds; // A vector of all the poll fd std::vector _client_data; // vector of all the client sockaddr_in }; diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 403363b..0f50c9a 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,11 +6,12 @@ /* 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 +#include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -30,18 +32,20 @@ using namespace webserv::server; // WARN: construtor will probably be changed and practicly do nothing 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"); _initEnvp(); _prep(); } 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"); _initEnvp(); _prep(); - CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]); + AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]); ResourceManager::append(in); } @@ -53,8 +57,8 @@ void Cgi::_prep(void) { throw std::runtime_error("stdin pipe failed for cgi D:"); if (pipe(_stdout_pipe) == -1) throw std::runtime_error("stdout pipe failed for cgi D:"); - _fd->fd = _stdout_pipe[STDIN_FILENO]; - _fd->events = POLLIN; + _fd= _stdout_pipe[STDIN_FILENO]; + _pfd_event = POLLIN; } void Cgi::_initEnvp(void) { @@ -118,19 +122,23 @@ char **Cgi::_genEnv(void) { } void Cgi::process(void) { + _processed = true; 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(); if (forkPid < 0) throw std::runtime_error("fork failed D:"); else if (forkPid == 0) { - dup2(_stdin_pipe[0], STDIN_FILENO); - close(_stdin_pipe[0]); - close(_stdin_pipe[1]); + dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO); + close(_stdin_pipe[PIPE_READ]); + close(_stdin_pipe[PIPE_WRITE]); - dup2(_stdout_pipe[1], STDOUT_FILENO); - close(_stdout_pipe[0]); - close(_stdout_pipe[1]); + dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO); + close(_stdout_pipe[PIPE_READ]); + close(_stdout_pipe[PIPE_WRITE]); char * argv[] = {const_cast(_script_path.c_str()), NULL}; char **env = _genEnv(); @@ -146,10 +154,9 @@ void Cgi::process(void) { exit(EXIT_FAILURE); } } - close(_stdin_pipe[0]); - close(_stdout_pipe[1]); + close(_stdin_pipe[PIPE_READ]); + close(_stdout_pipe[PIPE_WRITE]); waitpid(forkPid, NULL, 0); - _executed = true; } std::string Cgi::str(void) { @@ -165,5 +172,6 @@ std::string Cgi::str(void) { break; } str.append("\0"); + ResourceManager::remove(_stdin_pipe[PIPE_WRITE]); return str; } diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index 21ba46d..e3125d5 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,21 +6,26 @@ /* 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 #include #include #include #include +#include #include 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) { std::istringstream stream(data); @@ -50,8 +55,10 @@ void Get::parse(std::string const &data) { _url = new URL("http://" + _headers["Host"] + _target); - /*std::cout << "wtf = " << _headers["Host"] << std::endl; - std::cout << *_url << std::endl;*/ + // if (_route->isCgi(_target)) { + // _cgi = new server::Cgi(this, _route); + // server::ResourceManager::append(_cgi); + // } /* std::cout << "-- start-line --" << std::endl; diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 067f966..d5f0fd4 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* 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) { std::string part_header = body.substr(start, end - start); - // std::cout << std::endl << std::endl << std::endl << std::endl; std::string part_content = 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.close(); } else { - std::cerr << "open failed" << std::endl; + _log->error("open failed D:"); } } diff --git a/src/server/Client.cpp b/src/server/Client.cpp index fc61f51..9569df8 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou fd = cpy._pfd->fd; + _fd = cpy._fd; } } @@ -39,7 +39,7 @@ void Client::parse(void) { do { 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) { _log->error("failed to receive request"); throw std::runtime_error("failed to receive request"); @@ -135,7 +135,7 @@ void Client::answer(void) { _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); if (sent == -1) { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index eff9099..c85b577 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* 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 #include #include +#include #include #include #include @@ -29,8 +30,12 @@ #include #include + using namespace webserv::server; +std::vector PfdManager::_pfd_vec; +std::vector PfdManager::_pfd_type; + extern int _sig; Client *Server::_getClient(int fd) { @@ -62,7 +67,7 @@ std::string getMethod(std::string &data) { } int Server::_fillHostsPorts(std::vector &hosts, - std::vector &ports) { + std::vector & ports) { std::vector config = _conf->getServers(); for (auto it = range(config)) { @@ -101,13 +106,13 @@ void Server::_run(void) { it != _fds_server.end(); it++) { fd.fd = *it; fd.events = POLLIN; - _client_fds.push_back(fd); + PfdManager::append(fd, SRV); _log->debug("new socket in poll"); } // to add signal instead of 727 while (727 - sigHandling()) { - if (poll(_client_fds.data(), _client_fds.size(), 5000) < 0) { + if (poll(PfdManager::data(), PfdManager::size(), 5000) < 0) { if (errno == EINTR) { continue; } @@ -120,7 +125,7 @@ void Server::_run(void) { size_t i = 0; for (auto it = range(_fds_server), i++) { - if (_client_fds[i].revents & POLLIN) { + if (PfdManager::at(i).revents & POLLIN) { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); int client_fd = @@ -150,10 +155,8 @@ void Server::_run(void) { pfd.fd = client_fd; pfd.events = POLLIN | POLLOUT; pfd.revents = 0; - _client_fds.push_back(pfd); - struct pollfd *ppfd = - _client_fds.data() + _client_fds.size() - 1; - Client *new_client = new Client(ppfd, conf_srv); + PfdManager::append(pfd, CLIENT); + Client *new_client = new Client(pfd.fd, conf_srv); if (new_client == NULL) { continue; } @@ -162,16 +165,16 @@ void Server::_run(void) { } } - for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { - if (_client_fds[i].revents & POLLERR) { + for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) { + if (PfdManager::at(i).revents & POLLERR) { _log->debug("pollerr"); - close(_client_fds[i].fd); - _client_fds.erase(_client_fds.begin() + i); + close(PfdManager::at(i).fd); + PfdManager::remove(PfdManager::at(i).fd); delete _client_data[i - _fds_server.size()]; _client_data.erase(_client_data.begin() + i); - } else if (_client_fds[i].revents & POLLIN) { + } else if (PfdManager::at(i).revents & POLLIN) { _log->debug("pollin"); - Client *client = _getClient(_client_fds[i].fd); + Client *client = _getClient(PfdManager::at(i).fd); if (client == not_nullptr) { _log->error("client does not exist"); continue; @@ -183,21 +186,24 @@ void Server::_run(void) { _client_data.erase(std::find(_client_data.begin(), _client_data.end(), client)); delete client; - for (auto it = range(_client_fds)) { - if (_client_fds[i].fd == (*it).fd) { - _log->debug("client fds erased"); - close(it.base()->fd); - _client_fds.erase(it); - break; - } - } + // for (auto it = range(_client_fds)) { + // if (_client_fds[i].fd == (*it).fd) { + // _log->debug("client fds erased"); + // close(it.base()->fd); + // _client_fds.erase(it); + // break; + // } + // } + close(PfdManager::at(i).fd); + PfdManager::remove(PfdManager::at(i).fd); + _log->debug("client removed"); i--; } - } else if (_client_fds[i].revents & POLLOUT) { + } else if (PfdManager::at(i).revents & POLLOUT) { std::stringstream str; - str << _client_fds[i].fd; + str << PfdManager::at(i).fd; _log->debug("pollout = " + str.str()); - Client *client = _getClient(_client_fds[i].fd); + Client *client = _getClient(PfdManager::at(i).fd); if (client == not_nullptr) { _log->error("client does not exist"); @@ -213,14 +219,18 @@ void Server::_run(void) { _client_data.erase(std::find(_client_data.begin(), _client_data.end(), client)); delete client; - for (auto it = range(_client_fds)) { - if (_client_fds[i].fd == (*it).fd) { - _log->debug("client fds erased"); - close(it.base()->fd); - _client_fds.erase(it); - break; - } - } + // for (auto it = range(_client_fds)) { + // if (_client_fds[i].fd == (*it).fd) { + // _log->debug("client fds erased"); + // close(it.base()->fd); + // _client_fds.erase(it); + // break; + // } + // } + close(PfdManager::at(i).fd); + PfdManager::remove(PfdManager::at(i).fd); + _log->debug("client removed"); + i--; } } @@ -241,7 +251,5 @@ Server::Server(config::Config *conf) : _conf(conf) { Server::~Server(void) { log("➖", "Server::Server", "destructor called"); - for (auto it = range(_client_fds)) { - close(it->fd); - } + PfdManager::clear(); } diff --git a/test.py b/test.py new file mode 100644 index 0000000..d995946 --- /dev/null +++ b/test.py @@ -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("") +print("") +print("CGI Script Test") +print("") +print("") +print("

CGI Script is Running

") +print("

Your web server is working correctly!

") +print("") +print("")