diff --git a/Makefile b/Makefile index 971b463..a0d5c6c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 diff --git a/exemples/local.toml b/exemples/local.toml new file mode 100644 index 0000000..4552415 --- /dev/null +++ b/exemples/local.toml @@ -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" diff --git a/includes/log.hpp b/includes/log.hpp index 16209e6..f17b09a 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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: diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index 37a0dc5..26ef516 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { diff --git a/src/help.cpp b/src/help.cpp index 2982778..9cbcda6 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 { diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index b3a4c9e..6b2d67b 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _target)); - _log->debug(response.str().c_str()); +#ifdef VERBOSE + //_log->debug(response.str().c_str()); +#endif } } catch (...) diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index c38e4ec..5a9c329 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,12 +6,13 @@ /* By: mmoussou #include +#include /* - 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::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"; diff --git a/src/server/Client.cpp b/src/server/Client.cpp index ea44e6f..a1307be 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,10 +6,11 @@ /* By: mmoussou #include #include @@ -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

501 Not " "Implemented

"; 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); } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index e0320ed..892970c 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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::iterator it = _client_fds.begin(); it != _client_fds.end(); it++) close(it->fd); diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index 5c3f327..c383830 100644 --- a/src/server/ServerUtils.cpp +++ b/src/server/ServerUtils.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) {