🔨」 fix: fixed leak

This commit is contained in:
2025-04-25 17:22:15 +02:00
parent 5ce135594b
commit 202a403430
6 changed files with 38 additions and 27 deletions

View File

@ -27,7 +27,7 @@ redirect = "https://kanel.ovh"
[serverr]
server_names = { "ptnnnn.local"}
host = "127.0.0.2"
host = "127.0.0.1"
port = 9090
[serverr.error_pages]

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/04/25 15:28:10 by adjoly ### ########.fr */
/* Updated: 2025/04/25 17:03:34 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,9 +24,10 @@ namespace server {
class Client {
public:
Client(struct pollfd *, sockaddr_in, config::Config *);
void parse(void);
Client(const Client &cpy);
virtual ~Client(void);
void parse(void);
void answer(void);
struct pollfd *getPollfd(void) const { return _pfd; }
@ -37,14 +38,9 @@ class Client {
return true;
}
bool isToClose() {
return _toClose;
}
private:
void _getRequest(std::string);
bool _toClose;
struct pollfd *_pfd;
struct sockaddr_in _client_addr;
http::IRequest *_request;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/04/25 15:16:52 by adjoly ### ########.fr */
/* Updated: 2025/04/25 17:20:12 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -73,8 +73,6 @@ class Server {
Client *_getClient(int);
void _destroy_clients(void);
config::Config
*_conf; // Pointer to the configuration class (with all config in)
Logger *_log; // Pointer to the log class

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/25 15:28:08 by adjoly ### ########.fr */
/* Updated: 2025/04/25 17:03:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,10 +17,16 @@
using namespace webserv::server;
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
: _toClose(false), _pfd(pfd), _client_addr(socket), _Gconf(conf) {
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
log("", "Client", "constructor called");
}
Client::Client(const Client &cpy) {
if (this != &cpy) {
_pfd->fd = cpy._pfd->fd;
}
}
void Client::parse(void) {
std::string received_data;
char buffer[BUFFER_SIZE];
@ -83,6 +89,5 @@ void Client::answer(void) {
Client::~Client(void) {
log("", "Client", "destructor called");
//delete _pfd;
delete (http::Get *)(this->_request);
}

View File

@ -6,11 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/25 15:21:43 by adjoly ### ########.fr */
/* Updated: 2025/04/25 17:21:54 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstring>
@ -145,11 +146,10 @@ void Server::_run(void) {
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLERR) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i - _fds_server.size()];
_client_data.erase(_client_data.begin() + i);
i--;
// close(_client_fds[i].fd);
//_client_fds.erase(_client_fds.begin() + i);
// delete _client_data[i - _fds_server.size()];
//_client_data.erase(_client_data.begin() + i);
} else if (_client_fds[i].revents & POLLIN) {
Client *client = _getClient(_client_fds[i].fd);
if (client == not_nullptr) {
@ -164,10 +164,18 @@ void Server::_run(void) {
continue;
}
client->answer();
_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) {
_client_fds.erase(it);
break;
}
}
close(_client_fds[i].fd);
}
}
_destroy_clients();
}
}

View File

@ -6,10 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/25 15:17:55 by adjoly ### ########.fr */
/* Updated: 2025/04/25 17:19:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include <algorithm>
#include <netinet/in.h>
#include <server/Client.hpp>
#include <server/default.hpp>
@ -17,6 +19,7 @@
#include <stdexcept>
#include <string>
#include <sys/socket.h>
#include <vector>
using namespace webserv::server;
@ -81,10 +84,11 @@ int Server::_createSocket(std::string host, int port) {
return (fd);
}
void Server::_destroy_clients() {
for (auto it = range(_client_data)) {
if ((*it)->isToClose()) {
delete (*it);
std::vector<struct pollfd>::iterator getPfd(int fd, std::vector<struct pollfd> &s) {
for (auto it = range(s)) {
if ((*it).fd == fd) {
return it;
}
}
return s.end();
}