From a2cf80bfdecef525284f77276e77de02fc522ec0 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 18 Apr 2025 10:09:46 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=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) {