diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 04d5712..e70023c 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -1,6 +1,6 @@ #include "webserv.hpp" -/*|=======================| +/*|==========| * Environment destructor: * * The destructor call all servers and sockets destructors. @@ -16,7 +16,7 @@ Env::~Env() { } } -/*|=======================| +/*|==========| * Environment constructor: * * Input: The JSONParser output @@ -38,6 +38,12 @@ Env::Env(JSONNode *conf) { } delete conf; } +/*|==========| + * One server cycle + * - append sockets to listen to select list + * - select them + * - refresh and handle requests + */ void Env::cycle(void) { FD_ZERO(&Master::_readfds); @@ -52,19 +58,19 @@ void Env::cycle(void) { cout << "==> Handle requests and answers:\n"; refresh(); } - -/*|=======================| +/*|==========| * Append each master_sockets and their clients to list of fds SELECT must look * at. */ void Env::set_fds(void) { for (std::vector< Master * >::iterator it = _masters.begin(); - it < _masters.end(); it++) - (*it)->set_fds(); + it < _masters.end(); it++) { + if (*it) + (*it)->set_fds(); + } } - -/*|=======================| +/*|==========| * Refresh all master_sockets and their clients datas (disconnect, new * connection, etc..) and parse requests recieved. */ diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 82451cd..c07f005 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -1,4 +1,14 @@ #include "webserv.hpp" +/* |==========| + * Route constructor: + * A route is a class which is used with each request to know how to handle uri + * requested They are defined with conf file from locations block for raw routes + * and from servers blocks for Server class which herite from Route + * + * Input: The Server parent for locations blocks, the uri which lead to the + * route and the JSON node giving conf datas + * Output: A Route object + */ Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location) { @@ -30,12 +40,20 @@ Route::Route(Server *server, string location, JSONNode *datas) } } } +/* Route destructor */ Route::~Route(void) {} +/* Getters ... */ string Route::getLocation(void) { return _location; } string Route::getRoot(void) { return _root; } string Route::getReturn(void) { return _ret; } +/* |==========| + * Find either an autoindex or an index into the directory required by request + * + * Input: The uri client asked, the real local path to the directory + * Output: The file or the autoindex page to display + */ string Route::getIndex(string uri, string path) { std::stringstream content; @@ -73,6 +91,12 @@ string Route::getIndex(string uri, string path) { ret << "\r\n" << content.str(); return ret.str(); } +/* |==========| + * Correct the uri the client asked + * + * Input: The uri + * Output: The local path corresponding to that uri in the route + */ string Route::correctUri(string uri) { std::stringstream ret; diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index 0cfaf2e..22a76b2 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -1,6 +1,5 @@ #include "webserv.hpp" - -/*|=======================| +/*|===========| * Server destructor: * * delete all routes owned by the server; @@ -12,8 +11,7 @@ Server::~Server(void) { delete (*it).second; cout << "Server destroyed!\n"; } - -/*|=======================| +/*|===========| * Server constructor: * * Input: A server block node given by JSONParser. @@ -21,6 +19,7 @@ Server::~Server(void) { * Route. The Route constructor scrap the routing informations (index, root, * autoindex ...) and the Server one the others ones (server_name, sub-routes) */ + Server::Server(JSONNode *server) : Route(NULL, "/", server) { JSONObject datas = server->obj(); if (datas["server_name"]) @@ -36,7 +35,14 @@ Server::Server(JSONNode *server) : Route(NULL, "/", server) { } /* Get the server name (_server_name)*/ + string Server::getName(void) { return _name; } +/* |==========| + * Safely create a master socket: + * + * Input: a "ip:port" string + * Output: a Master socket or NULL if creation failed + */ Master *Server::create_master(string str) { listen_t listen = get_listen_t(str); @@ -44,16 +50,15 @@ Master *Server::create_master(string str) { cout << "Listen: IPv6 isn't supported\n"; } try { - Master *sock = new Master(listen); _listens.push_back(listen); + Master *sock = new Master(listen); return (sock); } catch (std::exception &e) { std::cerr << e.what() << '\n'; return NULL; } } - -/*|=======================| +/*|===========| * Create server's defined sockets: * * Input: A server block node from JSONParser. @@ -65,24 +70,26 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { JSONObject datas = server->obj(); std::vector< Master * > ret; listen_t listen; + Master *tmp; if (datas["listens"]) { JSONList listens = datas["listens"]->lst(); for (JSONList::iterator it = listens.begin(); it != listens.end(); it++) { - ret.push_back(create_master((*it)->str())); + if ((tmp = create_master((*it)->str()))) + ret.push_back(tmp); } - } else - ret.push_back(create_master("localhost:8080")); + } else if ((tmp = create_master("0.0.0.0"))) + ret.push_back(tmp); return ret; } - -/*|=======================| +/*|===========| * Choose the route an uri asked to the server must lead to. * * Intput: The uri asked by the client to the server. * Output: The route object choosen or the server itself if no location block is * adapted. */ + Route *Server::choose_route(string uri) { std::vector< string > req = split(uri, '/'); std::vector< string > root; @@ -90,7 +97,6 @@ Route *Server::choose_route(string uri) { rit != _routes.end(); rit++) { root = split((*rit).first, '/'); std::vector< string >::iterator root_it = root.begin(); - for (std::vector< string >::iterator it = req.begin(); it < req.end(); it++) { if (*it == "") diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index 3f45b4e..bf3ced6 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -54,7 +54,6 @@ bool Client::parseHeader() { for (std::vector< string >::iterator it = lines.begin() + 1; it < lines.end(); it++) { line = split(*it, ' '); - cout << line.at(0) << "scraped from header\n"; _request[line.at(0)] = std::vector< string >(line.begin() + 1, line.end()); } diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index fc55765..72b6684 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -1,9 +1,19 @@ #include "webserv.hpp" +/* Master destructor */ Master::~Master(void) { close(_fd); cout << "Destroyed master socket\n"; } +/* |==========| + * Master constructor + * Try to create a socket listening to ip and port defined by input. + * If the creation success, the socket is then ready to select for new clients. + * + * Input: A listen_t structure which contain the ip and the port the master care + * about. + * Output: A Master object. + */ Master::Master(listen_t list) : _listen(list) { int opt = 1; @@ -34,6 +44,7 @@ Master::Master(listen_t list) : _listen(list) { _min_fd = _fd; _amount++; } +/* Set into static Master::readfds the active fds which will be select.*/ void Master::set_fds(void) { FD_SET(_fd, &_readfds); @@ -47,6 +58,12 @@ void Master::set_fds(void) { _max_fd = child_fd; } } +/* |==========| + * Refresh master socket datas after select() + * - look first for new clients + * - look then if known clients sent requests or disconnected + * - if client sent request, handle it to generate answer adapted + */ void Master::refresh(Env *env) { int valread; @@ -88,11 +105,8 @@ Server *Master::choose_server(Env *env, string host) { std::vector< Server * > inrange; std::vector< string > ip_listen; std::vector< string > ip_required; - // string ip = inet_ntoa(sock->_address.sin_addr); - // int port = ntohs(sock->_address.sin_port); - // cout << "Which server for " << ip << ":" << port << "?\n"; - cout << "Socket: " << _listen.ip << ":" << _listen.port << "\n"; + cout << "Requested: " << _listen.ip << ":" << _listen.port << "\n"; ip_required = split(_listen.ip, '.'); for (std::vector< Server * >::iterator sit = env->_servers.begin(); sit < env->_servers.end(); sit++) { @@ -110,8 +124,8 @@ Server *Master::choose_server(Env *env, string host) { bool is_inrange = true; ip_listen = split((*it).ip, '.'); std::vector< string >::iterator r = ip_required.begin(); - for (std::vector< string >::iterator l = ip_listen.begin(); - l < ip_listen.end(); l++) { + for (std::vector< string >::iterator l = ip_listen.end(); + l >= ip_listen.begin(); --l) { if (*l != *r && *l != "0") is_inrange = false; } @@ -120,12 +134,22 @@ Server *Master::choose_server(Env *env, string host) { } } if (exact.size() == 0) { + cout << "Inrange: "; for (std::vector< Server * >::iterator sit = inrange.begin(); sit < inrange.end(); sit++) { - if (host == (*sit)->getName()) + cout << "- " << (*sit)->getName() << "\n"; + if (host == ((*sit)->getName() + "\r")) return *sit; } return inrange.front(); - } else + } else { + cout << "Exact: \n"; + for (std::vector< Server * >::iterator sit = exact.begin(); + sit < exact.end(); sit++) { + cout << "- " << (*sit)->getName() << "\n"; + if (host == ((*sit)->getName() + "\r")) + return *sit; + } return exact.front(); + } }