From e54c5e8323c5b090699ce7931d4efd1fe05cac92 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 1 May 2025 12:54:09 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20port=20issue=20and=20main=20loop=20issue=20for=20reasons=20o?= =?UTF-8?q?nly=20gods=20or=20kanel=20knows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 3 +- flake.nix | 83 +++++++++++++++++++++----------------- includes/config/Config.hpp | 15 ++++++- includes/config/Server.hpp | 3 +- includes/server/Client.hpp | 6 +-- src/config/Config.cpp | 12 +----- src/config/Server.cpp | 16 +++++--- src/server/Client.cpp | 39 +++++++----------- src/server/Server.cpp | 22 ++++++---- 9 files changed, 107 insertions(+), 92 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index ad55539..4dd9773 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -37,4 +37,5 @@ port = 9090 methods = { "GET", "DELETE" } root = "/var/lib/docker/volumes/test_data/_data" dirlist = false -cgi.py = "/bin/python" +cgi.py = "/bin/python + diff --git a/flake.nix b/flake.nix index e9ff69b..de5f7de 100644 --- a/flake.nix +++ b/flake.nix @@ -1,39 +1,50 @@ { - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - pogit = { - url = "github:y-syo/pogit"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - }; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + pogit = { + url = "github:y-syo/pogit"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; - outputs = inputs@{ nixpkgs, ... }: - let - supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; - forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { - pkgs = import nixpkgs { inherit system; }; - }); - in { - devShells = forEachSupportedSystem ({ pkgs }: { - default = pkgs.mkShell.override - {} - { - buildInputs = with pkgs;[ - - ]; - hardeningDisable = [ "all" ]; - packages = with pkgs; [ - gcc11 - clang_12 - norminette - valgrind - git - gdb - inputs.pogit.packages.${pkgs.system}.default - compiledb - ]; - }; - }); - }; + outputs = + inputs@{ nixpkgs, ... }: + let + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forEachSupportedSystem = + f: + nixpkgs.lib.genAttrs supportedSystems ( + system: + f { + pkgs = import nixpkgs { inherit system; }; + } + ); + in + { + devShells = forEachSupportedSystem ( + { pkgs }: + { + default = pkgs.mkShell.override { } { + buildInputs = with pkgs; [ + ]; + hardeningDisable = [ "all" ]; + packages = with pkgs; [ + gcc11 + clang_12 + norminette + valgrind + git + gdb + inputs.pogit.packages.${pkgs.system}.default + compiledb + ]; + }; + } + ); + }; } - diff --git a/includes/config/Config.hpp b/includes/config/Config.hpp index e570ce7..10d5a2f 100644 --- a/includes/config/Config.hpp +++ b/includes/config/Config.hpp @@ -6,13 +6,17 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ -/* Updated: 2025/04/22 15:25:39 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 12:48:12 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include "cppeleven.hpp" #include +#include +#include +#include namespace webserv { namespace config { @@ -25,7 +29,14 @@ class Config { Logger *getLogger(void) { return _log; } std::vector getServers(void) { return _servers; } - Server *getServer(const std::string &); + Server *getServer(size_t i) { + try { + Server *srv = _servers.at(i); + return srv; + } catch (std::out_of_range &e) { + return not_nullptr; + } + } private: std::vector _servers; diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index 5dfb258..747d0e9 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 15:25:58 by adjoly ### ########.fr */ +/* Updated: 2025/04/30 17:10:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ class Server { // @brief Can be used to check if a servername is present in this config bool isServerName(const std::string &); + // @brief Can be used to get the route correcponding Route *whatRoute(const URL &); protected: diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index dd21aa2..a7698e9 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/30 14:51:39 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 12:51:51 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ namespace server { class Client { public: - Client(struct pollfd *, sockaddr_in, config::Config *); + Client(struct pollfd *, config::Server *); Client(const Client &cpy); virtual ~Client(void); @@ -54,12 +54,10 @@ class Client { } struct pollfd *_pfd; - struct sockaddr_in _client_addr; http::ARequest *_request; // http::Response *_response; config::Server *_conf; config::Route *_route; - config::Config *_Gconf; }; } // namespace server diff --git a/src/config/Config.cpp b/src/config/Config.cpp index eb34fda..e856321 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/23 17:42:07 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 11:21:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "cppeleven.hpp" #include "node/ANode.hpp" #include +#include #include using namespace webserv::config; @@ -60,12 +61,3 @@ Config::~Config(void) { delete *it; } } - -Server *Config::getServer(const std::string &server_name) { - for (auto it = range(_servers)) { - if ((*it)->isServerName(server_name)) { - return (*it); - } - } - return (not_nullptr); -} diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 11a9740..f76905a 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,13 +6,16 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/30 15:37:18 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 10:08:15 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "config/URL.hpp" #include "cppeleven.hpp" +#include "log.hpp" #include +#include +#include #include using namespace webserv::config; @@ -75,9 +78,10 @@ Server::Server(toml::ANode *node) : _table(node) { std::map *location_table = it->second->getTable(); for (it = location_table->begin(); it != location_table->end(); it++) { - if (_routes->find(URL(it->first)) != _routes->end()) + if (_routes->find(URL(it->first)) != _routes->end()) { continue; - (*_routes)[URL(it->first)] = new Route(it->second); + } + _routes->insert(std::make_pair(URL(it->first), new Route(it->second))); } } // delete _table; @@ -129,8 +133,8 @@ Route *Server::whatRoute(const URL &url) { } } std::map::iterator it = _routes->find(URL("/")); - if (it == _routes->end()) { - return not_nullptr; + if (it != _routes->end()) { + return it->second; } - return it->second; + return not_nullptr; } diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 0d37ae8..c659ae6 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _conf = this->_Gconf->getServer(this->_request->getHeaders()["Host"]); - this->_route = this->_conf->whatRoute(URL(this->_request->getTarget())); - std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl; - if(!this->_route || this->_route == not_nullptr) - { + _route = _conf->whatRoute(URL(this->_request->getTarget())); + /* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl; + */ + if (!this->_route || this->_route == not_nullptr) { this->_request->setMethod("404"); - return ; + return; } - if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) - || (this->_request->getMethod() == "POST" && !_route->getMethods()[1]) - || (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2])) + if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) || + (this->_request->getMethod() == "POST" && !_route->getMethods()[1]) || + (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2])) this->_request->setMethod("405"); if (received_data.length() > (unsigned long)(_route->getMaxBody())) @@ -75,23 +74,16 @@ 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") { this->_request = new http::Get(request_str); _log->info("get request received"); - } - else if (method == "DELETE") - { + } else if (method == "DELETE") { this->_request = new http::Delete(request_str); _log->info("delete request received"); - } - else if (method == "POST") - { + } else if (method == "POST") { this->_request = new http::Post(request_str); _log->info("post request received"); - } - else - { + } else { this->_request = new http::Get(); this->_request->setMethod("501"); _log->info("unsupported request received"); @@ -100,7 +92,6 @@ void Client::_getRequest(std::string request_str) { } void Client::answer(void) { - (void)_client_addr; std::string response; if (_request == not_nullptr) { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index ca46bdf..3c25e98 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/30 15:41:46 by adjoly ### ########.fr */ +/* Updated: 2025/05/01 12:52:46 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -121,7 +121,6 @@ void Server::_run(void) { size_t i = 0; for (auto it = range(_fds_server), i++) { if (_client_fds[i].revents & POLLIN) { - _log->info("polliiiiiiiiiiiiiiiinnnnnnnnnnnnnnnn"); struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); int client_fd = @@ -137,8 +136,15 @@ void Server::_run(void) { _log->error(str.str()); continue; } - - _log->info("connection accepted"); + config::Server *conf_srv = _conf->getServer(i); + if (conf_srv == not_nullptr) { + _log->warn( + "where the f does he come from"); // does can't actually + // happen but just in + // case + close(client_fd); + continue; + } struct pollfd pfd; pfd.fd = client_fd; @@ -147,7 +153,7 @@ void Server::_run(void) { _client_fds.push_back(pfd); struct pollfd *ppfd = _client_fds.data() + _client_fds.size() - 1; - Client *new_client = new Client(ppfd, client_addr, _conf); + Client *new_client = new Client(ppfd, conf_srv); if (new_client == NULL) { continue; } @@ -195,7 +201,7 @@ void Server::_run(void) { break; } } - i++; + i--; } } } @@ -214,7 +220,7 @@ Server::Server(config::Config *conf) : _conf(conf) { Server::~Server(void) { log("➖", "Server::Server", "destructor called"); - for (std::vector::iterator it = _client_fds.begin(); - it != _client_fds.end(); it++) + for (auto it = range(_client_fds)) { close(it->fd); + } }