」 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,11 +6,12 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <ios>
#include <requests/default.hpp>
#include <server/default.hpp>
@ -22,6 +23,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
@ -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<char *>(_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;
}

View File

@ -6,21 +6,26 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <server/default.hpp>
#include <requests/default.hpp>
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;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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:");
}
}

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
Client::Client(struct pollfd *pfd, config::Server *conf)
: _pfd(pfd), _conf(conf) {
Client::Client(int fd, config::Server *conf)
: _fd(fd), _conf(conf) {
_request = not_nullptr;
log("", "Client", "constructor called");
_response_done = false;
@ -28,7 +28,7 @@ Client::Client(struct pollfd *pfd, config::Server *conf)
Client::Client(const Client &cpy) {
if (this != &cpy) {
_pfd->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) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <poll.h>
#include <requests/default.hpp>
#include <server/PfdManager.hpp>
#include <server/default.hpp>
#include <sstream>
#include <stdexcept>
@ -29,8 +30,12 @@
#include <sys/socket.h>
#include <webserv.hpp>
using namespace webserv::server;
std::vector<struct pollfd> PfdManager::_pfd_vec;
std::vector<pfd_type> 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<std::string> &hosts,
std::vector<int> &ports) {
std::vector<int> & ports) {
std::vector<config::Server *> 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();
}