From e0563436c49c36d514b97b959ca24036ecdc2113 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Fri, 11 Nov 2022 15:59:52 +0100 Subject: [PATCH] some more simplifications --- includes/Client.hpp | 3 +- includes/Server.hpp | 1 + includes/webserv.hpp | 3 +- srcs/load/Env.cpp | 44 +++++++++++++++------------ srcs/load/Route.cpp | 3 -- srcs/load/Server.cpp | 71 ++++++++++++++++++++------------------------ srcs/sock/Client.cpp | 11 +++++-- srcs/sock/Master.cpp | 29 +++++++----------- srcs/tools.cpp | 14 ++++----- 9 files changed, 89 insertions(+), 90 deletions(-) diff --git a/includes/Client.hpp b/includes/Client.hpp index 73a6574..9cfec6c 100644 --- a/includes/Client.hpp +++ b/includes/Client.hpp @@ -5,13 +5,14 @@ typedef std::map< string, std::vector< string > > request_t; class Client { int _fd; + listen_t _listen; Master *_parent; string _header; string _content; request_t _request; public: - Client(int fd, Master *parent); + Client(int fd, listen_t listen, Master *parent); ~Client(void); bool getRequest(string paquet); bool parseHeader(); diff --git a/includes/Server.hpp b/includes/Server.hpp index 1137582..530fddd 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -9,6 +9,7 @@ class Server : public Route { std::vector< listen_t > _listens; Server(JSONNode *server); ~Server(void); + Master *create_master(string str); std::vector< Master * > get_sockets(JSONNode *server); Route *choose_route(string uri); string getName(void); diff --git a/includes/webserv.hpp b/includes/webserv.hpp index dd5cbc9..e4be719 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -34,6 +34,7 @@ using std::strerror; using std::string; typedef struct listen_s { + int fd; string ip; int port; } listen_t; @@ -52,8 +53,8 @@ void *ft_memset(void *b, int c, size_t len); bool isInt(string str); std::vector< string > split(string str, char delim); listen_t get_listen_t(string listen); +listen_t get_listen_t(string ip, int port); string getMime(string path); -string get_extension(string str); string read_file(string path); #include "Client.hpp" diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index f1ab64f..04d5712 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -1,5 +1,21 @@ #include "webserv.hpp" +/*|=======================| + * Environment destructor: + * + * The destructor call all servers and sockets destructors. + */ +Env::~Env() { + for (std::vector< Server * >::iterator it = _servers.begin(); + it < _servers.end(); it++) { + delete *it; + } + for (std::vector< Master * >::iterator it = _masters.begin(); + it < _masters.end(); it++) { + delete *it; + } +} + /*|=======================| * Environment constructor: * @@ -18,10 +34,11 @@ Env::Env(JSONNode *conf) { _masters.insert(_masters.end(), tmp_s.begin(), tmp_s.end()); } } catch (std::exception &e) { - cout << e.what(); + std::cerr << e.what() << "\n"; } delete conf; } + void Env::cycle(void) { FD_ZERO(&Master::_readfds); Master::_max_fd = Master::_min_fd; @@ -31,10 +48,11 @@ void Env::cycle(void) { int activity = select(Master::_max_fd + Master::_amount, &(Master::_readfds), NULL, NULL, NULL); if ((activity < 0) && (errno != EINTR)) - cout << "Select: " << strerror(errno) << "\n"; + std::cerr << "Select: " << strerror(errno) << "\n"; cout << "==> Handle requests and answers:\n"; refresh(); } + /*|=======================| * Append each master_sockets and their clients to list of fds SELECT must look * at. @@ -54,21 +72,9 @@ void Env::set_fds(void) { void Env::refresh(void) { for (std::vector< Master * >::iterator it = _masters.begin(); it < _masters.end(); it++) - (*it)->refresh(this); -} - -/*|=======================| - * Environment destructor: - * - * The destructor call all servers and sockets destructors. - */ -Env::~Env() { - for (std::vector< Server * >::iterator it = _servers.begin(); - it < _servers.end(); it++) { - delete *it; - } - for (std::vector< Master * >::iterator it = _masters.begin(); - it < _masters.end(); it++) { - delete *it; - } + try { + (*it)->refresh(this); + } catch (std::exception &e) { + std::cerr << e.what(); + } } diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index b968354..82451cd 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -79,7 +79,6 @@ string Route::correctUri(string uri) { std::vector< string >::iterator it; std::vector< string >::iterator it2; - cout << "Correcting request: " << uri << " with root: " << _root << "\n"; ret << "./" << _root; std::vector< string > loc_split = split(_location, '/'); std::vector< string > uri_split = split(uri, '/'); @@ -96,7 +95,5 @@ string Route::correctUri(string uri) { while (it2 < uri_split.end()) { ret << "/" << *(it2++); } - - cout << "result: " << ret.str() << "\n"; return ret.str(); } diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index 0030f33..0cfaf2e 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -1,5 +1,18 @@ #include "webserv.hpp" +/*|=======================| + * Server destructor: + * + * delete all routes owned by the server; + */ + +Server::~Server(void) { + for (std::map< string, Route * >::iterator it = _routes.begin(); + it != _routes.end(); it++) + delete (*it).second; + cout << "Server destroyed!\n"; +} + /*|=======================| * Server constructor: * @@ -25,6 +38,21 @@ Server::Server(JSONNode *server) : Route(NULL, "/", server) { /* Get the server name (_server_name)*/ string Server::getName(void) { return _name; } +Master *Server::create_master(string str) { + listen_t listen = get_listen_t(str); + if (listen.ip.at(0) == '[') { + cout << "Listen: IPv6 isn't supported\n"; + } + try { + Master *sock = new Master(listen); + _listens.push_back(listen); + return (sock); + } catch (std::exception &e) { + std::cerr << e.what() << '\n'; + return NULL; + } +} + /*|=======================| * Create server's defined sockets: * @@ -32,6 +60,7 @@ string Server::getName(void) { return _name; } * Output: A vector containing all the succesfull created sockets using listens * from the server block. */ + std::vector< Master * > Server::get_sockets(JSONNode *server) { JSONObject datas = server->obj(); std::vector< Master * > ret; @@ -40,31 +69,10 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { JSONList listens = datas["listens"]->lst(); for (JSONList::iterator it = listens.begin(); it != listens.end(); it++) { - listen = get_listen_t((*it)->str()); - cout << listen.ip << ":" << listen.port << " socket creation...\n"; - if (listen.ip.at(0) == '[') { - cout << "Listen: IPv6 isn't supported\n"; - continue; - } - try { - Master *sock = new Master(listen); - ret.push_back(sock); - _listens.push_back(listen); - } catch (std::exception &e) { - cout << e.what() << '\n'; - } - } - } else { - listen = get_listen_t("localhost:80"); - - try { - Master *sock = new Master(listen); - _listens.push_back(listen); - ret.push_back(sock); - } catch (std::exception &e) { - cout << e.what() << '\n'; + ret.push_back(create_master((*it)->str())); } - } + } else + ret.push_back(create_master("localhost:8080")); return ret; } @@ -76,7 +84,6 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { * adapted. */ Route *Server::choose_route(string uri) { - // cout << uri << "\n"; std::vector< string > req = split(uri, '/'); std::vector< string > root; for (std::map< string, Route * >::iterator rit = _routes.begin(); @@ -88,7 +95,6 @@ Route *Server::choose_route(string uri) { it++) { if (*it == "") continue; - cout << *it << " - " << *root_it << "\n"; if (*it != *(root_it++)) break; if (root_it == root.end()) @@ -97,16 +103,3 @@ Route *Server::choose_route(string uri) { } return this; } - -/*|=======================| - * Server destructor: - * - * delete all routes owned by the server; - */ - -Server::~Server(void) { - for (std::map< string, Route * >::iterator it = _routes.begin(); - it != _routes.end(); it++) - delete (*it).second; - cout << "Server destroyed!\n"; -} diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index 581b739..3f45b4e 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -1,9 +1,14 @@ #include "webserv.hpp" -Client::Client(int fd, Master *parent) : _fd(fd), _parent(parent) {} +Client::Client(int fd, listen_t listen, Master *parent) + : _fd(fd), _listen(listen), _parent(parent) { + cout << "New connection, socket fd is " << fd << ", ip is : " << _listen.ip + << ", port : " << _listen.port << "\n"; +} Client::~Client(void) { close(_fd); - cout << "Destroyed client socket\n"; + cout << "Host disconnected, ip " << _listen.ip << ", port " << _listen.port + << "\n"; } bool Client::getRequest(string paquet) { @@ -87,6 +92,8 @@ string Client::header_pick(string key, int id) { return ret; } +inline string get_extension(string str) { return str.substr(str.rfind('.')); } + void Client::answer(Env *env) { string method = header_pick("Method:", 0); string uri = header_pick("Method:", 1); diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index 53cfaeb..fc55765 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -25,11 +25,11 @@ Master::Master(listen_t list) : _listen(list) { if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) throw std::runtime_error("bind() error: " + string(strerror(errno))); - cout << "Listener " << ip << " on port " << port << "\n"; if (listen(_fd, 3) < 0) throw std::runtime_error("listen() error: " + string(strerror(errno))); - cout << "Master: " << _fd << "\n"; + cout << "New master socket with fd " << _fd << " which listen " << ip << ":" + << port << "\n"; if (_fd < _min_fd) _min_fd = _fd; _amount++; @@ -55,18 +55,15 @@ void Master::refresh(Env *env) { if (FD_ISSET(_fd, &_readfds)) { int new_socket = accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen); - if (new_socket < 0) { - cout << "Accept: " << strerror(errno) << "\n"; - exit(EXIT_FAILURE); - } + if (new_socket < 0) + throw std::runtime_error("accept() error:" + + string(strerror(errno))); #ifdef __APPLE__ - // fcntl(new_socket, F_GETNOSIGPIPE); fcntl(new_socket, F_SETFL, O_NONBLOCK); #endif - cout << "New connection, socket fd is " << new_socket - << ", ip is : " << inet_ntoa(_address.sin_addr) - << ", port : " << ntohs(_address.sin_port) << "\n"; - _childs.push_back(new Client(new_socket, this)); + listen_t cli_listen = get_listen_t(inet_ntoa(_address.sin_addr), + ntohs(_address.sin_port)); + _childs.push_back(new Client(new_socket, cli_listen, this)); } int child_fd; for (std::vector< Client * >::iterator it = _childs.begin(); @@ -74,18 +71,14 @@ void Master::refresh(Env *env) { child_fd = (*it)->_fd; if (FD_ISSET(child_fd, &_readfds)) { valread = read(child_fd, buffer, 10000); + buffer[valread] = '\0'; if (valread == 0) { getpeername(child_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen); - cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr) - << ", port " << ntohs(_address.sin_port) << "\n"; delete (*it); _childs.erase(it); - } else { - buffer[valread] = '\0'; - if ((*it)->getRequest(buffer)) - (*it)->answer(env); - } + } else if ((*it)->getRequest(buffer)) + (*it)->answer(env); } } } diff --git a/srcs/tools.cpp b/srcs/tools.cpp index 62983b3..422018a 100644 --- a/srcs/tools.cpp +++ b/srcs/tools.cpp @@ -39,6 +39,13 @@ listen_t get_listen_t(string listen) { return ret; } +listen_t get_listen_t(string ip, int port) { + listen_t ret; + ret.ip = ip; + ret.port = port; + return ret; +} + string getMime(string path) { size_t pos = path.rfind('.'); string extension = (pos == string::npos) ? "txt" : path.substr(pos + 1); @@ -210,13 +217,6 @@ string getMime(string path) { return ("text/plain"); } -string get_extension(string str) { - int dot_pos = str.rfind('.'); - string ret = str.substr(dot_pos); - cout << ret << "\n"; - return ret; -} - string read_file(string path) { string str; string content;