mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 20:48:46 +02:00
「🔨」 fix: corrected invalid read and leak
This commit is contained in:
@ -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 14:33:54 by adjoly ### ########.fr */
|
/* Updated: 2025/04/25 15:15:48 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -37,9 +37,14 @@ class Client {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isToClose() {
|
||||||
|
return toClose;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _getRequest(std::string);
|
void _getRequest(std::string);
|
||||||
|
|
||||||
|
bool toClose;
|
||||||
struct pollfd *_pfd;
|
struct pollfd *_pfd;
|
||||||
struct sockaddr_in _client_addr;
|
struct sockaddr_in _client_addr;
|
||||||
http::IRequest *_request;
|
http::IRequest *_request;
|
||||||
|
@ -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 14:52:50 by adjoly ### ########.fr */
|
/* Updated: 2025/04/25 15:16:52 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -41,13 +41,6 @@ class Server {
|
|||||||
*/
|
*/
|
||||||
void _run(void);
|
void _run(void);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Used to handle client request
|
|
||||||
*
|
|
||||||
* @param the position in the _client_data
|
|
||||||
*/
|
|
||||||
bool _handle_client(Client *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Can be used to fill the vector passed as parameters with all the
|
* @brief Can be used to fill the vector passed as parameters with all the
|
||||||
* port and host in the config
|
* port and host in the config
|
||||||
@ -80,6 +73,8 @@ class Server {
|
|||||||
|
|
||||||
Client *_getClient(int);
|
Client *_getClient(int);
|
||||||
|
|
||||||
|
void _destroy_clients(void);
|
||||||
|
|
||||||
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
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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 14:35:37 by adjoly ### ########.fr */
|
/* Updated: 2025/04/25 15:19:43 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -83,6 +83,6 @@ void Client::answer(void) {
|
|||||||
|
|
||||||
Client::~Client(void) {
|
Client::~Client(void) {
|
||||||
log("➖", "Client", "destructor called");
|
log("➖", "Client", "destructor called");
|
||||||
delete _pfd;
|
//delete _pfd;
|
||||||
delete (http::Get *)(this->_request);
|
delete (http::Get *)(this->_request);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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 14:54:42 by adjoly ### ########.fr */
|
/* Updated: 2025/04/25 15:21:43 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ using namespace webserv::server;
|
|||||||
|
|
||||||
extern int _sig;
|
extern int _sig;
|
||||||
|
|
||||||
Client *Server::_getClient(int fd) {
|
Client *Server::_getClient(int fd) {
|
||||||
for (auto it = range(_client_data)) {
|
for (auto it = range(_client_data)) {
|
||||||
if (*(*it) == fd) {
|
if (*(*it) == fd) {
|
||||||
return *it;
|
return *it;
|
||||||
@ -108,6 +108,8 @@ void Server::_run(void) {
|
|||||||
// to add signal instead of 727
|
// to add signal instead of 727
|
||||||
while (727 - sigHandling()) {
|
while (727 - sigHandling()) {
|
||||||
if (poll(_client_fds.data(), _client_fds.size(), -1) < 0) {
|
if (poll(_client_fds.data(), _client_fds.size(), -1) < 0) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
str << "poll failed : ";
|
str << "poll failed : ";
|
||||||
str << strerror(errno);
|
str << strerror(errno);
|
||||||
@ -136,27 +138,36 @@ void Server::_run(void) {
|
|||||||
pfd.events = POLLIN | POLLOUT;
|
pfd.events = POLLIN | POLLOUT;
|
||||||
pfd.revents = 0;
|
pfd.revents = 0;
|
||||||
_client_fds.push_back(pfd);
|
_client_fds.push_back(pfd);
|
||||||
struct pollfd *ppfd = _client_fds.data() + _client_fds.size() -1;
|
struct pollfd *ppfd = _client_fds.data() + _client_fds.size() - 1;
|
||||||
Client *new_client = new Client(ppfd, 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) {
|
||||||
Client *client = _getClient(_client_fds[i].fd);
|
if (_client_fds[i].revents & POLLERR) {
|
||||||
if (client == not_nullptr) {
|
close(_client_fds[i].fd);
|
||||||
_log->error("client does not exist");
|
_client_fds.erase(_client_fds.begin() + i);
|
||||||
continue;
|
delete _client_data[i - _fds_server.size()];
|
||||||
}
|
_client_data.erase(_client_data.begin() + i);
|
||||||
if (_client_fds[i].revents & POLLIN) {
|
i--;
|
||||||
if (_handle_client(client)) {
|
} else if (_client_fds[i].revents & POLLIN) {
|
||||||
close(_client_fds[i].fd);
|
Client *client = _getClient(_client_fds[i].fd);
|
||||||
_client_fds.erase(_client_fds.begin() + i);
|
if (client == not_nullptr) {
|
||||||
delete _client_data[i];
|
_log->error("client does not exist");
|
||||||
_client_data.erase(_client_data.begin() + i);
|
continue;
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
|
client->parse();
|
||||||
|
} else if (_client_fds[i].revents & POLLOUT) {
|
||||||
|
Client *client = _getClient(_client_fds[i].fd);
|
||||||
|
if (client == not_nullptr) {
|
||||||
|
_log->error("client does not exist");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
client->answer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_destroy_clients();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
/* 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 14:53:17 by adjoly ### ########.fr */
|
/* Updated: 2025/04/25 15:17:55 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <server/Client.hpp>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <server/Client.hpp>
|
||||||
#include <server/default.hpp>
|
#include <server/default.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -74,22 +74,17 @@ int Server::_createSocket(std::string host, int port) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
std::ostringstream str;
|
std::ostringstream str;
|
||||||
str << port;
|
str << port;
|
||||||
throw std::runtime_error("listen failed for : " + host + ":" + str.str());
|
throw std::runtime_error("listen failed for : " + host + ":" +
|
||||||
|
str.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
return (fd);
|
return (fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Server::_handle_client(Client *client) {
|
void Server::_destroy_clients() {
|
||||||
try {
|
for (auto it = range(_client_data)) {
|
||||||
client->parse();
|
if ((*it)->isToClose()) {
|
||||||
client->answer();
|
delete (*it);
|
||||||
} catch (std::runtime_error &e) {
|
}
|
||||||
_log->error(e.what());
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user