From bb841f90c8d631e24323a9e55ae69e1e3df55e4c Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 12:13:27 +0200 Subject: [PATCH 01/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20started=20mainloop=20added=20help=20and=20file=20pars?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/help.hpp | 4 +- includes/log.hpp | 6 +- sample.conf | 9 +++ src/config/Server.cpp | 32 ++++++++-- src/help.cpp | 22 +++++-- src/main.cpp | 145 +++++++----------------------------------- test.toml | 0 7 files changed, 79 insertions(+), 139 deletions(-) create mode 100644 sample.conf create mode 100644 test.toml diff --git a/includes/help.hpp b/includes/help.hpp index 442cfc1..900ba33 100644 --- a/includes/help.hpp +++ b/includes/help.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */ -/* Updated: 2025/04/10 13:58:52 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:36:22 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,3 +14,5 @@ #define SAMPLE_CONF_PATH "./sample.conf" #define WEBSRV_VERSION "v0.1" + +bool help(int, char **); diff --git a/includes/log.hpp b/includes/log.hpp index 9d38ea3..b39558e 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/10 14:21:46 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:54:37 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,9 +103,9 @@ class Logger { os << type << "(" << what << "):" << msg; #else if (what.empty()) - os << "「" << emoji << "」" << type << ":" << msg; + os << "「" << emoji << "」" << type << ": " << msg; else - os << "「" << emoji << "」" << type << "(" << what << "):" << msg; + os << "「" << emoji << "」" << type << "(" << what << "): " << msg; #endif return os.str(); } diff --git a/sample.conf b/sample.conf new file mode 100644 index 0000000..531a1af --- /dev/null +++ b/sample.conf @@ -0,0 +1,9 @@ +[server] +host = "localhost" +port = 8080 + +[server.location./] +methods = { "GET" } +root = "/var/www/html" +dirlist = true +client_max_body_size = "10M" diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 06b4e77..1624708 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/03/26 08:47:50 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 12:12:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ #include "node/default.hpp" #include "tomlpp.hpp" #include +#include #include #include #include @@ -32,7 +33,7 @@ toml::ANode *Server::_getServerTable(void) { _table->getTable()->find("server"); if (table == _table->getTable()->end()) throw std::runtime_error( - "could not find any [server] table in config file :("); + "could not find any [server] table in config file :("); else serverT = table->second; return serverT; @@ -57,13 +58,23 @@ Server::Server(std::string file_name) { std::string log_file = *static_cast(val); } _log = new Logger(log_file); - _table = _getServerTable(); + try { + _table = _getServerTable(); + } catch(std::runtime_error &e) { + delete _table; + delete tomlFile; + delete _log; + throw e; + } // host and port parsing void *host = accessValue("host", toml::STRING, _table, _log); if (host != not_nullptr) { _host = *static_cast(host); } else { + delete _table; + delete tomlFile; + delete _log; throw std::runtime_error( "no host specified - please specify one in server.host"); } @@ -71,6 +82,9 @@ Server::Server(std::string file_name) { if (host != not_nullptr) { _port = *static_cast(port); } else { + delete _table; + delete tomlFile; + delete _log; throw std::runtime_error( "no port specified - please specify one in server.port"); } @@ -86,9 +100,11 @@ Server::Server(std::string file_name) { std::string str = *static_cast((*vecIt)->getValue()); _server_names->push_back(str); } - } else + } else { _log->warn( - "no server_names all request will be accepted from any hostname"); + "no server_names all request will be accepted from any hostname"); + _server_names = not_nullptr; + } // error_pages parsing map = static_cast *>( @@ -102,7 +118,8 @@ Server::Server(std::string file_name) { it = _table->accessIt("location", toml::TABLE, found); if (found == true && it != _table->getTable()->end()) { _routes = new std::map; - std::map *location_table = it->second->getTable(); + std::map *location_table = + it->second->getTable(); for (it = location_table->begin(); it != location_table->end(); it++) { if (_routes->find(it->first) != _routes->end()) continue; @@ -120,7 +137,8 @@ Server::~Server(void) { } delete _routes; delete _err_pages; - delete _server_names; + if (_server_names != not_nullptr) + delete _server_names; delete _log; // to see if nessecary } diff --git a/src/help.cpp b/src/help.cpp index 7bc3781..56b42d1 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/10 14:20:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:39:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ void _generateConf(void) { file << "[server]\nhost = \"localhost\"\nport = " "8080\n\n[server.location./]\nmethods = { \"GET\" }\nroot " "= \"/var/www/html\"\ndirlist = true\nclient_max_body_size " - "= \"10M\""; + "= \"10M\"\n"; file.close(); _log.info("config file successfully generated"); } else { @@ -50,16 +50,24 @@ void _printVersion(void) { std::cout << "You are running : Webserv " << WEBSRV_VERSION << std::endl; } -void help(int ac, char **av) { +bool help(int ac, char **av) { if (ac < 2) { _printHelp(); - return; + return true; } std::string option = av[1]; - if (option == "--help" || option == "-v") + if (option == "--help" || option == "-v") { _printHelp(); - else if (option == "--generate" || option == "-g") + return true; + } + else if (option == "--generate" || option == "-g") { _generateConf(); - else if (option == "--version" || option == "-v") + return true; + } + else if (option == "--version" || option == "-v") { _printVersion(); + return true; + } + else + return false; } diff --git a/src/main.cpp b/src/main.cpp index 5c7e217..13b1232 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,128 +12,31 @@ /* ************************************************************************** */ #include -#include -#include +#include +#include +#include #include +#include +#include +#include -#define PORT 8080 -#define BUFFER_SIZE 4096 +int main(int ac, char **av) { + if (help(ac, av)) { + return EXIT_SUCCESS; + } + std::cout << "Starting server..." << std::endl; + if (access(av[1], F_OK) < 0) { + std::cout << "File : " << av[1] << " could not be opened" << std::endl; + return EXIT_FAILURE; + } -int server_socket; -int client_socket; - -void close_socket(int signal) -{ - std::cerr << std::endl << "closing..." << std::endl; - close(client_socket); - close(server_socket); - exit(signal); -} - -std::string getMethod(std::string &data) -{ - return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); -} - -int main() -{ - // handle ctrl-C to close server socket - if (signal(SIGPIPE, SIG_IGN) == SIG_ERR || signal(SIGINT, close_socket) == SIG_ERR || signal(SIGQUIT, close_socket) == SIG_ERR) - { - std::cerr << "Error registering signal handlers!" << std::endl; - return 1; - } - - // create a socket - server_socket = socket(AF_INET, SOCK_STREAM, 0); - if (server_socket == -1) - { - std::cerr << "Failed to create socket" << std::endl; - return 1; - } - - // prepare the server address - sockaddr_in server_address; - std::memset(&server_address, 0, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_addr.s_addr = INADDR_ANY; - server_address.sin_port = htons(PORT); - - if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) - { - std::cerr << "Failed to bind socket" << std::endl; - return 1; - } - if (listen(server_socket, 5) == -1) - { - std::cerr << "Failed to listen on socket" << std::endl; - return 1; - } - - std::cout << "Server is listening on port " << PORT << std::endl; - - while (true) - { - // accept an incoming connection - sockaddr_in client_address; - socklen_t client_address_len = sizeof(client_address); - //int client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_len); - client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_len); - if (client_socket == -1) { - std::cerr << "Failed to accept connection" << std::endl; - continue; - } - - // receive the HTTP request - std::string received_data; - char buffer[BUFFER_SIZE]; - ssize_t bytes_received; - do - { - std::memset(buffer, 0, BUFFER_SIZE); - bytes_received = recv(client_socket, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received == -1) - { - std::cerr << "Failed to receive request" << std::endl; - close(client_socket); - continue; - } - received_data += std::string(buffer, bytes_received); - } - while (buffer[bytes_received]); - - // parse the request - - // handle the request - std::string response; - - //std::cout << received_data << std::endl; - std::cout << getMethod(received_data) << std::endl; - - if (getMethod(received_data) == "GET") - { - std::cout << "------------ GET REQUEST ------------" << std::endl; - http::Get request(received_data); - - response = request.execute().str(); - } - else if (getMethod(received_data) == "POST") - { - std::cout << "------------ POST REQUEST ------------" << std::endl; - http::Post request(received_data); - - response = request.execute().str(); - //std::cout << "worked" << std::endl; - } - else - { - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; - } - - send(client_socket, response.c_str(), response.length(), 0); - //close(client_socket); - } - - close(server_socket); - return 0; + config::Server *conf; + try { + conf = new config::Server(av[1]); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + return 1; + } + (void)conf; + delete conf; } diff --git a/test.toml b/test.toml new file mode 100644 index 0000000..e69de29 From b220361474fdf90f4b54e67f60ab2b33e027b510 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 15:17:32 +0200 Subject: [PATCH 02/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20added=20log=20debug=20function=20with=20VERBOSE=20mod?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 1 + includes/log.hpp | 46 ++++++++++++++++++++++++++++++++----------- lib/tomlpp | 2 +- src/config/Server.cpp | 15 +++++++------- src/main.cpp | 5 ++--- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index 777ed4b..f4b6ece 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -2,6 +2,7 @@ server_names = { "localhost", "2B5.local" } host = "localhost" port = 8080 +log_file = "test.log" [server.error_pages] 404 = "not_found.html" diff --git a/includes/log.hpp b/includes/log.hpp index b39558e..108f89e 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/11 11:54:37 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 15:09:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,22 +19,44 @@ #include namespace webserv { + +/** + * @brief Used to log debug message + * + * @note Only work if VERBOSE mode is active + */ +static inline void log(std::string emoji, std::string who, + std::string str) { +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」debug: " << str << std::endl; + else + std::cout << "「" << emoji << "」debug(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + class Logger { public: Logger(const std::string &fileName) : _fileName(fileName) { - if (fileName.empty()) + log("➕", "Logger", "filename constructor called"); + if (!fileName.empty()) { _ttyOnly = true; - else { + } else { + std::cout << "wtfff: " << _fileName << std::endl; _file.open(fileName.c_str(), std::ios::app); - _ttyOnly = false; - } - if (!_file.is_open() && !_ttyOnly) { - _ttyOnly = true; - warn("could not open log file, going tty only"); + if (!_file.is_open() && !_ttyOnly) { + _ttyOnly = true; + warn("could not open log file, going tty only"); + } else + _ttyOnly = false; } } Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { + log("➕", "Logger", "copy constructor called"); if (!other._ttyOnly) { _file.open(other._fileName.c_str(), std::ios::app); if (!_file.is_open()) { @@ -46,6 +68,7 @@ class Logger { // Copy assignment operator Logger &operator=(const Logger &other) { + log("➕", "Logger", "copy assignment constructor called"); if (this != &other) { if (_file.is_open()) { _file.close(); @@ -62,6 +85,7 @@ class Logger { return *this; } ~Logger(void) { + log("➖", "Logger", "destructor called"); if (_file.is_open()) _file.close(); } @@ -90,10 +114,8 @@ class Logger { protected: private: - std::string printPogitMsg(const std::string &emoji, - const std::string &type, - const std::string &what, - const std::string &msg) { + std::string printPogitMsg(const std::string &emoji, const std::string &type, + const std::string &what, const std::string &msg) { std::stringstream os; #ifdef TTY (void)emoji; diff --git a/lib/tomlpp b/lib/tomlpp index d9e5070..9926aa1 160000 --- a/lib/tomlpp +++ b/lib/tomlpp @@ -1 +1 @@ -Subproject commit d9e507093a3b37d35d5115d766e6d81044cacb00 +Subproject commit 9926aa1530c08923d092df755fd5c876e56a409c diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 1624708..a510406 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/11 12:12:56 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 14:56:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,12 +52,6 @@ Server::Server(std::string file_name) { std::map *map; _table = tomlFile->getParsedFile(); - void *val = _table->access("log_file", toml::STRING, found); - std::string log_file = ""; - if (found == true && val != not_nullptr) { - std::string log_file = *static_cast(val); - } - _log = new Logger(log_file); try { _table = _getServerTable(); } catch(std::runtime_error &e) { @@ -66,6 +60,13 @@ Server::Server(std::string file_name) { delete _log; throw e; } + void *val = _table->access("log_file", toml::STRING, found); + std::string log_file = ""; + if (found == true && val != not_nullptr) { + std::string log_file = *static_cast(val); + std::cout << log_file << std::endl; + } + _log = new Logger(log_file); // host and port parsing void *host = accessValue("host", toml::STRING, _table, _log); diff --git a/src/main.cpp b/src/main.cpp index 13b1232..6595318 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,7 @@ /* By: mmoussou getLogger()->info("testtt"); delete conf; } From 04e782a7800a96e30b740c89e77db3757215410d Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 15:20:17 +0200 Subject: [PATCH 03/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20log=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/log.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/log.hpp b/includes/log.hpp index 108f89e..489f227 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/11 15:09:59 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 15:19:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ static inline void log(std::string emoji, std::string who, std::cout << "「" << emoji << "」debug(" << who << "): " << str << std::endl; #else - (void)emoji, (void)what, (void)who, (void)str; + (void)emoji, (void)who, (void)str; #endif } @@ -45,7 +45,6 @@ class Logger { if (!fileName.empty()) { _ttyOnly = true; } else { - std::cout << "wtfff: " << _fileName << std::endl; _file.open(fileName.c_str(), std::ios::app); if (!_file.is_open() && !_ttyOnly) { _ttyOnly = true; From 521bc49d48c4a3534c4762b7cc64e4ada9a1d047 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 15:57:02 +0200 Subject: [PATCH 04/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20log=20file=20writing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/log.hpp | 4 ++-- src/config/Server.cpp | 5 ++--- test.toml | 0 3 files changed, 4 insertions(+), 5 deletions(-) delete mode 100644 test.toml diff --git a/includes/log.hpp b/includes/log.hpp index 489f227..c678cb3 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/11 15:19:58 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 15:49:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ class Logger { public: Logger(const std::string &fileName) : _fileName(fileName) { log("➕", "Logger", "filename constructor called"); - if (!fileName.empty()) { + if (fileName.empty()) { _ttyOnly = true; } else { _file.open(fileName.c_str(), std::ios::app); diff --git a/src/config/Server.cpp b/src/config/Server.cpp index a510406..a26bd06 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/11 14:56:56 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 15:44:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,8 +63,7 @@ Server::Server(std::string file_name) { void *val = _table->access("log_file", toml::STRING, found); std::string log_file = ""; if (found == true && val != not_nullptr) { - std::string log_file = *static_cast(val); - std::cout << log_file << std::endl; + log_file = *static_cast(val); } _log = new Logger(log_file); diff --git a/test.toml b/test.toml deleted file mode 100644 index e69de29..0000000 From 1c79dbcb13bbf27528b6dfa114bd30bd6f9eb826 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 16:04:42 +0200 Subject: [PATCH 05/45] =?UTF-8?q?=E3=80=8C=F0=9F=97=91=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20clean:=20cleaned=20logfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/main.cpp | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2a54193..383646c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ obj/ .direnv/ compile_commands.json .cache +*.log diff --git a/src/main.cpp b/src/main.cpp index 6595318..64a673f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou getLogger()->info("testtt"); delete conf; } From 20a0bf80de2f498abc27bbef86917528ae44210c Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 19:23:44 +0200 Subject: [PATCH 06/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20started=20server=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/default.hpp | 12 +++--- includes/server/server.hpp | 54 ++++++++++++++++++++++++++ src/main.cpp | 3 +- src/setup.cpp | 76 +++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 includes/server/server.hpp create mode 100644 src/setup.cpp diff --git a/includes/server/default.hpp b/includes/server/default.hpp index 1f956c6..e2061d4 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,20 +6,18 @@ /* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ +/* Updated: 2025/04/11 19:22:43 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "log.hpp" +#include +#include +#include +#include +#include + +namespace webserv { + +class Server { + public: + Server(config::Server *); + ~Server(void); + + protected: + private: + /** + * @brief Used to setup the webserver (primarly socket) + */ + void _setup(void); + /** + * @brief Used to run the webserver + */ + void _run(void); + + /** + * @brief Used to handle client request + * + * @param The number of the client fd + */ + void _handle_client(int); + + config::Server + *_conf; ///> Pointer to the configuration class (with all config in) + Logger *_log; ///> Pointer to the log class + int _fd_server; ///> The fd of the socket + std::vector _client_fds; ///> A vector of all the poll fd +}; + +}; // namespace webserv diff --git a/src/main.cpp b/src/main.cpp index 64a673f..983fd62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ +/* Updated: 2025/04/11 19:22:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "log.hpp" +#include "server/default.hpp" +#include +#include +#include +#include +#include + +using namespace webserv; + +void Server::_setup(void) { + _fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (_fd_server == -1) + throw std::runtime_error("error at socket setup"); + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(_conf->getPort()); + + if (bind(_fd_server, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + close(_fd_server); + throw std::runtime_error("error when binding address"); + } + + if (listen(_fd_server, SOMAXCONN) < 0) { + close(_fd_server); + throw std::runtime_error("error when listening"); + } +} + +void Server::_run(void) { + _client_fds[0].fd = _fd_server; + _client_fds[0].events = POLLIN; + + int nbr_client = 0; + + while (727) { + int ret = poll(_client_fds.data(), nbr_client + 1, -1); + if (ret < 0) { + close(_fd_server); + throw std::runtime_error("poll failed :("); + } + + if (_client_fds[0].revents & POLLIN) { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + int client_fd = + accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); + if (client_fd < 0) { + _log->error("accept failed"); + continue; + } + //if (nbr_client ) TODO do we need a max client probably not + } + for (int i = 1; i <= nbr_client; ++i) { + if (_client_fds[i].revents & POLLIN) { + _handle_client(i); + close(_client_fds[i].fd); + _client_fds[i] = _client_fds[nbr_client--]; + } + } + } +} From 9bb74f82833e4ef5e1c642aabbf5d16586e581fc Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 12 Apr 2025 10:17:55 +0200 Subject: [PATCH 07/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20added=20constructor=20and=20destructor=20in=20server:?= =?UTF-8?q?:server=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{setup.cpp => server.cpp} | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) rename src/{setup.cpp => server.cpp} (78%) diff --git a/src/setup.cpp b/src/server.cpp similarity index 78% rename from src/setup.cpp rename to src/server.cpp index 359f482..e50f519 100644 --- a/src/setup.cpp +++ b/src/server.cpp @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* setup.cpp :+: :+: :+: */ +/* server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/11 19:22:50 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 10:16:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "log.hpp" -#include "server/default.hpp" #include +#include #include +#include #include #include #include @@ -63,7 +63,7 @@ void Server::_run(void) { _log->error("accept failed"); continue; } - //if (nbr_client ) TODO do we need a max client probably not + // if (nbr_client ) TODO do we need a max client probably not :D } for (int i = 1; i <= nbr_client; ++i) { if (_client_fds[i].revents & POLLIN) { @@ -74,3 +74,16 @@ void Server::_run(void) { } } } + +Server::Server(config::Server *conf) : _conf(conf) { + log("➕", "Server::Server", "config constructor called"); + _log = conf->getLogger(); +} + +Server::~Server(void) { + log("➖", "Server::Server", "destructor called"); + for (std::vector::iterator it = _client_fds.begin(); + it != _client_fds.end(); it++) + close(it->fd); + close(_fd_server); +} From c5d8abb3d65a68e06a27f460eb16e07374309c7a Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 12 Apr 2025 11:19:19 +0200 Subject: [PATCH 08/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20n?= =?UTF-8?q?ow=20lauhcning=20at=20constrution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/server.cpp b/src/server.cpp index e50f519..b2bcaa7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/12 10:16:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 11:17:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,6 +78,8 @@ void Server::_run(void) { Server::Server(config::Server *conf) : _conf(conf) { log("➕", "Server::Server", "config constructor called"); _log = conf->getLogger(); + _setup(); + _run(); } Server::~Server(void) { From f185a811d8bfb1b5b88718ab173307a91f2bd061 Mon Sep 17 00:00:00 2001 From: y-syo Date: Sat, 12 Apr 2025 18:57:41 +0200 Subject: [PATCH 09/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip(server/=5Frun):=20fixed=20=5Frun=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/server.hpp | 4 ++-- src/main.cpp | 6 ++++- src/server.cpp | 45 ++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/includes/server/server.hpp b/includes/server/server.hpp index d528979..b15dab2 100644 --- a/includes/server/server.hpp +++ b/includes/server/server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/11 19:22:43 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 15:39:14 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ class Server { * * @param The number of the client fd */ - void _handle_client(int); + void _handle_client(int fd); config::Server *_conf; ///> Pointer to the configuration class (with all config in) diff --git a/src/main.cpp b/src/main.cpp index 983fd62..77512a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,12 @@ /* By: mmoussou +#include #include #include #include @@ -37,5 +38,8 @@ int main(int ac, char **av) { return 1; } + webserv::Server *serv = new webserv::Server(conf); + + delete serv; delete conf; } diff --git a/src/server.cpp b/src/server.cpp index b2bcaa7..b405c4b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/12 11:17:30 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 18:53:45 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,11 @@ using namespace webserv; +void Server::_handle_client(int fd) +{ + (void) fd; +} + void Server::_setup(void) { _fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (_fd_server == -1) @@ -42,35 +47,53 @@ void Server::_setup(void) { } void Server::_run(void) { - _client_fds[0].fd = _fd_server; - _client_fds[0].events = POLLIN; + struct pollfd fd; + + fd.fd = _fd_server; + fd.events = POLLIN; + _client_fds.clear(); + _client_fds.push_back(fd); int nbr_client = 0; - while (727) { + while (727) + { int ret = poll(_client_fds.data(), nbr_client + 1, -1); - if (ret < 0) { + if (ret < 0) + { close(_fd_server); throw std::runtime_error("poll failed :("); } - if (_client_fds[0].revents & POLLIN) { + if (_client_fds[0].revents & POLLIN) + { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); - if (client_fd < 0) { + int client_fd = accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); + if (client_fd < 0) + { _log->error("accept failed"); continue; } + struct pollfd client_pollfd; + client_pollfd.fd = client_fd; + client_pollfd.events = POLLIN; + _client_fds.push_back(client_pollfd); + ++nbr_client; + // if (nbr_client ) TODO do we need a max client probably not :D } - for (int i = 1; i <= nbr_client; ++i) { - if (_client_fds[i].revents & POLLIN) { + for (int i = 1; i <= nbr_client; ) + { + if (_client_fds[i].revents & POLLIN) + { _handle_client(i); close(_client_fds[i].fd); _client_fds[i] = _client_fds[nbr_client--]; + _client_fds.pop_back(); } + else + ++i; } } } From 5fa656c4532f17ad35594f9820b22ff8d4dfbddb Mon Sep 17 00:00:00 2001 From: y-syo Date: Sun, 13 Apr 2025 11:54:26 +0200 Subject: [PATCH 10/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(src/se?= =?UTF-8?q?rver.cpp):=20handle=5Fclient=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/webserv.hpp | 4 ++- src/server.cpp | 61 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/includes/webserv.hpp b/includes/webserv.hpp index a519920..43b7042 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include +#define BUFFER_SIZE 4096 + namespace webserv { } //-namespace webserv diff --git a/src/server.cpp b/src/server.cpp index b405c4b..7f8efc9 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/12 18:53:45 by mmoussou ### ########.fr */ +/* Updated: 2025/04/13 11:53:40 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,64 @@ #include #include #include +#include using namespace webserv; +std::string getMethod(std::string &data) +{ + return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); +} + void Server::_handle_client(int fd) { (void) fd; + + std::string received_data; + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do + { + std::memset(buffer, 0, BUFFER_SIZE); + bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); + if (bytes_received == -1) + { + _log->error("failed to receive request"); + continue; + } + received_data += std::string(buffer, bytes_received); + } + while (buffer[bytes_received]); + + std::string response; + + if (getMethod(received_data) == "GET") + { + _log->info("get request received"); + http::Get request(received_data); + + response = request.execute().str(); + } + else if (getMethod(received_data) == "DELETE") + { + _log->info("delete request received"); + http::Delete request(received_data); + + response = request.execute().str(); + } + else if (getMethod(received_data) == "POST") + { + _log->info("post request received"); + http::Post request(received_data); + + response = request.execute().str(); + } + else + { + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + } + + send(fd, response.c_str(), response.length(), 0); } void Server::_setup(void) { @@ -46,7 +98,8 @@ void Server::_setup(void) { } } -void Server::_run(void) { +void Server::_run(void) +{ struct pollfd fd; fd.fd = _fd_server; @@ -81,13 +134,13 @@ void Server::_run(void) { _client_fds.push_back(client_pollfd); ++nbr_client; - // if (nbr_client ) TODO do we need a max client probably not :D + // if (nbr_client) TODO do we need a max client probably not :D } for (int i = 1; i <= nbr_client; ) { if (_client_fds[i].revents & POLLIN) { - _handle_client(i); + _handle_client(_client_fds[i].fd); close(_client_fds[i].fd); _client_fds[i] = _client_fds[nbr_client--]; _client_fds.pop_back(); From c21612d8732563e5730ecadf045ceefdacbdc54c Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 12 Apr 2025 11:23:32 +0200 Subject: [PATCH 11/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20upd?= =?UTF-8?q?ated=20flake?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flake.lock | 26 +++++++++++++------------- flake.nix | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/flake.lock b/flake.lock index a62f218..40b4963 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1722555600, - "narHash": "sha256-XOQkdLafnb/p9ij77byFQjDf5m5QYl9b2REiVClC+x4=", + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "8471fe90ad337a8074e957b69ca4d0089218391d", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", "type": "github" }, "original": { @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1732014248, - "narHash": "sha256-y/MEyuJ5oBWrWAic/14LaIr/u5E0wRVzyYsouYY3W6w=", + "lastModified": 1744232761, + "narHash": "sha256-gbl9hE39nQRpZaLjhWKmEu5ejtQsgI5TWYrIVVJn30U=", "owner": "nixos", "repo": "nixpkgs", - "rev": "23e89b7da85c3640bbc2173fe04f4bd114342367", + "rev": "f675531bc7e6657c10a18b565cfebd8aa9e24c14", "type": "github" }, "original": { @@ -36,14 +36,14 @@ }, "nixpkgs-lib": { "locked": { - "lastModified": 1722555339, - "narHash": "sha256-uFf2QeW7eAHlYXuDktm9c25OxOyCoUOQmh5SZ9amE5Q=", + "lastModified": 1733096140, + "narHash": "sha256-1qRH7uAUsyQI7R1Uwl4T+XvdNv778H0Nb5njNrqvylY=", "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz" + "url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz" }, "original": { "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/a5d394176e64ab29c852d03346c1fc9b0b7d33eb.tar.gz" + "url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz" } }, "pogit": { @@ -54,11 +54,11 @@ ] }, "locked": { - "lastModified": 1730655101, - "narHash": "sha256-gQiaaNAXSgQc2va1zqxrRA6X0Lr5kAtOO8HH3A5I20E=", + "lastModified": 1738098586, + "narHash": "sha256-jibZsqeSh74PLIsOVdp2jIDiYxTHzlVaat0AXRcpeiU=", "owner": "y-syo", "repo": "pogit", - "rev": "9de63350cf2e8297c9038f17cb5c2365bdf5cfa5", + "rev": "29a0535fea029e1c5e7762f187fc259f93927e31", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 8253a7a..e9ff69b 100644 --- a/flake.nix +++ b/flake.nix @@ -30,7 +30,7 @@ git gdb inputs.pogit.packages.${pkgs.system}.default - python312Packages.compiledb + compiledb ]; }; }); From 81b837eceb9bb9a889615a378c29e7b19a42c752 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:25:20 +0200 Subject: [PATCH 12/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20new=20config=20should=20be=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 36 +++++++++++++++++++ includes/config/Server.hpp | 16 ++------- includes/config/default.hpp | 3 +- includes/log.hpp | 22 ++++++------ src/config/Config.cpp | 45 ++++++++++++++++++++++++ src/config/Server.cpp | 69 +++++-------------------------------- 6 files changed, 104 insertions(+), 87 deletions(-) create mode 100644 includes/config/Config.hpp create mode 100644 src/config/Config.cpp diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp new file mode 100644 index 0000000..a7b2b60 --- /dev/null +++ b/includes/config/Config.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Config.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ +/* Updated: 2025/04/14 12:55:46 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "config/default.hpp" + +namespace webserv { +namespace config { + +class Config { + public: + Config(std::string &); + ~Config(); + + Logger *getLogger(void) { return _log; } + + std::vector *getServers(void) { return _servers; } + + private: + toml::ANode *_table; + Logger *_log; + std::vector *_servers; +}; + +}; // namespace config +}; // namespace webserv diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 34c3964..41cf1ee 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/03/25 17:56:34 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:39:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ namespace config { class Server { public: - Server(std::string); + Server(toml::ANode *, Logger *); ~Server(); /** @@ -48,11 +48,6 @@ class Server { return not_nullptr; } - /** - * @brief Can be used to get the Logger pointer - */ - Logger *getLogger(void) { return _log; } - // @brief Can be used to get a server name std::vector *getServerNames(void) { return _server_names; } // @brief Can be used to get the host specified in the config file @@ -79,13 +74,6 @@ class Server { std::map * _parseErrPages(std::map *table); - - /** - * @brief Can be used to get the [server] table in _table - * - * @return A pointer to the [server] table as an ANode - */ - toml::ANode *_getServerTable(void); }; } // namespace config diff --git a/includes/config/default.hpp b/includes/config/default.hpp index 5ff94dc..7713797 100644 --- a/includes/config/default.hpp +++ b/includes/config/default.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ -/* Updated: 2025/03/26 08:39:08 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:54:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "Route.hpp" #include "Server.hpp" +#include "Config.hpp" #include "cppeleven.hpp" #include "node/Table.hpp" #include "node/default.hpp" diff --git a/includes/log.hpp b/includes/log.hpp index c678cb3..c973c46 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/11 15:49:38 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:03:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +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) { +static 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,18 +39,17 @@ static inline void log(std::string emoji, std::string who, class Logger { public: + Logger(void) : _ttyOnly(true) { + log("➕", "Logger", "default constructor called"); + } Logger(const std::string &fileName) : _fileName(fileName) { log("➕", "Logger", "filename constructor called"); - if (fileName.empty()) { + _file.open(fileName.c_str(), std::ios::app); + if (!_file.is_open() && !_ttyOnly) { _ttyOnly = true; - } else { - _file.open(fileName.c_str(), std::ios::app); - if (!_file.is_open() && !_ttyOnly) { - _ttyOnly = true; - warn("could not open log file, going tty only"); - } else - _ttyOnly = false; - } + warn("could not open log file, going tty only"); + } else + _ttyOnly = false; } Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { diff --git a/src/config/Config.cpp b/src/config/Config.cpp new file mode 100644 index 0000000..650d5b3 --- /dev/null +++ b/src/config/Config.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Config.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ +/* Updated: 2025/04/14 13:25:09 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cppeleven.hpp" +#include "node/ANode.hpp" +#include +#include + +using namespace webserv::config; + +Config::Config(std::string &filename) { + toml::Toml file(filename); + + try { + file.parse(); + } catch (std::exception &e) { + throw e; + } + + _table = file.getParsedFile(); + + bool found = false; + void *logFile = _table->access("log_file", toml::STRING, found); + if (found == true && logFile != not_nullptr) { + _log = new Logger(*static_cast(logFile)); + } else { + _log = new Logger(); + } + + std::map *node = _table->getTable(); + for (std::map::iterator it = node->begin(); + it != node->end(); it++) { + Server *srv = new Server(it->second, _log); + _servers->push_back(srv); + } +} diff --git a/src/config/Server.cpp b/src/config/Server.cpp index a26bd06..278ebd2 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,84 +6,33 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/11 15:44:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 12:57:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "config/Server.hpp" -#include "config/Route.hpp" -#include "cppeleven.hpp" -#include "log.hpp" -#include "node/ANode.hpp" -#include "node/Table.hpp" -#include "node/default.hpp" -#include "tomlpp.hpp" #include -#include -#include -#include -#include using namespace webserv::config; -toml::ANode *Server::_getServerTable(void) { - toml::ANode *serverT; - - std::map::iterator table = - _table->getTable()->find("server"); - if (table == _table->getTable()->end()) - throw std::runtime_error( - "could not find any [server] table in config file :("); - else - serverT = table->second; - return serverT; -} - -Server::Server(std::string file_name) { - toml::Toml *tomlFile = new toml::Toml(file_name); - - try { - tomlFile->parse(); - } catch (std::runtime_error &e) { - throw e; - } +Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { bool found; - std::map *map; - _table = tomlFile->getParsedFile(); - - try { - _table = _getServerTable(); - } catch(std::runtime_error &e) { - delete _table; - delete tomlFile; - delete _log; - throw e; - } - void *val = _table->access("log_file", toml::STRING, found); - std::string log_file = ""; - if (found == true && val != not_nullptr) { - log_file = *static_cast(val); - } - _log = new Logger(log_file); - - // host and port parsing + // host parsing void *host = accessValue("host", toml::STRING, _table, _log); if (host != not_nullptr) { _host = *static_cast(host); } else { delete _table; - delete tomlFile; delete _log; throw std::runtime_error( "no host specified - please specify one in server.host"); } + // port parsing void *port = accessValue("port", toml::INT, _table, _log); if (host != not_nullptr) { _port = *static_cast(port); } else { delete _table; - delete tomlFile; delete _log; throw std::runtime_error( "no port specified - please specify one in server.port"); @@ -102,13 +51,14 @@ Server::Server(std::string file_name) { } } else { _log->warn( - "no server_names all request will be accepted from any hostname"); + "no server_names all request will be accepted from any hostname"); _server_names = not_nullptr; } // error_pages parsing - map = static_cast *>( - accessValue("error_pages", toml::TABLE, _table, _log)); + std::map *map = + static_cast *>( + accessValue("error_pages", toml::TABLE, _table, _log)); if (map != not_nullptr) { _err_pages = _parseErrPages(map); } else @@ -126,8 +76,7 @@ Server::Server(std::string file_name) { (*_routes)[it->first] = new Route(it->second, _log); } } - delete tomlFile->getParsedFile(); - delete tomlFile; + delete _table; } Server::~Server(void) { From 5595a404d0eb9e8a9972ff9dfbfe56809491872e Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:33:07 +0200 Subject: [PATCH 13/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20added=20destructor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/Config.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 650d5b3..277313f 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,27 +6,19 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:25:09 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:32:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "cppeleven.hpp" -#include "node/ANode.hpp" #include -#include using namespace webserv::config; Config::Config(std::string &filename) { - toml::Toml file(filename); + toml::Toml *file = new toml::Toml(filename); - try { - file.parse(); - } catch (std::exception &e) { - throw e; - } - - _table = file.getParsedFile(); + file->parse(); + _table = file->getParsedFile(); bool found = false; void *logFile = _table->access("log_file", toml::STRING, found); @@ -42,4 +34,14 @@ Config::Config(std::string &filename) { Server *srv = new Server(it->second, _log); _servers->push_back(srv); } + delete _table; + delete file; +} + +Config::~Config(void) { + std::vector::iterator it = _servers->begin(); + for (; it != _servers->end() ; it++) { + delete *it; + } + delete _servers; } From bff58bad9225f7784e43fc72e5a73495ede42f1b Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:37:25 +0200 Subject: [PATCH 14/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20r?= =?UTF-8?q?emove=20useless=20=5Ftable=20in=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 3 +-- src/config/Config.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index a7b2b60..b1bbe47 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/14 12:55:46 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:36:42 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,6 @@ class Config { std::vector *getServers(void) { return _servers; } private: - toml::ANode *_table; Logger *_log; std::vector *_servers; }; diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 277313f..35eaec6 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:32:56 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 13:36:46 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "node/ANode.hpp" #include using namespace webserv::config; @@ -18,23 +19,23 @@ Config::Config(std::string &filename) { toml::Toml *file = new toml::Toml(filename); file->parse(); - _table = file->getParsedFile(); + toml::ANode *table = file->getParsedFile(); bool found = false; - void *logFile = _table->access("log_file", toml::STRING, found); + void *logFile = table->access("log_file", toml::STRING, found); if (found == true && logFile != not_nullptr) { _log = new Logger(*static_cast(logFile)); } else { _log = new Logger(); } - std::map *node = _table->getTable(); + std::map *node = table->getTable(); for (std::map::iterator it = node->begin(); it != node->end(); it++) { Server *srv = new Server(it->second, _log); _servers->push_back(srv); } - delete _table; + delete table; delete file; } From 42ab0cfc97f47e6decf12eb98583485dd700b202 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 13:43:42 +0200 Subject: [PATCH 15/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20a?= =?UTF-8?q?dapted=20constructor=20to=20the=20new=20config=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/server.hpp | 6 +++--- src/server.cpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/server/server.hpp b/includes/server/server.hpp index b15dab2..16e5915 100644 --- a/includes/server/server.hpp +++ b/includes/server/server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/12 15:39:14 by mmoussou ### ########.fr */ +/* Updated: 2025/04/14 13:42:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ namespace webserv { class Server { public: - Server(config::Server *); + Server(config::Config *); ~Server(void); protected: @@ -44,7 +44,7 @@ class Server { */ void _handle_client(int fd); - config::Server + config::Config *_conf; ///> Pointer to the configuration class (with all config in) Logger *_log; ///> Pointer to the log class int _fd_server; ///> The fd of the socket diff --git a/src/server.cpp b/src/server.cpp index 7f8efc9..641e293 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/13 11:53:40 by mmoussou ### ########.fr */ +/* Updated: 2025/04/14 13:41:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -151,7 +151,7 @@ void Server::_run(void) } } -Server::Server(config::Server *conf) : _conf(conf) { +Server::Server(config::Config *conf) : _conf(conf) { log("➕", "Server::Server", "config constructor called"); _log = conf->getLogger(); _setup(); From 2004386c8d3d674df97504300e4a2e259d12e81a Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 15:05:26 +0200 Subject: [PATCH 16/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20Added=20proto=20for=20Client=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Client.hpp | 33 +++++++++++++++++++++++++++++++++ includes/server/server.hpp | 5 +++-- includes/webserv.hpp | 3 ++- src/server.cpp | 2 +- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 includes/server/Client.hpp diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp new file mode 100644 index 0000000..a13a55a --- /dev/null +++ b/includes/server/Client.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Client.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ +/* Updated: 2025/04/14 15:00:41 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "config/default.hpp" +#include "default.hpp" +#include "requests/default.hpp" +#include +#include + +class Client { + public: + Client(int, sockaddr_in); + ~Client(void); + + void answer(void); + private: + int _fd; + struct sockaddr_in _client_addr; + http::IRequest *_request; + http::Response *_response; + config::Config *_conf; +}; diff --git a/includes/server/server.hpp b/includes/server/server.hpp index 16e5915..f0f75fe 100644 --- a/includes/server/server.hpp +++ b/includes/server/server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:42:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 14:21:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "log.hpp" #include #include +#include #include #include #include @@ -42,7 +43,7 @@ class Server { * * @param The number of the client fd */ - void _handle_client(int fd); + void _handle_client(Client); config::Config *_conf; ///> Pointer to the configuration class (with all config in) diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 43b7042..7fb6d3a 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,11 +6,12 @@ /* By: mmoussou #ifndef __WEBSERV_WEBSERV_HPP__ # define __WEBSERV_WEBSERV_HPP__ diff --git a/src/server.cpp b/src/server.cpp index 641e293..59d81df 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:41:59 by adjoly ### ########.fr */ +/* Updated: 2025/04/14 14:07:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ From a407ee51a31a0be45700caa8ff0f89be7a680a9c Mon Sep 17 00:00:00 2001 From: y-syo Date: Thu, 17 Apr 2025 13:11:18 +0200 Subject: [PATCH 17/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(server?= =?UTF-8?q?/client):=20added=20Client=20class=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/Errors.hpp | 10 +--- includes/server/Client.hpp | 16 +++++- src/server/Client.cpp | 105 +++++++++++++++++++++++++++++++++++ upload.html | 90 ------------------------------ 4 files changed, 120 insertions(+), 101 deletions(-) create mode 100644 src/server/Client.cpp delete mode 100644 upload.html diff --git a/includes/requests/Errors.hpp b/includes/requests/Errors.hpp index c57a05d..a31b50b 100644 --- a/includes/requests/Errors.hpp +++ b/includes/requests/Errors.hpp @@ -6,7 +6,7 @@ /* By: mmoussou ); static std::map message; private: diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index a13a55a..f1d5efc 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/14 15:00:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 12:46:04 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,26 @@ #include #include +namespace webserv { +namespace server { + class Client { public: - Client(int, sockaddr_in); + Client(int, sockaddr_in, config::Config *); ~Client(void); void answer(void); + private: + void _getRequest(std::string); + int _fd; struct sockaddr_in _client_addr; http::IRequest *_request; http::Response *_response; - config::Config *_conf; + config::Server *_conf; + Logger *_log; }; + +} // -namespace server +} // -namespace webserv diff --git a/src/server/Client.cpp b/src/server/Client.cpp new file mode 100644 index 0000000..29d6e85 --- /dev/null +++ b/src/server/Client.cpp @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Client.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +using namespace server; + +/*class Client { + public: + Client(int, sockaddr_in, config::Config *); + ~Client(void); + + void answer(void); + + private: + void getRequest(void); + + int _fd; + struct sockaddr_in _client_addr; + http::IRequest *_request; + http::Response *_response; + config::Server *_conf; + std::string _request_method; +};*/ + +Client::Client(int fd, sockaddr_in socket, config::Servr *conf, Logger *log) +{ + this->_fd = fd; + this->_client_addr = socket; + this->_conf = conf; + this->_log = log; + + std::string received_data; + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do + { + std::memset(buffer, 0, BUFFER_SIZE); + bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); + if (bytes_received == -1) + { + _log->error("failed to receive request"); + continue; + } + received_data += std::string(buffer, bytes_received); + } + while (buffer[bytes_received]); + + + this->getRequest(request_str); +} + +void Client::_getRequest(std::string request_str) +{ + std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1); + + if (method == "GET") + { + _log->info("get request received"); + this->_request = new http::Get(request_str); + } + else if (method == "DELETE") + { + _log->info("delete request received"); + this->_request = new http::Delete(request_str); + } + else if (method == "POST") + { + _log->info("post request received"); + this->_request = new http::Post(request_str); + } + else + { + _log->info("unsupported request received"); + this->_request = new http::Get(); + this->_request->setMethod("501"); + } +} + +void Client::answer(void) +{ + std::string response; + + if (this->_request == "GET" || this->_request == "DELETE" || this->_request == "POST") + response = this->_request.execute().str(); + else + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + send(this->_fd, response.c_str(), response.length(), 0); +} + + +Client::~Client(void) +{ + delete this->_request; + delete this->_response; +} diff --git a/upload.html b/upload.html deleted file mode 100644 index a9e575f..0000000 --- a/upload.html +++ /dev/null @@ -1,90 +0,0 @@ - - - - upload - - - - - -
-
- -

-

-
- - - - - From b98bec117b975da0cf843bca724d0408cdf56e7e Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 17 Apr 2025 13:12:46 +0200 Subject: [PATCH 18/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20Socket=20creation=20should=20be=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 +- exemples/test.toml | 3 +- includes/server/{server.hpp => Server.hpp} | 27 ++++- includes/server/default.hpp | 5 +- src/Client.cpp | 20 ++++ src/config/Server.cpp | 4 +- src/server.cpp | 124 ++++++++++----------- src/socket.cpp | 71 ++++++++++++ 8 files changed, 178 insertions(+), 79 deletions(-) rename includes/server/{server.hpp => Server.hpp} (63%) create mode 100644 src/Client.cpp create mode 100644 src/socket.cpp diff --git a/Makefile b/Makefile index e2cedc7..f9fb2b2 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/04/10 11:52:13 by mmoussou ### ########.fr # -# Updated: 2025/03/25 18:13:53 by adjoly ### ########.fr # +# Updated: 2025/04/15 17:53:00 by adjoly ### ########.fr # # # # **************************************************************************** # diff --git a/exemples/test.toml b/exemples/test.toml index f4b6ece..a513196 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -1,8 +1,9 @@ +log_file = "test.log" + [server] server_names = { "localhost", "2B5.local" } host = "localhost" port = 8080 -log_file = "test.log" [server.error_pages] 404 = "not_found.html" diff --git a/includes/server/server.hpp b/includes/server/Server.hpp similarity index 63% rename from includes/server/server.hpp rename to includes/server/Server.hpp index f0f75fe..2744e75 100644 --- a/includes/server/server.hpp +++ b/includes/server/Server.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* server.hpp :+: :+: :+: */ +/* Server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/14 14:21:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 11:48:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,11 +45,26 @@ class Server { */ void _handle_client(Client); + /** + * @brief Can be used to fill the vector passed as parameters with all the + * port and host in the config + * @param The vector of host + * @param The vector of port + */ + int _fillHostsPorts(std::vector &, std::vector &); + + /** + * @brief Can be used to open a socket with a specific port and host + * @param The host + * @param The port + */ + int _createSocket(std::string, int); + config::Config - *_conf; ///> Pointer to the configuration class (with all config in) - Logger *_log; ///> Pointer to the log class - int _fd_server; ///> The fd of the socket - std::vector _client_fds; ///> A vector of all the poll fd + *_conf; // Pointer to the configuration class (with all config in) + Logger *_log; // Pointer to the log class + std::vector _fds_server; // The fds of the sockets + std::vector _client_fds; // A vector of all the poll fd }; }; // namespace webserv diff --git a/includes/server/default.hpp b/includes/server/default.hpp index e2061d4..739752a 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,13 +6,14 @@ /* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 14:44:55 by adjoly #+# #+# */ +/* Updated: 2025/04/15 18:38:42 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +Client::Client(int fd, sockaddr_in sockData, config::Config *conf) + : _fd(fd), _client_addr(sockData) { + +} diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 278ebd2..dc33db3 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/14 12:57:30 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 11:22:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { } // port parsing void *port = accessValue("port", toml::INT, _table, _log); - if (host != not_nullptr) { + if (port != not_nullptr) { _port = *static_cast(port); } else { delete _table; diff --git a/src/server.cpp b/src/server.cpp index 59d81df..ee7b4c0 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,100 +6,98 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/14 14:07:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 12:37:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include +#include #include +#include #include #include #include -#include using namespace webserv; -std::string getMethod(std::string &data) -{ +std::string getMethod(std::string &data) { return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); } -void Server::_handle_client(int fd) -{ - (void) fd; - +void Server::_handle_client(int fd) { std::string received_data; - char buffer[BUFFER_SIZE]; - ssize_t bytes_received; - do - { + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do { std::memset(buffer, 0, BUFFER_SIZE); bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received == -1) - { + if (bytes_received == -1) { _log->error("failed to receive request"); continue; } received_data += std::string(buffer, bytes_received); - } - while (buffer[bytes_received]); + } while (buffer[bytes_received]); - std::string response; + std::string response; - if (getMethod(received_data) == "GET") - { + if (getMethod(received_data) == "GET") { _log->info("get request received"); - http::Get request(received_data); + http::Get request(received_data); response = request.execute().str(); - } - else if (getMethod(received_data) == "DELETE") - { + } else if (getMethod(received_data) == "DELETE") { _log->info("delete request received"); - http::Delete request(received_data); + http::Delete request(received_data); response = request.execute().str(); - } - else if (getMethod(received_data) == "POST") - { + } else if (getMethod(received_data) == "POST") { _log->info("post request received"); - http::Post request(received_data); + http::Post request(received_data); response = request.execute().str(); - } - else - { - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + } else { + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " + "text/html\r\n\r\n

501 Not " + "Implemented

"; } - send(fd, response.c_str(), response.length(), 0); + send(fd, response.c_str(), response.length(), 0); +} + +int Server::_fillHostsPorts(std::vector &hosts, + std::vector &ports) { + std::vector *config = _conf->getServers(); + + for (std::vector::iterator it = config->begin(); + it != config->end(); it++) { + hosts.push_back((*it)->getHost()); + ports.push_back((*it)->getPort()); + } + return config->size(); } void Server::_setup(void) { - _fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); - if (_fd_server == -1) - throw std::runtime_error("error at socket setup"); + std::vector hosts; + std::vector ports; - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons(_conf->getPort()); + int size = _fillHostsPorts(hosts, ports); + if (size < 1) + throw std::runtime_error("no server present in the config file"); - if (bind(_fd_server, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - close(_fd_server); - throw std::runtime_error("error when binding address"); - } - - if (listen(_fd_server, SOMAXCONN) < 0) { - close(_fd_server); - throw std::runtime_error("error when listening"); + std::vector::iterator itH = hosts.begin(); + for (std::vector::iterator itP = ports.begin(); itP != ports.end(); itP++, itH++) { + try { + int fd = _createSocket(*itH, *itP); + _fds_server.push_back(fd); + } catch (std::exception &e) { + throw e; + } } } -void Server::_run(void) -{ +void Server::_run(void) { struct pollfd fd; fd.fd = _fd_server; @@ -109,22 +107,19 @@ void Server::_run(void) int nbr_client = 0; - while (727) - { + while (727) { int ret = poll(_client_fds.data(), nbr_client + 1, -1); - if (ret < 0) - { + if (ret < 0) { close(_fd_server); throw std::runtime_error("poll failed :("); } - if (_client_fds[0].revents & POLLIN) - { + if (_client_fds[0].revents & POLLIN) { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); - int client_fd = accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); - if (client_fd < 0) - { + int client_fd = + accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); + if (client_fd < 0) { _log->error("accept failed"); continue; } @@ -136,16 +131,13 @@ void Server::_run(void) // if (nbr_client) TODO do we need a max client probably not :D } - for (int i = 1; i <= nbr_client; ) - { - if (_client_fds[i].revents & POLLIN) - { + for (int i = 1; i <= nbr_client;) { + if (_client_fds[i].revents & POLLIN) { _handle_client(_client_fds[i].fd); close(_client_fds[i].fd); _client_fds[i] = _client_fds[nbr_client--]; - _client_fds.pop_back(); - } - else + _client_fds.pop_back(); + } else ++i; } } diff --git a/src/socket.cpp b/src/socket.cpp new file mode 100644 index 0000000..2aac70e --- /dev/null +++ b/src/socket.cpp @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* socket.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ +/* Updated: 2025/04/17 12:35:31 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +using namespace webserv; + +bool convertStringToIP(const char *ip_str, struct in_addr *addr) { + // Split the IP string into four octets + unsigned int a, b, c, d; + if (sscanf(ip_str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) { + return false; + } + + // Check if each octet is within the valid range + if (a > 255 || b > 255 || c > 255 || d > 255) { + return false; + } + + // Combine the octets into a single 32-bit address + addr->s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d); + return true; +} + +int Server::_createSocket(std::string host, int port) { + int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (fd == -1) { + std::ostringstream str; + str << port; + throw std::runtime_error("socket binding failed for : " + host + ":" + + str.str()); + } + return -1; + + int opt = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { + close(fd); + throw std::runtime_error("setsockopt failed"); + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + convertStringToIP(host.c_str(), &addr.sin_addr); + addr.sin_port = htons(port); + + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + close(fd); + std::ostringstream str; + str << port; + throw std::runtime_error("bind failed for : " + host + ":" + str.str()); + } + + if (listen(fd, SOMAXCONN) < 0) { + close(fd); + std::ostringstream str; + str << port; + throw std::runtime_error("listen failed for : " + host + ":" + str.str()); + } + + return (fd); +} From 0862a76c30ed4bd365e40350eeb923d952f5c142 Mon Sep 17 00:00:00 2001 From: y-syo Date: Thu, 17 Apr 2025 14:26:26 +0200 Subject: [PATCH 19/45] =?UTF-8?q?=E3=80=8C=F0=9F=97=91=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20clean(client.cpp):=20cleaned=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.cpp | 20 -------------------- src/server/Client.cpp | 27 ++------------------------- 2 files changed, 2 insertions(+), 45 deletions(-) delete mode 100644 src/Client.cpp diff --git a/src/Client.cpp b/src/Client.cpp deleted file mode 100644 index eade040..0000000 --- a/src/Client.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* Client.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/15 14:44:55 by adjoly #+# #+# */ -/* Updated: 2025/04/15 18:38:42 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -Client::Client(int fd, sockaddr_in sockData, config::Config *conf) - : _fd(fd), _client_addr(sockData) { - -} diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 29d6e85..e520e79 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _fd = fd; - this->_client_addr = socket; - this->_conf = conf; - this->_log = log; - std::string received_data; char buffer[BUFFER_SIZE]; ssize_t bytes_received; From dac3df1a031666a963a000f00dd295d4eadf2d9d Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 17 Apr 2025 14:36:07 +0200 Subject: [PATCH 20/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20started=20=5Frun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 6 ++-- src/{server.cpp => server/Server.cpp} | 52 ++++++--------------------- 2 files changed, 13 insertions(+), 45 deletions(-) rename src/{server.cpp => server/Server.cpp} (74%) diff --git a/src/main.cpp b/src/main.cpp index 77512a2..d6f5158 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,11 @@ /* By: mmoussou +#include #include #include #include @@ -32,7 +32,7 @@ int main(int ac, char **av) { config::Server *conf; try { - conf = new config::Server(av[1]); + conf = new config::Config(std::string(av[1])); } catch (std::exception &e) { std::cout << e.what() << std::endl; return 1; diff --git a/src/server.cpp b/src/server/Server.cpp similarity index 74% rename from src/server.cpp rename to src/server/Server.cpp index ee7b4c0..9def9c2 100644 --- a/src/server.cpp +++ b/src/server/Server.cpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* server.cpp :+: :+: :+: */ +/* Server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/17 12:37:45 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 14:35:01 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,8 @@ void Server::_setup(void) { throw std::runtime_error("no server present in the config file"); std::vector::iterator itH = hosts.begin(); - for (std::vector::iterator itP = ports.begin(); itP != ports.end(); itP++, itH++) { + for (std::vector::iterator itP = ports.begin(); itP != ports.end(); + itP++, itH++) { try { int fd = _createSocket(*itH, *itP); _fds_server.push_back(fd); @@ -99,47 +100,15 @@ void Server::_setup(void) { void Server::_run(void) { struct pollfd fd; + int nbr_client = 0; - fd.fd = _fd_server; - fd.events = POLLIN; - _client_fds.clear(); - _client_fds.push_back(fd); - - int nbr_client = 0; + for (std::vector::iterator it = _fds_server.begin(); + it != _fds_server.end(); it++, nbr_client++) { + fd.fd = *it; + fd.events = POLLIN; + } while (727) { - int ret = poll(_client_fds.data(), nbr_client + 1, -1); - if (ret < 0) { - close(_fd_server); - throw std::runtime_error("poll failed :("); - } - - if (_client_fds[0].revents & POLLIN) { - struct sockaddr_in client_addr; - socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); - if (client_fd < 0) { - _log->error("accept failed"); - continue; - } - struct pollfd client_pollfd; - client_pollfd.fd = client_fd; - client_pollfd.events = POLLIN; - _client_fds.push_back(client_pollfd); - ++nbr_client; - - // if (nbr_client) TODO do we need a max client probably not :D - } - for (int i = 1; i <= nbr_client;) { - if (_client_fds[i].revents & POLLIN) { - _handle_client(_client_fds[i].fd); - close(_client_fds[i].fd); - _client_fds[i] = _client_fds[nbr_client--]; - _client_fds.pop_back(); - } else - ++i; - } } } @@ -155,5 +124,4 @@ Server::~Server(void) { for (std::vector::iterator it = _client_fds.begin(); it != _client_fds.end(); it++) close(it->fd); - close(_fd_server); } From 49fcb87395df441f90446b0d91db5e1a866df335 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 17 Apr 2025 18:22:05 +0200 Subject: [PATCH 21/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20main=20loop=20should=20be=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Server.hpp | 6 +-- src/server/Server.cpp | 81 +++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index 2744e75..86d3fba 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/17 11:48:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 18:08:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,9 +41,9 @@ class Server { /** * @brief Used to handle client request * - * @param The number of the client fd + * @param The fd of the client */ - void _handle_client(Client); + void _handle_client(int, sockaddr_in, config::Config *, struct pollfd); /** * @brief Can be used to fill the vector passed as parameters with all the diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 9def9c2..315c7d6 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,64 +6,40 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/17 14:35:01 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 18:21:34 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #include +#include #include #include -#include #include #include #include using namespace webserv; -std::string getMethod(std::string &data) { - return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); +std::string convertIPToString(const struct in_addr *addr) { + unsigned int ip = ntohl(addr->s_addr); + std::stringstream ss; + ss << ((ip >> 24) & 0xFF) << "." << ((ip >> 16) & 0xFF) << "." + << ((ip >> 8) & 0xFF) << "." << (ip & 0xFF); + return ss.str(); } -void Server::_handle_client(int fd) { - std::string received_data; - char buffer[BUFFER_SIZE]; - ssize_t bytes_received; - do { - std::memset(buffer, 0, BUFFER_SIZE); - bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received == -1) { - _log->error("failed to receive request"); - continue; - } - received_data += std::string(buffer, bytes_received); - } while (buffer[bytes_received]); +std::string convertPortToString(const struct sockaddr_in *sa) { + int port = ntohs(sa->sin_port); + std::stringstream ss; + ss << port; + return ss.str(); +} - std::string response; - - if (getMethod(received_data) == "GET") { - _log->info("get request received"); - http::Get request(received_data); - - response = request.execute().str(); - } else if (getMethod(received_data) == "DELETE") { - _log->info("delete request received"); - http::Delete request(received_data); - - response = request.execute().str(); - } else if (getMethod(received_data) == "POST") { - _log->info("post request received"); - http::Post request(received_data); - - response = request.execute().str(); - } else { - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " - "text/html\r\n\r\n

501 Not " - "Implemented

"; - } - - send(fd, response.c_str(), response.length(), 0); +std::string getMethod(std::string &data) { + return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); } int Server::_fillHostsPorts(std::vector &hosts, @@ -106,9 +82,32 @@ void Server::_run(void) { it != _fds_server.end(); it++, nbr_client++) { fd.fd = *it; fd.events = POLLIN; + _client_fds.push_back(fd); } while (727) { + int ret = poll(_client_fds.data(), nbr_client, -1); + if (ret < 0) { + _log->error("poll failed :("); + continue; + } + + for (int i = 0; i < nbr_client; i++) { + if (_client_fds[i].revents & POLLIN) { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + int client_fd = + accept(_client_fds[i].fd, (struct sockaddr *)&client_addr, + &addrlen); + if (client_fd < 0) { + _log->error("accept failed with : " + + convertIPToString(&client_addr.sin_addr) + ":" + + convertPortToString(&client_addr)); + continue ; + } + _handle_client(client_fd, client_addr, _conf, _client_fds[i]); + } + } } } From 0b77d7b80cbd699b5e79b3dcd98fd502203f98e6 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 17 Apr 2025 19:04:30 +0200 Subject: [PATCH 22/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20started=20client=20handling=20(need=20to=20redo=20a?= =?UTF-8?q?=20part=20of=20the=20client=20fd=20handling)=20not=20yet=20usab?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Client.hpp | 4 ++-- includes/server/Server.hpp | 4 ++-- src/server/Client.cpp | 27 +++++++++++++++++++++++++-- src/server/Server.cpp | 2 +- src/{socket.cpp => server/Socket.cpp} | 10 ++++++++-- 5 files changed, 38 insertions(+), 9 deletions(-) rename src/{socket.cpp => server/Socket.cpp} (87%) diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index f1d5efc..c074826 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/17 12:46:04 by mmoussou ### ########.fr */ +/* Updated: 2025/04/17 18:48:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ namespace server { class Client { public: - Client(int, sockaddr_in, config::Config *); + Client(int, sockaddr_in, config::Server *); ~Client(void); void answer(void); diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index 86d3fba..d8fa556 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/17 18:08:38 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 18:45:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ class Server { * * @param The fd of the client */ - void _handle_client(int, sockaddr_in, config::Config *, struct pollfd); + void _handle_client(int, sockaddr_in, struct pollfd); /** * @brief Can be used to fill the vector passed as parameters with all the diff --git a/src/server/Client.cpp b/src/server/Client.cpp index e520e79..d0a36a3 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _fd = fd; + this->_client_addr = socket; + this->_conf = conf; + this->_log = log; + std::string received_data; char buffer[BUFFER_SIZE]; ssize_t bytes_received; diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 315c7d6..a45a8a7 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/17 18:21:34 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 18:57:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/socket.cpp b/src/server/Socket.cpp similarity index 87% rename from src/socket.cpp rename to src/server/Socket.cpp index 2aac70e..edc1579 100644 --- a/src/socket.cpp +++ b/src/server/Socket.cpp @@ -1,15 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* socket.cpp :+: :+: :+: */ +/* Socket.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/17 12:35:31 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 19:03:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "server/Client.hpp" +#include #include #include @@ -69,3 +71,7 @@ int Server::_createSocket(std::string host, int port) { return (fd); } + +void Server::_handle_client(int fd, sockaddr_in client_addr, struct pollfd poll_fd) { + server::Client *client = new server::Client(fd, client_addr, ); +} From a2cf80bfdecef525284f77276e77de02fc522ec0 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 18 Apr 2025 10:09:46 +0200 Subject: [PATCH 23/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20passed=20=5Flog=20to=20a=20global=20pointer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 18 ++++++++++-------- includes/config/Config.hpp | 3 +-- includes/config/Route.hpp | 6 ++---- includes/config/Server.hpp | 5 ++--- includes/log.hpp | 4 +++- src/config/Config.cpp | 2 +- src/config/Route.cpp | 5 ++--- src/config/Server.cpp | 9 +++------ src/main.cpp | 20 +++++++++++++++++--- src/server/Client.cpp | 3 ++- src/server/Server.cpp | 3 ++- src/server/Socket.cpp | 9 +++++++-- 12 files changed, 52 insertions(+), 35 deletions(-) diff --git a/Dockerfile b/Dockerfile index 509185a..d12e52d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,13 @@ -FROM alpine:3.21 +FROM alpine:3.21 -COPY ./ /build +COPY ./ /build -RUN apk add --no-cache clang make \ - && cd /build \ - && make \ - && chmod +x webserv \ - && cp webserv /bin/webserv +RUN apk add --no-cache clang make \ + && cd /build \ + && make \ + && chmod +x webserv \ + && cp webserv /bin/webserv -RUN [ "/bin/webserv", "$WEBSERV-CONF"] +STOPSIGNAL SIGINT + +RUN [ "/bin/webserv", "$WEBSERV-CONF"] diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index b1bbe47..8b2e413 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:36:42 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:03:09 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,6 @@ class Config { std::vector *getServers(void) { return _servers; } private: - Logger *_log; std::vector *_servers; }; diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index a09e6ed..5bb08cc 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/03/26 08:31:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:05:22 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ namespace config { class Route { public: - Route(toml::ANode *, Logger *); + Route(toml::ANode *); ~Route(void); protected: @@ -42,8 +42,6 @@ class Route { std::string _index; std::map *_cgi; - Logger *_log; - bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, ///1: POST, 2: DELETE toml::ANode *_table; diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 41cf1ee..38db1ea 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/14 12:39:17 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:08:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ namespace config { class Server { public: - Server(toml::ANode *, Logger *); + Server(toml::ANode *); ~Server(); /** @@ -70,7 +70,6 @@ class Server { toml::ANode *_table; ///> The table used for the parsing (is deleted at the /// end of the constructor) - Logger *_log; ///> A pointer to the logger class std::map * _parseErrPages(std::map *table); diff --git a/includes/log.hpp b/includes/log.hpp index c973c46..f3ac4cb 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/14 13:03:31 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:03:06 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -134,4 +134,6 @@ class Logger { std::ofstream _file; }; +Logger *_log = NULL; + }; // namespace webserv diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 35eaec6..f460309 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/14 13:36:46 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 09:57:01 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/config/Route.cpp b/src/config/Route.cpp index db52d41..1946a65 100644 --- a/src/config/Route.cpp +++ b/src/config/Route.cpp @@ -57,12 +57,11 @@ void Route::_parseMethods(std::vector *table) { } } -Route::Route(toml::ANode *table, Logger *logger) - : _max_body(10485760), _log(logger) { +Route::Route(toml::ANode *table) + : _max_body(10485760) { void *val; bool found; - _log = logger; _table = table; if (_table->type() != toml::TABLE) { _log->warn("location need to be a table and not a :" + diff --git a/src/config/Server.cpp b/src/config/Server.cpp index dc33db3..abc08fd 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/17 11:22:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:09:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ using namespace webserv::config; -Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { +Server::Server(toml::ANode *node) : _table(node) { bool found; // host parsing @@ -23,7 +23,6 @@ Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { _host = *static_cast(host); } else { delete _table; - delete _log; throw std::runtime_error( "no host specified - please specify one in server.host"); } @@ -33,7 +32,6 @@ Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { _port = *static_cast(port); } else { delete _table; - delete _log; throw std::runtime_error( "no port specified - please specify one in server.port"); } @@ -73,7 +71,7 @@ Server::Server(toml::ANode *node, Logger *log) : _table(node), _log(log) { for (it = location_table->begin(); it != location_table->end(); it++) { if (_routes->find(it->first) != _routes->end()) continue; - (*_routes)[it->first] = new Route(it->second, _log); + (*_routes)[it->first] = new Route(it->second); } } delete _table; @@ -88,7 +86,6 @@ Server::~Server(void) { delete _err_pages; if (_server_names != not_nullptr) delete _server_names; - delete _log; // to see if nessecary } std::map * diff --git a/src/main.cpp b/src/main.cpp index d6f5158..49cf161 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,12 +6,14 @@ /* By: mmoussou +#include #include +#include #include #include #include @@ -20,6 +22,12 @@ #include #include +int _sig = 0; + +void ft_sig(int sig) { + _sig = sig; +} + int main(int ac, char **av) { if (help(ac, av)) { return EXIT_SUCCESS; @@ -30,13 +38,19 @@ int main(int ac, char **av) { return EXIT_FAILURE; } - config::Server *conf; + + config::Config *conf; try { - conf = new config::Config(std::string(av[1])); + std::string str = av[1]; + conf = new config::Config(str); } catch (std::exception &e) { std::cout << e.what() << std::endl; return 1; } + if (signal(SIGINT, &ft_sig) == SIG_ERR) { + conf->getLogger()->error("could not bind sigint :("); + return EXIT_FAILURE; + } webserv::Server *serv = new webserv::Server(conf); diff --git a/src/server/Client.cpp b/src/server/Client.cpp index d0a36a3..d03e77d 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,11 +6,12 @@ /* By: mmoussou +#include using namespace server; diff --git a/src/server/Server.cpp b/src/server/Server.cpp index a45a8a7..212e7b0 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/17 18:57:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 09:19:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,6 +85,7 @@ void Server::_run(void) { _client_fds.push_back(fd); } + // to add signal instead of 727 while (727) { int ret = poll(_client_fds.data(), nbr_client, -1); if (ret < 0) { diff --git a/src/server/Socket.cpp b/src/server/Socket.cpp index edc1579..32ba835 100644 --- a/src/server/Socket.cpp +++ b/src/server/Socket.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/17 19:03:27 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 09:37:32 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,8 +41,13 @@ int Server::_createSocket(std::string host, int port) { str << port; throw std::runtime_error("socket binding failed for : " + host + ":" + str.str()); + return -1; + } + + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { + close(fd); + throw std::runtime_error("fcntl failed"); } - return -1; int opt = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { From 5824985c939ddc706fc8f4f3d4a40212d9b45cd3 Mon Sep 17 00:00:00 2001 From: y-syo Date: Sun, 20 Apr 2025 11:26:45 +0200 Subject: [PATCH 24/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20client=20bullshit=20things?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpRequest.hpp | 7 +++++- includes/requests/HttpResponse.hpp | 3 ++- includes/server/Client.hpp | 5 ++-- src/requests_handling/HttpRequests.cpp | 14 ++++++++++- src/requests_handling/HttpResponse.cpp | 6 ++++- src/server/Client.cpp | 35 +++++--------------------- src/server/Socket.cpp | 4 +-- 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 8670da5..19e4983 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/17 18:48:25 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 11:25:37 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ namespace server { class Client { public: Client(int, sockaddr_in, config::Server *); - ~Client(void); + virtual ~Client(void); void answer(void); @@ -36,7 +36,6 @@ class Client { http::IRequest *_request; http::Response *_response; config::Server *_conf; - Logger *_log; }; } // -namespace server diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index 1ac8959..89e9fbb 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,7 +6,7 @@ /* By: mmoussou parse(data); @@ -218,6 +222,10 @@ http::Delete::Delete(void) { } +http::Delete::~Delete(void) +{ +} + http::Delete::Delete(std::string &data) { this->parse(data); @@ -297,6 +305,10 @@ http::Post::Post(void) { } +http::Post::~Post(void) +{ +} + http::Post::Post(std::string &data) { this->parse(data); diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index 9fccdb4..e7549ff 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _fd = fd; - this->_client_addr = socket; - this->_conf = conf; - this->_log = log; - std::string received_data; char buffer[BUFFER_SIZE]; ssize_t bytes_received; @@ -57,7 +34,7 @@ Client::Client(int fd, sockaddr_in socket, config::Server *conf, Logger *log) while (buffer[bytes_received]); - this->getRequest(request_str); + this->_getRequest(received_data); } void Client::_getRequest(std::string request_str) @@ -91,8 +68,8 @@ void Client::answer(void) { std::string response; - if (this->_request == "GET" || this->_request == "DELETE" || this->_request == "POST") - response = this->_request.execute().str(); + if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST") + response = this->_request->execute().str(); else response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; send(this->_fd, response.c_str(), response.length(), 0); @@ -101,6 +78,6 @@ void Client::answer(void) Client::~Client(void) { - delete this->_request; + delete (http::Get *)(this->_request); delete this->_response; } diff --git a/src/server/Socket.cpp b/src/server/Socket.cpp index 32ba835..3538eee 100644 --- a/src/server/Socket.cpp +++ b/src/server/Socket.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/18 09:37:32 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 10:58:26 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,5 +78,5 @@ int Server::_createSocket(std::string host, int port) { } void Server::_handle_client(int fd, sockaddr_in client_addr, struct pollfd poll_fd) { - server::Client *client = new server::Client(fd, client_addr, ); + server::Client *client = new server::Client(fd, client_addr, NULL); } From bd19d78ac7bc6653d0d6dbac108b0e97ceef0063 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 20 Apr 2025 13:30:10 +0200 Subject: [PATCH 25/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20m?= =?UTF-8?q?erge=20conflic=20on=20socket.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Server.hpp | 28 ++++++++++++++-- src/config/Config.cpp | 4 +-- src/server/Server.cpp | 66 ++++++++++++++++++++++++++++---------- src/server/Socket.cpp | 5 +-- 4 files changed, 77 insertions(+), 26 deletions(-) diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index d8fa556..ef1b43b 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/17 18:45:07 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 12:37:23 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,11 @@ namespace webserv { +struct client_data { + sockaddr_in sock_data; + pollfd poll_fd; +}; + class Server { public: Server(config::Config *); @@ -43,7 +48,7 @@ class Server { * * @param The fd of the client */ - void _handle_client(int, sockaddr_in, struct pollfd); + bool _handle_client(struct pollfd &, sockaddr_in *); /** * @brief Can be used to fill the vector passed as parameters with all the @@ -60,11 +65,28 @@ class Server { */ int _createSocket(std::string, int); + /** + * @brief Can be used to check if an fd is one of the socket or not + * + * @param the fd to check + */ + bool _isServerSocket(int fd) { + for (std::vector::iterator it = _fds_server.begin(); + it != _fds_server.end(); it++) { + if (fd == *it) { + return true; + } + } + return false; + } + config::Config *_conf; // Pointer to the configuration class (with all config in) Logger *_log; // Pointer to the log class - std::vector _fds_server; // The fds of the sockets + std::vector _fds_server; // The fds of the sockets std::vector _client_fds; // A vector of all the poll fd + std::vector + _client_data; // vector of all the client sockaddr_in }; }; // namespace webserv diff --git a/src/config/Config.cpp b/src/config/Config.cpp index f460309..e0b8fbf 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/18 09:57:01 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:21:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ Config::Config(std::string &filename) { std::map *node = table->getTable(); for (std::map::iterator it = node->begin(); it != node->end(); it++) { - Server *srv = new Server(it->second, _log); + Server *srv = new Server(it->second); _servers->push_back(srv); } delete table; diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 212e7b0..6e14275 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,18 +6,21 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/18 09:19:14 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 13:00:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #include #include #include #include #include +#include #include +#include #include #include @@ -76,37 +79,66 @@ void Server::_setup(void) { void Server::_run(void) { struct pollfd fd; - int nbr_client = 0; for (std::vector::iterator it = _fds_server.begin(); - it != _fds_server.end(); it++, nbr_client++) { + it != _fds_server.end(); it++) { fd.fd = *it; fd.events = POLLIN; _client_fds.push_back(fd); + _client_data.push_back(NULL); } // to add signal instead of 727 while (727) { - int ret = poll(_client_fds.data(), nbr_client, -1); + int ret = poll(_client_fds.data(), _client_fds.size(), -1); if (ret < 0) { - _log->error("poll failed :("); + std::stringstream str; + str << "poll failed : "; + str << strerror(errno); + _log->error(str.str()); continue; } - for (int i = 0; i < nbr_client; i++) { + for (size_t i = 0; i < _client_fds.size(); ++i) { if (_client_fds[i].revents & POLLIN) { - struct sockaddr_in client_addr; - socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_client_fds[i].fd, (struct sockaddr *)&client_addr, - &addrlen); - if (client_fd < 0) { - _log->error("accept failed with : " + - convertIPToString(&client_addr.sin_addr) + ":" + - convertPortToString(&client_addr)); - continue ; + if (_isServerSocket(_client_fds[i].fd)) { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + int client_fd = + accept(_client_fds[i].fd, + (struct sockaddr *)&client_addr, &addrlen); + + if (client_fd < 0) { + std::stringstream str; + str << "Accept failed: "; + str << strerror(errno); + _log->error(str.str()); + continue; + } + + if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) { + std::stringstream str; + str << "Failed to set non-blocking mode: "; + str << strerror(errno); + _log->error(str.str()); + close(client_fd); + continue; + } + + pollfd pfd; + pfd.fd = client_fd; + pfd.events = POLLIN; + _client_fds.push_back(pfd); + _client_data.push_back(new sockaddr_in(client_addr)); + } else { + if (!_handle_client(_client_fds[i], _client_data[i])) { + close(_client_fds[i].fd); + _client_fds.erase(_client_fds.begin() + i); + delete _client_data[i]; + _client_data.erase(_client_data.begin() + i); + i--; + } } - _handle_client(client_fd, client_addr, _conf, _client_fds[i]); } } } diff --git a/src/server/Socket.cpp b/src/server/Socket.cpp index 3538eee..ff58107 100644 --- a/src/server/Socket.cpp +++ b/src/server/Socket.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/20 10:58:26 by mmoussou ### ########.fr */ +/* Updated: 2025/04/20 13:29:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,3 @@ int Server::_createSocket(std::string host, int port) { return (fd); } -void Server::_handle_client(int fd, sockaddr_in client_addr, struct pollfd poll_fd) { - server::Client *client = new server::Client(fd, client_addr, NULL); -} From 25eef85b27537e3d66d8965e7622b2dcf7398009 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 20 Apr 2025 18:30:19 +0200 Subject: [PATCH 26/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20need=20to=20do=20=5Fhandle=5Fclient=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/Server.cpp | 6 +++--- src/server/Socket.cpp | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 6e14275..97b056c 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/20 13:00:33 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 18:29:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include #include #include @@ -90,8 +91,7 @@ void Server::_run(void) { // to add signal instead of 727 while (727) { - int ret = poll(_client_fds.data(), _client_fds.size(), -1); - if (ret < 0) { + if (poll(_client_fds.data(), _client_fds.size(), -1) < 0) { std::stringstream str; str << "poll failed : "; str << strerror(errno); diff --git a/src/server/Socket.cpp b/src/server/Socket.cpp index ff58107..a0bfd93 100644 --- a/src/server/Socket.cpp +++ b/src/server/Socket.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/20 13:29:53 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 17:31:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,4 +76,3 @@ int Server::_createSocket(std::string host, int port) { return (fd); } - From 773a54d7cdd43bb7c769b4c4dc4f28604e0efa94 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 21 Apr 2025 11:08:11 +0200 Subject: [PATCH 27/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20done=20=5Fhandle=5Fclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Server.hpp | 2 +- src/server/Server.cpp | 77 +++++++++++----------- src/server/{Socket.cpp => ServerUtils.cpp} | 20 +++++- 3 files changed, 58 insertions(+), 41 deletions(-) rename src/server/{Socket.cpp => ServerUtils.cpp} (86%) diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index ef1b43b..bea5532 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/20 12:37:23 by adjoly ### ########.fr */ +/* Updated: 2025/04/21 10:46:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 97b056c..5e02319 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/20 18:29:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 18:41:04 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -99,45 +100,45 @@ void Server::_run(void) { continue; } + for (std::vector::iterator it = _client_fds.begin(); + it != _client_fds.end(); it++) { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + int client_fd = + accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen); + + if (client_fd < 0) { + std::stringstream str; + str << "Accept failed: "; + str << strerror(errno); + _log->error(str.str()); + continue; + } + + if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) { + std::stringstream str; + str << "Failed to set non-blocking mode: "; + str << strerror(errno); + _log->error(str.str()); + close(client_fd); + continue; + } + + pollfd pfd; + pfd.fd = client_fd; + pfd.events = POLLIN | POLLOUT; + _client_fds.push_back(pfd); + _client_data.push_back(new sockaddr_in(client_addr)); + } + for (size_t i = 0; i < _client_fds.size(); ++i) { if (_client_fds[i].revents & POLLIN) { - if (_isServerSocket(_client_fds[i].fd)) { - struct sockaddr_in client_addr; - socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_client_fds[i].fd, - (struct sockaddr *)&client_addr, &addrlen); - - if (client_fd < 0) { - std::stringstream str; - str << "Accept failed: "; - str << strerror(errno); - _log->error(str.str()); - continue; - } - - if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) { - std::stringstream str; - str << "Failed to set non-blocking mode: "; - str << strerror(errno); - _log->error(str.str()); - close(client_fd); - continue; - } - - pollfd pfd; - pfd.fd = client_fd; - pfd.events = POLLIN; - _client_fds.push_back(pfd); - _client_data.push_back(new sockaddr_in(client_addr)); - } else { - if (!_handle_client(_client_fds[i], _client_data[i])) { - close(_client_fds[i].fd); - _client_fds.erase(_client_fds.begin() + i); - delete _client_data[i]; - _client_data.erase(_client_data.begin() + i); - i--; - } + if (!_handle_client(_client_fds[i], _client_data[i])) { + close(_client_fds[i].fd); + _client_fds.erase(_client_fds.begin() + i); + delete _client_data[i]; + _client_data.erase(_client_data.begin() + i); + i--; } } } diff --git a/src/server/Socket.cpp b/src/server/ServerUtils.cpp similarity index 86% rename from src/server/Socket.cpp rename to src/server/ServerUtils.cpp index a0bfd93..b1879fb 100644 --- a/src/server/Socket.cpp +++ b/src/server/ServerUtils.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/20 17:31:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/21 10:53:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #include #include -using namespace webserv; +using namespace webserv::server; bool convertStringToIP(const char *ip_str, struct in_addr *addr) { // Split the IP string into four octets @@ -76,3 +76,19 @@ int Server::_createSocket(std::string host, int port) { return (fd); } + +bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) { + Client *client; + + try { + client = new Client(pollfd.fd, sock_data, _conf); + client->answer(); + } catch (std::exception &e) { + _log->error(e.what()); + return false; + } + + delete client; + + return true; +} From 8ff043802cf562667d4a82a74d5f2c2e54bc8e77 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 11:17:05 +0200 Subject: [PATCH 28/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20add?= =?UTF-8?q?ed=20getserver=20in=20config=20to=20get=20a=20server=20from=20a?= =?UTF-8?q?=20server=5Fname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 7 ++++--- includes/config/Server.hpp | 13 ++++++++++++- src/config/Config.cpp | 22 +++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index 8b2e413..52280d6 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:03:09 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:57:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,11 @@ class Config { Logger *getLogger(void) { return _log; } - std::vector *getServers(void) { return _servers; } + std::vector getServers(void) { return _servers; } + Server *getServer(const std::string &); private: - std::vector *_servers; + std::vector _servers; }; }; // namespace config diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 38db1ea..8bdc37d 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:08:33 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:59:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "config/default.hpp" #include "cppeleven.hpp" #include "node/ANode.hpp" +#include "webserv.hpp" namespace webserv { namespace config { @@ -55,6 +56,16 @@ class Server { // @brief Can be used to get the port specified in the config file int getPort(void) { return _port; } + // @brief Can be used to check if a servername is present in this config + bool isServerName(const std::string &server_name) { + for (auto it = prange(_server_names)) { + if (*it == server_name) { + return true; + } + } + return false; + } + protected: private: std::map diff --git a/src/config/Config.cpp b/src/config/Config.cpp index e0b8fbf..7ab3369 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,12 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:21:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:11:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "cppeleven.hpp" #include "node/ANode.hpp" #include +#include using namespace webserv::config; @@ -30,19 +32,25 @@ Config::Config(std::string &filename) { } std::map *node = table->getTable(); - for (std::map::iterator it = node->begin(); - it != node->end(); it++) { + for (auto it = prange(node)) { Server *srv = new Server(it->second); - _servers->push_back(srv); + _servers.push_back(srv); } delete table; delete file; } Config::~Config(void) { - std::vector::iterator it = _servers->begin(); - for (; it != _servers->end() ; it++) { + for (auto it = range(_servers)) { delete *it; } - delete _servers; +} + +Server *Config::getServer(const std::string &server_name) { + for (auto it = range(_servers)) { + if ((*it)->isServerName(server_name)) { + return (*it); + } + } + return (not_nullptr); } From 6db9d66374ef34b32c1d497ae559ff2e73a9886d Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 22 Apr 2025 11:19:28 +0200 Subject: [PATCH 29/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(http?= =?UTF-8?q?Response):=20fix=20to=20compile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpResponse.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index f407843..698b1ac 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,7 +6,7 @@ /* By: mmoussou Date: Tue, 22 Apr 2025 11:41:05 +0200 Subject: [PATCH 30/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20added=20some=20macro=20for=20easier=20coding=20with?= =?UTF-8?q?=20iterator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/webserv.hpp | 7 ++++++- src/server/Server.cpp | 30 +++++++++--------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 7fb6d3a..557b577 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include @@ -25,6 +26,10 @@ #include #include +#define auto __auto_type +#define range(x) x.begin(); it != x.end(); it++ +#define prange(x) x->begin(); it != x->end(); it++ + #define BUFFER_SIZE 4096 namespace webserv { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 5e02319..1ca2443 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/20 18:41:04 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:51:15 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,14 +49,13 @@ std::string getMethod(std::string &data) { int Server::_fillHostsPorts(std::vector &hosts, std::vector &ports) { - std::vector *config = _conf->getServers(); + std::vector config = _conf->getServers(); - for (std::vector::iterator it = config->begin(); - it != config->end(); it++) { + for (auto it = range(config)) { hosts.push_back((*it)->getHost()); ports.push_back((*it)->getPort()); } - return config->size(); + return config.size(); } void Server::_setup(void) { @@ -67,11 +66,10 @@ void Server::_setup(void) { if (size < 1) throw std::runtime_error("no server present in the config file"); - std::vector::iterator itH = hosts.begin(); - for (std::vector::iterator itP = ports.begin(); itP != ports.end(); - itP++, itH++) { + auto itH = hosts.begin(); + for (auto it = range(ports), itH++) { try { - int fd = _createSocket(*itH, *itP); + int fd = _createSocket(*itH, *it); _fds_server.push_back(fd); } catch (std::exception &e) { throw e; @@ -100,12 +98,11 @@ void Server::_run(void) { continue; } - for (std::vector::iterator it = _client_fds.begin(); - it != _client_fds.end(); it++) { + for (auto it = range(_fds_server)) { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); int client_fd = - accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen); + accept((*it), (struct sockaddr *)&client_addr, &addrlen); if (client_fd < 0) { std::stringstream str; @@ -115,15 +112,6 @@ void Server::_run(void) { continue; } - if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) { - std::stringstream str; - str << "Failed to set non-blocking mode: "; - str << strerror(errno); - _log->error(str.str()); - close(client_fd); - continue; - } - pollfd pfd; pfd.fd = client_fd; pfd.events = POLLIN | POLLOUT; From b9b299cd68b128d76dc90d8441a28c4f11836e60 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 22 Apr 2025 12:06:48 +0200 Subject: [PATCH 31/45] =?UTF-8?q?=E3=80=8C=E2=9C=8F=EF=B8=8F=E3=80=8D=20no?= =?UTF-8?q?rm(includes):=20cleaned=20headers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/cgi.hpp | 3 ++- includes/config/Config.hpp | 4 ++-- includes/config/Route.hpp | 8 ++++---- includes/config/Server.hpp | 10 +++++----- includes/config/default.hpp | 14 +++++++------- includes/log.hpp | 2 +- includes/requests/Errors.hpp | 6 +----- includes/requests/HttpIMessage.hpp | 6 +----- includes/requests/HttpRequest.hpp | 11 ++++++----- includes/requests/HttpResponse.hpp | 6 +----- includes/requests/default.hpp | 6 +----- includes/server/Client.hpp | 10 +++++----- includes/server/Server.hpp | 4 ++-- includes/server/default.hpp | 6 +++--- includes/webserv.hpp | 2 +- src/config/Route.cpp | 2 +- src/help.cpp | 4 ++-- src/server/Client.cpp | 11 +++++++++-- src/server/ServerUtils.cpp | 8 ++++---- 19 files changed, 58 insertions(+), 65 deletions(-) diff --git a/includes/cgi.hpp b/includes/cgi.hpp index 90f9cce..558c8d9 100644 --- a/includes/cgi.hpp +++ b/includes/cgi.hpp @@ -6,13 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */ -/* Updated: 2025/03/24 14:20:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:51:33 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include + class cgi { public: cgi(); diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index 52280d6..51eb335 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,13 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/22 10:57:26 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:52:33 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "config/default.hpp" +#include namespace webserv { namespace config { diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 5bb08cc..63e2cfd 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:05:22 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:49:19 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "cppeleven.hpp" -#include "log.hpp" -#include "node/default.hpp" +#include +#include +#include #include #include #include diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 8bdc37d..7280e50 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,16 +6,16 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/22 10:59:27 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 12:02:36 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "config/default.hpp" -#include "cppeleven.hpp" -#include "node/ANode.hpp" -#include "webserv.hpp" +#include +#include +#include +#include namespace webserv { namespace config { diff --git a/includes/config/default.hpp b/includes/config/default.hpp index 7713797..14c8d43 100644 --- a/includes/config/default.hpp +++ b/includes/config/default.hpp @@ -6,18 +6,18 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ -/* Updated: 2025/04/14 12:54:29 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 12:03:30 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "Route.hpp" -#include "Server.hpp" -#include "Config.hpp" -#include "cppeleven.hpp" -#include "node/Table.hpp" -#include "node/default.hpp" +#include +#include +#include +#include +#include +#include #include namespace webserv { diff --git a/includes/log.hpp b/includes/log.hpp index f3ac4cb..d9aaa5a 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/18 10:03:06 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:50:42 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/requests/Errors.hpp b/includes/requests/Errors.hpp index a31b50b..e2b2e46 100644 --- a/includes/requests/Errors.hpp +++ b/includes/requests/Errors.hpp @@ -6,13 +6,11 @@ /* By: mmoussou #include @@ -37,5 +35,3 @@ private: } // -namespace http } // -namespace webserv - -#endif // __WEBSERV_REQUESTS_ERRORS_HPP__ diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index 9ed38b2..a357f8d 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,13 +6,11 @@ /* By: mmoussou #include @@ -43,5 +41,3 @@ protected: } // -namespace http } // -namespace webserv - -#endif // __WEBSERV_REQUESTS_HTTP_IMESSAGE_HPP__ diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 19e4983..4a811c0 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,13 +6,11 @@ /* By: mmoussou #include @@ -22,6 +20,8 @@ #include #include +#include + namespace webserv { namespace http { @@ -37,15 +37,18 @@ public: std::string getMethod(void) const; std::string getTarget(void) const; std::string getProtocol(void) const; + config::Server *getConfig(void) const; void setMethod(std::string const method); void setTarget(std::string const target); void setProtocol(std::string const protocol); + void setServer(std::string const protocol); protected: std::string _method; std::string _target; std::string _protocol; + config::Server *_conf; }; @@ -87,5 +90,3 @@ public: } // -namespace http } // -namespace webserv - -#endif // __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__ diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index 698b1ac..cf44d32 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,13 +6,11 @@ /* By: mmoussou @@ -46,5 +44,3 @@ private: } // -namespace http } // -namespace webserv - -#endif // __WEBSERV_REQUESTS_HTTP_RESPONSE_HPP__ diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 82636b6..6359f52 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,18 +6,14 @@ /* By: mmoussou #include #include using namespace webserv; - -#endif // __WEBSERV_REQUESTS_DEFAULT_HPP__ diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index d0998e6..035a1fe 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/20 11:25:37 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 12:04:57 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "config/default.hpp" -#include "default.hpp" -#include "requests/default.hpp" +#include +#include +#include #include #include @@ -23,7 +23,7 @@ namespace server { class Client { public: - Client(int, sockaddr_in, config::Server *); + Client(int, sockaddr_in, config::Config *); virtual ~Client(void); void answer(void); diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index bea5532..2d13f61 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,13 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/21 10:46:31 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:51:27 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "log.hpp" +#include #include #include #include diff --git a/includes/server/default.hpp b/includes/server/default.hpp index 739752a..76c1351 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,14 +6,14 @@ /* By: mmoussou +#include namespace webserv { diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 557b577..0f9654b 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include "node/default.hpp" #include #include diff --git a/src/help.cpp b/src/help.cpp index 56b42d1..2982778 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */ -/* Updated: 2025/04/11 11:39:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:47:39 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include "log.hpp" +#include #include #include #include diff --git a/src/server/Client.cpp b/src/server/Client.cpp index a79bde6..f974dc7 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _getRequest(received_data); + + this->_conf = conf->getServer(this->_request->getHeaders()["Host"]); + + + //if (received_data.length > (get max_body_size from Route corresponding) ) + // throw error } void Client::_getRequest(std::string request_str) @@ -67,6 +73,7 @@ void Client::_getRequest(std::string request_str) void Client::answer(void) { std::string response; + (void) _client_addr; if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST") response = this->_request->execute().str(); diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index b1879fb..e34ac98 100644 --- a/src/server/ServerUtils.cpp +++ b/src/server/ServerUtils.cpp @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* Socket.cpp :+: :+: :+: */ +/* ServerUtils.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/21 10:53:45 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 11:46:07 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include "server/Client.hpp" +#include #include #include #include @@ -81,7 +81,7 @@ bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) { Client *client; try { - client = new Client(pollfd.fd, sock_data, _conf); + client = new Client(pollfd.fd, *sock_data, _conf); client->answer(); } catch (std::exception &e) { _log->error(e.what()); From 7ee79b6fa4a9c2a3ef3fe2f415d51aa83af83ef8 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 12:34:42 +0200 Subject: [PATCH 32/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20merge=20conflict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Route.hpp | 15 +++++++- includes/config/Server.hpp | 10 +++++- includes/config/URL.hpp | 73 ++++++++++++++++++++++++++++++++++++++ includes/webserv.hpp | 31 ++++++++++------ 4 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 includes/config/URL.hpp diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 63e2cfd..7cded90 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/04/22 11:49:19 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 12:34:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,19 @@ class Route { Route(toml::ANode *); ~Route(void); + bool getDirList(void) { return _dirlist; } + bool getCookies(void) { return _cookies; } + bool getRedirect(void) { return _redirect; } + + int32_t getMaxBody(void) { return _max_body; } + + std::string getRootDir(void) { return _root; } + std::string getUpRoot(void) { return _up_root; } + std::string getIndex(void) { return _index; } + std::map *getCgi(void) { return _cgi; } + + bool *getMethods(void) { return _methods; } + protected: private: bool _dirlist; diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 7280e50..1fd2570 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:02:36 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 12:34:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,12 @@ class Server { return false; } + Route *whatRoute(const std::string &route_path) { + for (auto it = prange(_routes)) { + + } + } + protected: private: std::map @@ -84,6 +90,8 @@ class Server { std::map * _parseErrPages(std::map *table); + + }; } // namespace config diff --git a/includes/config/URL.hpp b/includes/config/URL.hpp new file mode 100644 index 0000000..d674c2c --- /dev/null +++ b/includes/config/URL.hpp @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* URL.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */ +/* Updated: 2025/04/22 12:26:21 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include + +class URL { + public: + URL(const std::string &url) : _full_url(url) { parse(); } + + bool operator==(const URL &other) const { + return comparePathSegments(other); + } + + std::vector getSegments(void) { return _path_segments; } + std::string getFullUrl(void) { return _full_url; } + + private: + void parse() { + size_t scheme_pos = _full_url.find("://"); + size_t path_start = 0; + + if (scheme_pos != std::string::npos) { + path_start = _full_url.find('/', scheme_pos + 3); + } else { + path_start = 0; + } + + if (path_start != std::string::npos) { + std::string path = _full_url.substr(path_start); + splitPath(path, _path_segments); + } + } + + void splitPath(const std::string &path, + std::vector &segments) const { + std::stringstream ss(path); + std::string segment; + while (std::getline(ss, segment, '/')) { + if (!segment.empty()) { + segments.push_back(segment); + } + } + } + + bool comparePathSegments(const URL &other) const { + size_t min_size = + std::min(_path_segments.size(), other._path_segments.size()); + for (size_t i = 0; i < min_size; ++i) { + if (_path_segments[i] != other._path_segments[i]) { + return false; + } + } + return true; + } + + std::string _full_url; + std::vector _path_segments; +}; diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 0f9654b..94f0507 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,34 +6,45 @@ /* By: mmoussou >>>>>> 8129f45 (「🏗️」 wip: added url class for easier manipulation) /* */ /* ************************************************************************** */ #pragma once #include #ifndef __WEBSERV_WEBSERV_HPP__ -# define __WEBSERV_WEBSERV_HPP__ +#define __WEBSERV_WEBSERV_HPP__ - -#include -#include -#include #include #include +#include +#include #include -#include -#include #include +#include +#include +#include +#include +#include #define auto __auto_type -#define range(x) x.begin(); it != x.end(); it++ -#define prange(x) x->begin(); it != x->end(); it++ +#define range(x) \ + x.begin(); \ + it != x.end(); \ + it++ +#define prange(x) \ + x->begin(); \ + it != x->end(); \ + it++ #define BUFFER_SIZE 4096 namespace webserv { -} //-namespace webserv +} // namespace webserv #endif // __WEBSERV_WEBSERV_HPP__ From 43a34e759fd7dcdecf8e755a7b1ef7e3221eb1d3 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 12:44:50 +0200 Subject: [PATCH 33/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20implemented=20url=20class=20in=20config=20need=20to?= =?UTF-8?q?=20fix=20compilation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Server.hpp | 13 +++++++++---- includes/config/URL.hpp | 4 +++- includes/config/default.hpp | 2 +- includes/webserv.hpp | 11 ++--------- src/config/Server.cpp | 11 +++++------ 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 1fd2570..a6cca82 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,16 +6,18 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:34:14 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 12:44:16 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include #include #include #include #include +#include namespace webserv { namespace config { @@ -66,15 +68,18 @@ class Server { return false; } - Route *whatRoute(const std::string &route_path) { + Route *whatRoute(const URL &url) { for (auto it = prange(_routes)) { - + if (it->first == url) { + return it->second; + } } + return not_nullptr; } protected: private: - std::map + std::map *_routes; ///> A map of all the route present in the config file std::map *_err_pages; ///> An error pages map to map error /// specified in the config file diff --git a/includes/config/URL.hpp b/includes/config/URL.hpp index d674c2c..f5a7a50 100644 --- a/includes/config/URL.hpp +++ b/includes/config/URL.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:26:21 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 12:42:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ class URL { return comparePathSegments(other); } + bool operator<(const URL &other) const { return _full_url < other._full_url; } + std::vector getSegments(void) { return _path_segments; } std::string getFullUrl(void) { return _full_url; } diff --git a/includes/config/default.hpp b/includes/config/default.hpp index 14c8d43..b976777 100644 --- a/includes/config/default.hpp +++ b/includes/config/default.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:03:30 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 12:40:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 94f0507..f7f3865 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,19 +6,13 @@ /* By: mmoussou >>>>>> 8129f45 (「🏗️」 wip: added url class for easier manipulation) +/* Updated: 2025/04/22 12:43:26 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include -#ifndef __WEBSERV_WEBSERV_HPP__ -#define __WEBSERV_WEBSERV_HPP__ +#include #include #include #include @@ -47,4 +41,3 @@ namespace webserv { } // namespace webserv -#endif // __WEBSERV_WEBSERV_HPP__ diff --git a/src/config/Server.cpp b/src/config/Server.cpp index abc08fd..5dcc2e9 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/18 10:09:25 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 12:38:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,21 +65,20 @@ Server::Server(toml::ANode *node) : _table(node) { // location parsing it = _table->accessIt("location", toml::TABLE, found); if (found == true && it != _table->getTable()->end()) { - _routes = new std::map; + _routes = new std::map; std::map *location_table = it->second->getTable(); for (it = location_table->begin(); it != location_table->end(); it++) { - if (_routes->find(it->first) != _routes->end()) + if (_routes->find(URL(it->first)) != _routes->end()) continue; - (*_routes)[it->first] = new Route(it->second); + (*_routes)[URL(it->first)] = new Route(it->second); } } delete _table; } Server::~Server(void) { - std::map::iterator it = _routes->begin(); - for (; it != _routes->end(); it++) { + for (auto it = prange(_routes)) { delete it->second; } delete _routes; From cc953a791c1d32b77418935b7b4f364d8308fc21 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 14:32:35 +0200 Subject: [PATCH 34/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20compilation=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- includes/config/Server.hpp | 20 +++----------------- includes/server/Client.hpp | 14 +++++++------- includes/webserv.hpp | 6 ++++-- src/config/Config.cpp | 4 ++-- src/config/Server.cpp | 21 ++++++++++++++++++++- 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index f9fb2b2..b447634 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/04/15 17:53:00 by adjoly ### ########.fr # +# Updated: 2025/04/22 14:31:00 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ SHELL = bash NAME = webserv -CC = c++ +CC = clang++ OBJSDIR = obj/ diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index a6cca82..fb3f72b 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:44:16 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 14:25:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,23 +59,9 @@ class Server { int getPort(void) { return _port; } // @brief Can be used to check if a servername is present in this config - bool isServerName(const std::string &server_name) { - for (auto it = prange(_server_names)) { - if (*it == server_name) { - return true; - } - } - return false; - } + bool isServerName(const std::string &); - Route *whatRoute(const URL &url) { - for (auto it = prange(_routes)) { - if (it->first == url) { - return it->second; - } - } - return not_nullptr; - } + Route *whatRoute(const URL &); protected: private: diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index 035a1fe..2d660ff 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,16 +6,16 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:04:57 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 14:23:44 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include -#include -#include #include +#include +#include #include namespace webserv { @@ -26,10 +26,10 @@ class Client { Client(int, sockaddr_in, config::Config *); virtual ~Client(void); - void answer(void); + void answer(void); private: - void _getRequest(std::string); + void _getRequest(std::string); int _fd; struct sockaddr_in _client_addr; @@ -38,5 +38,5 @@ class Client { config::Server *_conf; }; -} // -namespace server -} // -namespace webserv +} // namespace server +} // namespace webserv diff --git a/includes/webserv.hpp b/includes/webserv.hpp index f7f3865..1462958 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,12 +6,14 @@ /* By: mmoussou #include #include @@ -25,7 +27,7 @@ #include #include -#define auto __auto_type + #define range(x) \ x.begin(); \ it != x.end(); \ diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 7ab3369..e461c8b 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,14 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/22 11:11:26 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 14:31:34 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "webserv.hpp" #include "cppeleven.hpp" #include "node/ANode.hpp" #include -#include using namespace webserv::config; diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 5dcc2e9..9290d80 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:38:59 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 14:26:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include using namespace webserv::config; @@ -106,3 +107,21 @@ Server::_parseErrPages(std::map *table) { } return errPages; } + +bool Server::isServerName(const std::string &server_name) { + for (auto it = prange(_server_names)) { + if (*it == server_name) { + return true; + } + } + return false; +} + +Route *Server::whatRoute(const URL &url) { + for (auto it = prange(_routes)) { + if (it->first == url) { + return it->second; + } + } + return not_nullptr; +} From 7abe04bf449de4b69037994dccc76d7bb3be9015 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 14:51:00 +0200 Subject: [PATCH 35/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20compilation=20error=20with=20logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- includes/log.hpp | 4 ++-- src/config/Route.cpp | 1 + src/main.cpp | 7 ++++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index b447634..971b463 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:31:00 by adjoly ### ########.fr # +# Updated: 2025/04/22 14:32:50 by adjoly ### ########.fr # # # # **************************************************************************** # diff --git a/includes/log.hpp b/includes/log.hpp index d9aaa5a..96fee4a 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 11:50:42 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 14:46:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -134,6 +134,6 @@ class Logger { std::ofstream _file; }; -Logger *_log = NULL; +extern Logger *_log; }; // namespace webserv diff --git a/src/config/Route.cpp b/src/config/Route.cpp index ecd16d0..43e6d5b 100644 --- a/src/config/Route.cpp +++ b/src/config/Route.cpp @@ -15,6 +15,7 @@ #include #include + using namespace webserv::config; std::map *Route::_parseCGI(toml::ANode *table) { diff --git a/src/main.cpp b/src/main.cpp index 49cf161..4476e72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include +#include #include +namespace webserv { + Logger *_log = not_nullptr; +} + int _sig = 0; void ft_sig(int sig) { From 5d1c3910bc3b16d24e11e84c11d67a14b9eab131 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 15:34:13 +0200 Subject: [PATCH 36/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20first=20segfault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Config.hpp | 4 ++-- includes/config/Server.hpp | 4 ++-- includes/config/default.hpp | 8 ++++---- includes/requests/HttpIMessage.hpp | 2 +- includes/requests/HttpRequest.hpp | 2 +- src/config/Config.cpp | 9 ++++++--- src/config/Server.cpp | 8 ++++++-- src/main.cpp | 2 +- src/requests_handling/HttpRequests.cpp | 6 +++++- 9 files changed, 28 insertions(+), 17 deletions(-) diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index 51eb335..e570ce7 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,13 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/22 11:52:33 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 15:25:39 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include +#include namespace webserv { namespace config { diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index fb3f72b..5dfb258 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,14 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/04/22 14:25:17 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:25:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include -#include +#include #include #include #include diff --git a/includes/config/default.hpp b/includes/config/default.hpp index b976777..3e77b19 100644 --- a/includes/config/default.hpp +++ b/includes/config/default.hpp @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ -/* Updated: 2025/04/22 12:40:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:28:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include #include #include -#include #include #include #include @@ -35,13 +35,13 @@ namespace config { * @return The value got or not_nullptr */ static inline void *accessValue(const std::string &name, toml::nodeType type, - toml::ANode *table, Logger *log) { + toml::ANode *table, Logger *log) { void *val; bool found = false; if (table == not_nullptr) return not_nullptr; - val = dynamic_cast(table)->access(name, type, found); + val = dynamic_cast(table)->access(name, type, found); if (found == true && val != not_nullptr) { return val; } else { diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index a357f8d..eb63d3e 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/22 14:31:34 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:32:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "node/default.hpp" #include "webserv.hpp" #include "cppeleven.hpp" #include "node/ANode.hpp" @@ -33,8 +34,10 @@ Config::Config(std::string &filename) { std::map *node = table->getTable(); for (auto it = prange(node)) { - Server *srv = new Server(it->second); - _servers.push_back(srv); + if (it->second->type() == toml::TABLE) { + Server *srv = new Server(it->second); + _servers.push_back(srv); + } } delete table; delete file; diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 9290d80..85ba913 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,18 +6,22 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/22 14:26:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:25:06 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include "cppeleven.hpp" #include +#include using namespace webserv::config; Server::Server(toml::ANode *node) : _table(node) { bool found; + if (_table == not_nullptr) + return; + // host parsing void *host = accessValue("host", toml::STRING, _table, _log); if (host != not_nullptr) { diff --git a/src/main.cpp b/src/main.cpp index 4476e72..bc65aab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou Date: Tue, 22 Apr 2025 15:41:35 +0200 Subject: [PATCH 37/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20add?= =?UTF-8?q?ed=20ctrl=20c=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/Config.cpp | 3 ++- src/config/Server.cpp | 4 ++-- src/main.cpp | 7 ++++++- src/server/Server.cpp | 13 +++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 20bc8e3..02513d1 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/22 15:32:19 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:35:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ Config::Config(std::string &filename) { std::map *node = table->getTable(); for (auto it = prange(node)) { if (it->second->type() == toml::TABLE) { + _log->info("taking server from table : " + it->first); Server *srv = new Server(it->second); _servers.push_back(srv); } diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 85ba913..1e49c05 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/22 15:25:06 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:36:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,7 +79,7 @@ Server::Server(toml::ANode *node) : _table(node) { (*_routes)[URL(it->first)] = new Route(it->second); } } - delete _table; + //delete _table; } Server::~Server(void) { diff --git a/src/main.cpp b/src/main.cpp index bc65aab..f74c3ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include +#include #include #include #include @@ -31,6 +32,10 @@ int _sig = 0; void ft_sig(int sig) { _sig = sig; + std::stringstream str; + str << "sig hitted = "; + str << sig; + _log->info(str.str()); } int main(int ac, char **av) { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 1ca2443..2484109 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/22 10:51:15 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 15:40:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ using namespace webserv; +extern int _sig; + std::string convertIPToString(const struct in_addr *addr) { unsigned int ip = ntohl(addr->s_addr); std::stringstream ss; @@ -77,6 +79,13 @@ void Server::_setup(void) { } } +short sigHandling(void) { + if (_sig == SIGINT) { + return 727; + } + return 0; +} + void Server::_run(void) { struct pollfd fd; @@ -89,7 +98,7 @@ void Server::_run(void) { } // to add signal instead of 727 - while (727) { + while (727 - sigHandling()) { if (poll(_client_fds.data(), _client_fds.size(), -1) < 0) { std::stringstream str; str << "poll failed : "; From b660b023516517a5b230d31a4ab668f63e3a2739 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 15:43:37 +0200 Subject: [PATCH 38/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20c?= =?UTF-8?q?orrected=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index f74c3ec..37142c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou Date: Tue, 22 Apr 2025 16:23:28 +0200 Subject: [PATCH 39/45] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20add?= =?UTF-8?q?ed=20debug=20function=20in=20log=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/log.hpp | 15 ++++++++++++++- src/server/ServerUtils.cpp | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/log.hpp b/includes/log.hpp index 96fee4a..16209e6 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 14:46:05 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 16:14:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,6 +109,19 @@ class Logger { } } + void debug(const std::string &msg) { +#ifdef VERBOSE + std::string ss = printPogitMsg("🏗️", "webserv", "debug", msg); + std::cerr << ss << std::endl; + if (!_ttyOnly) { + _file << ss << std::endl; + } +#else + (void)msg; +#endif + + } + protected: private: std::string printPogitMsg(const std::string &emoji, const std::string &type, diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index e34ac98..e8c3a83 100644 --- a/src/server/ServerUtils.cpp +++ b/src/server/ServerUtils.cpp @@ -6,13 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/22 11:46:07 by mmoussou ### ########.fr */ +/* Updated: 2025/04/22 16:14:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include +#include #include using namespace webserv::server; @@ -58,6 +59,9 @@ int Server::_createSocket(std::string host, int port) { struct sockaddr_in addr; addr.sin_family = AF_INET; convertStringToIP(host.c_str(), &addr.sin_addr); + std::stringstream str; + str << port; + _log->debug("port : " + str.str()); addr.sin_port = htons(port); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { From b4eb64ca3f79bfc8fa10e873dd20073dfe4d44ee Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 16:35:52 +0200 Subject: [PATCH 40/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20c?= =?UTF-8?q?orrected=20issue=20with=20ip=20in=20the=20main=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 2 +- src/server/Server.cpp | 11 ++++++++--- src/server/ServerUtils.cpp | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index a513196..8c0d802 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -2,7 +2,7 @@ log_file = "test.log" [server] server_names = { "localhost", "2B5.local" } -host = "localhost" +host = "0.0.0.0" port = 8080 [server.error_pages] diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 2484109..976b9b6 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,12 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/22 15:40:59 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 16:34:40 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include #include #include @@ -145,8 +146,12 @@ void Server::_run(void) { Server::Server(config::Config *conf) : _conf(conf) { log("➕", "Server::Server", "config constructor called"); _log = conf->getLogger(); - _setup(); - _run(); + try { + _setup(); + _run(); + } catch (std::exception &e) { + _log->error(e.what()); + } } Server::~Server(void) { diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index e8c3a83..9fc4dd4 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/22 16:14:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 16:32:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,9 @@ int Server::_createSocket(std::string host, int port) { struct sockaddr_in addr; addr.sin_family = AF_INET; - convertStringToIP(host.c_str(), &addr.sin_addr); + if (!convertStringToIP(host.c_str(), &addr.sin_addr)) { + throw std::runtime_error("ip is not of the valid format : " + host); + } std::stringstream str; str << port; _log->debug("port : " + str.str()); From cc2dc2396387aae412100e70c3841eba9c08a31d Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 16:41:28 +0200 Subject: [PATCH 41/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20some=20exception=20handling=20that=20were=20a=20little?= =?UTF-8?q?=20SUS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 2 +- src/server/Server.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index 8c0d802..a513196 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -2,7 +2,7 @@ log_file = "test.log" [server] server_names = { "localhost", "2B5.local" } -host = "0.0.0.0" +host = "localhost" port = 8080 [server.error_pages] diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 976b9b6..0dd8220 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/22 16:34:40 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 16:37:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -149,7 +149,7 @@ Server::Server(config::Config *conf) : _conf(conf) { try { _setup(); _run(); - } catch (std::exception &e) { + } catch (std::runtime_error &e) { _log->error(e.what()); } } From 7cd221524f06a356b75902961935655ebf22fb0c Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 23 Apr 2025 12:43:38 +0200 Subject: [PATCH 42/45] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20loop=20nearly=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 2 +- src/server/Client.cpp | 65 ++++++++++++++++---------------------- src/server/Server.cpp | 24 ++++++++------ src/server/ServerUtils.cpp | 15 ++++----- 4 files changed, 49 insertions(+), 57 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index a513196..8c0d802 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -2,7 +2,7 @@ log_file = "test.log" [server] server_names = { "localhost", "2B5.local" } -host = "localhost" +host = "0.0.0.0" port = 8080 [server.error_pages] diff --git a/src/server/Client.cpp b/src/server/Client.cpp index f974dc7..7216e11 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,85 +6,74 @@ /* By: mmoussou #include +#include using namespace server; -Client::Client(int fd, sockaddr_in socket, config::Config *conf): _fd(fd), _client_addr(socket) -{ +Client::Client(int fd, sockaddr_in socket, config::Config *conf) + : _fd(fd), _client_addr(socket) { std::string received_data; - char buffer[BUFFER_SIZE]; - ssize_t bytes_received; - do - { + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do { std::memset(buffer, 0, BUFFER_SIZE); bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received == -1) - { + if (bytes_received == -1) { _log->error("failed to receive request"); continue; } received_data += std::string(buffer, bytes_received); - } - while (buffer[bytes_received]); - + } while (buffer[bytes_received]); this->_getRequest(received_data); this->_conf = conf->getServer(this->_request->getHeaders()["Host"]); - - //if (received_data.length > (get max_body_size from Route corresponding) ) + // if (received_data.length > (get max_body_size from Route corresponding) ) // throw error } -void Client::_getRequest(std::string request_str) -{ - std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1); +void Client::_getRequest(std::string request_str) { + std::string method = request_str.substr( + 0, request_str.substr(0, 4).find_last_not_of(" ") + 1); - if (method == "GET") - { + if (method == "GET") { _log->info("get request received"); this->_request = new http::Get(request_str); - } - else if (method == "DELETE") - { + } else if (method == "DELETE") { _log->info("delete request received"); this->_request = new http::Delete(request_str); - } - else if (method == "POST") - { + } else if (method == "POST") { _log->info("post request received"); this->_request = new http::Post(request_str); - } - else - { + } else { _log->info("unsupported request received"); this->_request = new http::Get(); this->_request->setMethod("501"); } } -void Client::answer(void) -{ +void Client::answer(void) { std::string response; - (void) _client_addr; - - if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST") + (void)_client_addr; + + if (this->_request->getMethod() == "GET" || + this->_request->getMethod() == "DELETE" || + this->_request->getMethod() == "POST") response = this->_request->execute().str(); else - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " + "text/html\r\n\r\n

501 Not " + "Implemented

"; send(this->_fd, response.c_str(), response.length(), 0); } - -Client::~Client(void) -{ +Client::~Client(void) { delete (http::Get *)(this->_request); delete this->_response; } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 0dd8220..bc20ef7 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,12 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/22 16:37:31 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 12:38:15 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include #include #include @@ -71,12 +72,8 @@ void Server::_setup(void) { auto itH = hosts.begin(); for (auto it = range(ports), itH++) { - try { - int fd = _createSocket(*itH, *it); - _fds_server.push_back(fd); - } catch (std::exception &e) { - throw e; - } + int fd = _createSocket(*itH, *it); + _fds_server.push_back(fd); } } @@ -126,11 +123,18 @@ void Server::_run(void) { pfd.fd = client_fd; pfd.events = POLLIN | POLLOUT; _client_fds.push_back(pfd); - _client_data.push_back(new sockaddr_in(client_addr)); + struct sockaddr_in* new_client_sock = new sockaddr_in(); + std::memmove(new_client_sock, &client_addr, sizeof(struct sockaddr_in)); + std::cout << "tamere ==== " << new_client_sock << std::endl; + _client_data.push_back(new_client_sock); } - for (size_t i = 0; i < _client_fds.size(); ++i) { + for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { + std::cout << i << std::endl; if (_client_fds[i].revents & POLLIN) { + std::cout << _client_fds.size() << " " << _client_data.size() << std::endl; + std::cout << "tamere ==== " << _client_data[i] << std::endl; + std::cout << i << std::endl; if (!_handle_client(_client_fds[i], _client_data[i])) { close(_client_fds[i].fd); _client_fds.erase(_client_fds.begin() + i); @@ -149,7 +153,7 @@ Server::Server(config::Config *conf) : _conf(conf) { try { _setup(); _run(); - } catch (std::runtime_error &e) { + } catch (std::exception &e) { _log->error(e.what()); } } diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index 9fc4dd4..c20bc8f 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/22 16:32:45 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 12:33:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include using namespace webserv::server; @@ -84,17 +86,14 @@ int Server::_createSocket(std::string host, int port) { } bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) { - Client *client; - try { - client = new Client(pollfd.fd, *sock_data, _conf); - client->answer(); - } catch (std::exception &e) { + std::cout << "tamere ==== " << sock_data << std::endl; + Client client(pollfd.fd, *sock_data, _conf); + client.answer(); + } catch (std::runtime_error &e) { _log->error(e.what()); return false; } - delete client; - return true; } From e28807078f24db39968ffff3f417a6a436855563 Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 23 Apr 2025 14:40:57 +0200 Subject: [PATCH 43/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(Clie?= =?UTF-8?q?nt):=20fixed=20client=20just=20segfaulting=20on=20destructor=20?= =?UTF-8?q?cuz=20i=20am=20stupid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpIMessage.hpp | 4 +++- includes/requests/HttpResponse.hpp | 4 ++-- includes/server/Client.hpp | 4 ++-- src/requests_handling/HttpResponse.cpp | 2 +- src/server/Client.cpp | 11 ++++------- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index eb63d3e..60511ee 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou getHeaders(void) const; virtual std::string getBody(void) const; + virtual ~IMessage() {} + virtual void setHeaders(std::map const headers); virtual void setBody(std::string const body); diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index cf44d32..8ac7e33 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/22 14:23:44 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 14:39:16 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ class Client { int _fd; struct sockaddr_in _client_addr; http::IRequest *_request; - http::Response *_response; + //http::Response *_response; config::Server *_conf; }; diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index e7549ff..c38e4ec 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,7 +6,7 @@ /* By: mmoussou error("failed to receive request"); - continue; + throw std::runtime_error("failed to receive request"); } received_data += std::string(buffer, bytes_received); } while (buffer[bytes_received]); @@ -59,21 +59,18 @@ void Client::_getRequest(std::string request_str) { } void Client::answer(void) { + (void) _client_addr; std::string response; - (void)_client_addr; if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST") response = this->_request->execute().str(); else - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " - "text/html\r\n\r\n

501 Not " - "Implemented

"; + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; send(this->_fd, response.c_str(), response.length(), 0); } Client::~Client(void) { delete (http::Get *)(this->_request); - delete this->_response; } From 5a2285e3d078ccdc6f2537847330d80b502fea29 Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 23 Apr 2025 15:34:42 +0200 Subject: [PATCH 44/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20but=20why=20i=20don't=20know?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/Server.cpp | 9 ++------- src/server/ServerUtils.cpp | 3 +-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index bc20ef7..1da8308 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/23 12:38:15 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 15:31:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,17 +125,12 @@ void Server::_run(void) { _client_fds.push_back(pfd); struct sockaddr_in* new_client_sock = new sockaddr_in(); std::memmove(new_client_sock, &client_addr, sizeof(struct sockaddr_in)); - std::cout << "tamere ==== " << new_client_sock << std::endl; _client_data.push_back(new_client_sock); } for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { - std::cout << i << std::endl; if (_client_fds[i].revents & POLLIN) { - std::cout << _client_fds.size() << " " << _client_data.size() << std::endl; - std::cout << "tamere ==== " << _client_data[i] << std::endl; - std::cout << i << std::endl; - if (!_handle_client(_client_fds[i], _client_data[i])) { + if (_handle_client(_client_fds[i], _client_data[i])) { close(_client_fds[i].fd); _client_fds.erase(_client_fds.begin() + i); delete _client_data[i]; diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index c20bc8f..114448d 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/23 12:33:47 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 15:33:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,6 @@ int Server::_createSocket(std::string host, int port) { bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) { try { - std::cout << "tamere ==== " << sock_data << std::endl; Client client(pollfd.fd, *sock_data, _conf); client.answer(); } catch (std::runtime_error &e) { From 84407173e0548be2ce5d13cca7d2c475b0f06a49 Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 23 Apr 2025 16:23:20 +0200 Subject: [PATCH 45/45] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20f?= =?UTF-8?q?ixed=20invalid=20read=20and=20accept=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/Server.cpp | 11 ++++++++--- src/server/ServerUtils.cpp | 7 +------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 1da8308..ec8f0cd 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/23 15:31:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 16:22:22 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,6 +93,7 @@ void Server::_run(void) { fd.events = POLLIN; _client_fds.push_back(fd); _client_data.push_back(NULL); + _log->debug("new socket in poll"); } // to add signal instead of 727 @@ -112,6 +113,8 @@ void Server::_run(void) { accept((*it), (struct sockaddr *)&client_addr, &addrlen); if (client_fd < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + continue; std::stringstream str; str << "Accept failed: "; str << strerror(errno); @@ -122,9 +125,11 @@ void Server::_run(void) { pollfd pfd; pfd.fd = client_fd; pfd.events = POLLIN | POLLOUT; + pfd.revents = 0; _client_fds.push_back(pfd); - struct sockaddr_in* new_client_sock = new sockaddr_in(); - std::memmove(new_client_sock, &client_addr, sizeof(struct sockaddr_in)); + struct sockaddr_in *new_client_sock = new sockaddr_in(); + std::memmove(new_client_sock, &client_addr, + sizeof(struct sockaddr_in)); _client_data.push_back(new_client_sock); } diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index 114448d..e2becb6 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/23 15:33:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/23 16:00:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,11 +47,6 @@ int Server::_createSocket(std::string host, int port) { return -1; } - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - close(fd); - throw std::runtime_error("fcntl failed"); - } - int opt = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { close(fd);