🔨」 fix: now working

This commit is contained in:
2025-04-25 14:54:50 +02:00
parent 8d4ef4095a
commit 4ba80434e6
6 changed files with 48 additions and 23 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */ /* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */
/* Updated: 2025/04/24 14:24:09 by adjoly ### ########.fr */ /* Updated: 2025/04/25 13:40:10 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/04/25 13:20:13 by adjoly ### ########.fr */ /* Updated: 2025/04/25 14:33:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#include <config/default.hpp> #include <config/default.hpp>
#include <netinet/in.h> #include <netinet/in.h>
#include <requests/default.hpp> #include <requests/default.hpp>
#include <server/default.hpp> #include <server/Server.hpp>
#include <webserv.hpp> #include <webserv.hpp>
namespace webserv { namespace webserv {
@ -23,13 +23,20 @@ namespace server {
class Client { class Client {
public: public:
Client();
Client(struct pollfd *, sockaddr_in, config::Config *); Client(struct pollfd *, sockaddr_in, config::Config *);
void parse(void); void parse(void);
virtual ~Client(void); virtual ~Client(void);
void answer(void); void answer(void);
struct pollfd *getPollfd(void) const { return _pfd; }
bool operator==(int fd) {
if (fd != _pfd->fd)
return false;
return true;
}
private: private:
void _getRequest(std::string); void _getRequest(std::string);

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/25 13:23:08 by adjoly ### ########.fr */ /* Updated: 2025/04/25 14:52:50 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,7 @@ class Server {
/** /**
* @brief Used to handle client request * @brief Used to handle client request
* *
* @param The fd of the client * @param the position in the _client_data
*/ */
bool _handle_client(Client *); bool _handle_client(Client *);
@ -78,6 +78,8 @@ class Server {
return false; return false;
} }
Client *_getClient(int);
config::Config config::Config
*_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

View File

@ -6,16 +6,15 @@
/* 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/04/25 13:21:42 by adjoly ### ########.fr */ /* Updated: 2025/04/25 14:35:37 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <log.hpp> #include <log.hpp>
#include <server/Client.hpp> #include <server/Client.hpp>
#include <sstream>
using namespace server; using namespace webserv::server;
Client::Client(void) {}
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf) Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
: _pfd(pfd), _client_addr(socket), _Gconf(conf) { : _pfd(pfd), _client_addr(socket), _Gconf(conf) {
@ -26,6 +25,8 @@ void Client::parse(void) {
std::string received_data; std::string received_data;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
ssize_t bytes_received; ssize_t bytes_received;
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(_pfd->fd, buffer, BUFFER_SIZE - 1, 0);
@ -82,5 +83,6 @@ void Client::answer(void) {
Client::~Client(void) { Client::~Client(void) {
log("", "Client", "destructor called"); log("", "Client", "destructor called");
delete _pfd;
delete (http::Get *)(this->_request); delete (http::Get *)(this->_request);
} }

View File

@ -6,10 +6,11 @@
/* 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/04/25 13:09:33 by adjoly ### ########.fr */ /* Updated: 2025/04/25 14:54:42 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp"
#include <cerrno> #include <cerrno>
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
@ -32,6 +33,15 @@ using namespace webserv::server;
extern int _sig; extern int _sig;
Client *Server::_getClient(int fd) {
for (auto it = range(_client_data)) {
if (*(*it) == fd) {
return *it;
}
}
return not_nullptr;
}
std::string convertIPToString(const struct in_addr *addr) { std::string convertIPToString(const struct in_addr *addr) {
unsigned int ip = ntohl(addr->s_addr); unsigned int ip = ntohl(addr->s_addr);
std::stringstream ss; std::stringstream ss;
@ -92,7 +102,6 @@ void Server::_run(void) {
fd.fd = *it; fd.fd = *it;
fd.events = POLLIN; fd.events = POLLIN;
_client_fds.push_back(fd); _client_fds.push_back(fd);
_client_data.push_back(NULL);
_log->debug("new socket in poll"); _log->debug("new socket in poll");
} }
@ -122,27 +131,31 @@ void Server::_run(void) {
continue; continue;
} }
pollfd pfd; struct pollfd pfd;
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); _client_fds.push_back(pfd);
auto itpfd = _client_fds.end() - 1; struct pollfd *ppfd = _client_fds.data() + _client_fds.size() -1;
Client *new_client = new Client(itpfd, client_addr, _conf); Client *new_client = new Client(ppfd, client_addr, _conf);
_client_data.push_back(new_client); _client_data.push_back(new_client);
} }
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLERR) { Client *client = _getClient(_client_fds[i].fd);
if (_handle_client(_client_fds[i])) { if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
if (_client_fds[i].revents & POLLIN) {
if (_handle_client(client)) {
close(_client_fds[i].fd); close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i); _client_fds.erase(_client_fds.begin() + i);
delete _client_data[i]; delete _client_data[i];
_client_data.erase(_client_data.begin() + i); _client_data.erase(_client_data.begin() + i);
i--; i--;
} }
} else if (_client_fds[i].revents & POLLIN) {} }
else if()
} }
} }
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/25 13:23:13 by adjoly ### ########.fr */ /* Updated: 2025/04/25 14:53:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -84,7 +84,6 @@ bool Server::_handle_client(Client *client) {
try { try {
client->parse(); client->parse();
client->answer(); client->answer();
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
_log->error(e.what()); _log->error(e.what());
return false; return false;
@ -92,3 +91,5 @@ bool Server::_handle_client(Client *client) {
return true; return true;
} }