🔨」 fix: fixed mainloop

This commit is contained in:
2025-04-29 14:27:28 +02:00
parent 640ed6ce3c
commit c696b8c7e2
10 changed files with 102 additions and 29 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/04/22 14:32:50 by adjoly ### ########.fr #
# Updated: 2025/04/28 14:57:54 by adjoly ### ########.fr #
# #
# **************************************************************************** #
@ -24,7 +24,7 @@ SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP -g
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP -g #-fsanitize=address
RED = \033[0;31m
GREEN = \033[0;32m

40
exemples/local.toml Normal file
View File

@ -0,0 +1,40 @@
log_file = "test.log"
[server]
server_names = { "localhost", "2B5.local" }
host = "0.0.0.0"
port = 8080
[server.error_pages]
404 = "not_found.html"
401 = "unauthorized.html"
402 = "uwu.html"
[server.location./]
methods = { "GET" }
root = "./html"
dirlist = true
client_max_body_size = "10M"
[server.location./api]
methods = { "GET", "POST" }
root = "./api"
upload_path = "./up"
cgi.go = "/bin/go"
[server.location./redir]
redirect = "https://kanel.ovh"
[serverr]
server_names = { "ptnnnn.local"}
host = "127.0.0.1"
port = 9090
[serverr.error_pages]
404 = "existepasfd***.html"
[serverr.location./]
methods = { "GET", "DELETE" }
root = "/var/lib/docker/volumes/test_data/_data"
dirlist = false
cgi.py = "/bin/python"

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
/* Updated: 2025/04/22 16:14:25 by adjoly ### ########.fr */
/* Updated: 2025/04/28 14:29:54 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,7 +25,7 @@ namespace webserv {
*
* @note Only work if VERBOSE mode is active
*/
static inline void log(std::string emoji, std::string who, std::string str) {
inline void log(std::string emoji, std::string who, std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "」debug: " << str << std::endl;
@ -40,10 +40,10 @@ static inline void log(std::string emoji, std::string who, std::string str) {
class Logger {
public:
Logger(void) : _ttyOnly(true) {
log("", "Logger", "default constructor called");
//log("", "Logger", "default constructor called");
}
Logger(const std::string &fileName) : _fileName(fileName) {
log("", "Logger", "filename constructor called");
//log("", "Logger", "filename constructor called");
_file.open(fileName.c_str(), std::ios::app);
if (!_file.is_open() && !_ttyOnly) {
_ttyOnly = true;
@ -53,7 +53,7 @@ class Logger {
}
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
log("", "Logger", "copy constructor called");
//log("", "Logger", "copy constructor called");
if (!other._ttyOnly) {
_file.open(other._fileName.c_str(), std::ios::app);
if (!_file.is_open()) {
@ -109,7 +109,7 @@ class Logger {
}
}
void debug(const std::string &msg) {
void debug(std::string msg) {
#ifdef VERBOSE
std::string ss = printPogitMsg("🏗️", "webserv", "debug", msg);
std::cerr << ss << std::endl;
@ -119,7 +119,6 @@ class Logger {
#else
(void)msg;
#endif
}
protected:

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 17:03:34 by adjoly ### ########.fr */
/* Updated: 2025/04/29 14:24:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,6 +30,8 @@ class Client {
void parse(void);
void answer(void);
bool requestParsed(void);
struct pollfd *getPollfd(void) const { return _pfd; }
bool operator==(int fd) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */
/* Updated: 2025/04/22 11:47:39 by mmoussou ### ########.fr */
/* Updated: 2025/04/28 14:29:20 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ void _printHelp(void) {
}
void _generateConf(void) {
webserv::Logger _log("");
webserv::Logger _log;
if (access("./sample.conf", F_OK) == 0) {
_log.warn(std::string(SAMPLE_CONF_PATH) + " already exist, aborting");
} else {

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/04/25 12:36:17 by mmoussou ### ########.fr */
/* Updated: 2025/04/28 14:32:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -226,7 +226,9 @@ body {\n\
response.setStatusCode(200);
response.addHeader("Content-Type", http::Mime::getType(this->_target));
_log->debug(response.str().c_str());
#ifdef VERBOSE
//_log->debug(response.str().c_str());
#endif
}
}
catch (...)

View File

@ -6,12 +6,13 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
/* Updated: 2025/04/23 14:30:28 by mmoussou ### ########.fr */
/* Updated: 2025/04/29 12:35:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <requests/HttpResponse.hpp>
#include <requests/Errors.hpp>
#include <webserv.hpp>
/*
- do a map of all the status_text and get it from here, not storing them
@ -38,7 +39,7 @@ std::string http::Response::str(void) const
response << this->_protocol << " " << this->_status_code << " " << this->_status_text;
response << "\r\n";
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
for (auto it = range(_headers))
response << it->first << ": " << it->second << "\r\n";
response << "\r\n";

View File

@ -6,10 +6,11 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/25 17:03:35 by adjoly ### ########.fr */
/* Updated: 2025/04/29 14:24:31 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include <log.hpp>
#include <server/Client.hpp>
#include <sstream>
@ -18,7 +19,8 @@ using namespace webserv::server;
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
log("", "Client", "constructor called");
_request = not_nullptr;
//log("", "Client", "constructor called");
}
Client::Client(const Client &cpy) {
@ -51,6 +53,13 @@ void Client::parse(void) {
// throw error
}
bool Client::requestParsed(void) {
if (_request == not_nullptr) {
return false;
}
return true;
}
void Client::_getRequest(std::string request_str) {
std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
@ -76,6 +85,10 @@ void Client::answer(void) {
(void)_client_addr;
std::string response;
if (_request == not_nullptr) {
return;
}
if (this->_request->getMethod() == "GET" ||
this->_request->getMethod() == "DELETE" ||
this->_request->getMethod() == "POST")
@ -85,9 +98,10 @@ void Client::answer(void) {
"text/html\r\n\r\n<html><body><h1>501 Not "
"Implemented</h1></body></html>";
send(_pfd->fd, response.c_str(), response.length(), 0);
_log->info("response sent");
}
Client::~Client(void) {
log("", "Client", "destructor called");
//log("", "Client", "destructor called");
delete (http::Get *)(this->_request);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/26 16:35:53 by adjoly ### ########.fr */
/* Updated: 2025/04/29 14:26:19 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -102,7 +102,7 @@ void Server::_run(void) {
fd.fd = *it;
fd.events = POLLIN;
_client_fds.push_back(fd);
_log->debug("new socket in poll");
//_log->debug("new socket in poll");
}
// to add signal instead of 727
@ -140,16 +140,22 @@ void Server::_run(void) {
_client_fds.push_back(pfd);
struct pollfd *ppfd = _client_fds.data() + _client_fds.size() - 1;
Client *new_client = new Client(ppfd, client_addr, _conf);
if (new_client == NULL) {
continue;
}
_client_data.push_back(new_client);
std::cout << "client pushed" << std::endl;
}
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLERR) {
close(_client_fds[i].fd);
_log->info("pollerr");
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i - _fds_server.size()];
delete _client_data[i - _fds_server.size()];
_client_data.erase(_client_data.begin() + i);
} else if (_client_fds[i].revents & POLLIN) {
_log->info("pollin");
Client *client = _getClient(_client_fds[i].fd);
if (client == not_nullptr) {
_log->error("client does not exist");
@ -157,29 +163,38 @@ void Server::_run(void) {
}
client->parse();
} else if (_client_fds[i].revents & POLLOUT) {
std::stringstream str;
str << _client_fds[i].fd;
_log->info("pollout = " + str.str());
Client *client = _getClient(_client_fds[i].fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
if (client->requestParsed() == false) {
continue;
}
client->answer();
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
for (auto it = range (_client_fds)) {
for (auto it = range(_client_fds)) {
if (_client_fds[i].fd == (*it).fd) {
std::cout << "client fds erased" << std::endl;
close(it.base()->fd);
_client_fds.erase(it);
break;
}
}
close(_client_fds[i].fd);
i++;
}
}
}
}
Server::Server(config::Config *conf) : _conf(conf) {
log("", "Server::Server", "config constructor called");
// log("", "Server::Server", "config constructor called");
_log = conf->getLogger();
try {
_setup();
@ -190,7 +205,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
}
Server::~Server(void) {
log("", "Server::Server", "destructor called");
// log("", "Server::Server", "destructor called");
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
it != _client_fds.end(); it++)
close(it->fd);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/25 17:19:55 by adjoly ### ########.fr */
/* Updated: 2025/04/28 14:30:27 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,7 +63,7 @@ int Server::_createSocket(std::string host, int port) {
}
std::stringstream str;
str << port;
_log->debug("port : " + str.str());
//_log->debug("port : " + str.str());
addr.sin_port = htons(port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {