diff --git a/default.json b/default.json index f469081..66d707d 100644 --- a/default.json +++ b/default.json @@ -24,7 +24,6 @@ "root": "images/", "autoindex": true } - } } ] diff --git a/includes/Env.hpp b/includes/Env.hpp index e6e4b77..d7e2ab7 100644 --- a/includes/Env.hpp +++ b/includes/Env.hpp @@ -7,7 +7,7 @@ class Env { public: Env(JSONNode *conf); - void set_fds(); - void refresh(); + void set_fds(void); + void refresh(void); Server *choose_server(Socket *sock, string host); }; diff --git a/includes/Server.hpp b/includes/Server.hpp index 76bb661..4d57949 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -7,7 +7,8 @@ class Server : public Route { public: std::vector _listens; Server(JSONNode *server); - ~Server(); + ~Server(void); std::vector get_sockets(JSONNode *server); Route *get_route(string uri); + string getName(void); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index bf6d339..4755fb0 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -14,9 +14,9 @@ class Socket { static int _min_fd; static int _amount; Socket(listen_t listen); - ~Socket(); - int launch(); - void set_fds(); + ~Socket(void); + int launch(void); + void set_fds(void); void refresh(Env *env); void answer(Env *env, int fd, string request); void send_answer(int fd, string msg); diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 9f0d43f..05619eb 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -42,8 +42,8 @@ typedef std::map JSONObject; typedef std::vector JSONList; typedef struct listen_s { - string ip; - int port; + string ip; + int port; } listen_t; void *ft_memset(void *b, int c, size_t len); diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 50e6385..90284a5 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -25,42 +25,60 @@ Env::Env(JSONNode *conf) { Server *Env::choose_server(Socket *sock, string host) { std::vector exact; std::vector inrange; - string ip = inet_ntoa(sock->_address.sin_addr); - int port = ntohs(sock->_address.sin_port); + std::vector ip_list; + std::vector ip_requ; + // string ip = inet_ntoa(sock->_address.sin_addr); + // int port = ntohs(sock->_address.sin_port); - cout << "Which server for " << ip << ":" << port << "?\n"; + // cout << "Which server for " << ip << ":" << port << "?\n"; cout << "Socket: " << sock->_listen.ip << ":" << sock->_listen.port << "\n"; - (void)host; + ip_requ = split(sock->_listen.ip, '.'); for (std::vector::iterator sit = _servers.begin(); sit < _servers.end(); sit++) { + std::vector serv_listens = (*sit)->_listens; for (std::vector::iterator it = serv_listens.begin(); it < serv_listens.end(); it++) { + + ip_list = split((*it).ip, '.'); if (sock->_listen.port != (*it).port) continue; - if (sock->_listen.ip == (*it).ip) + if (sock->_listen.ip == (*it).ip) { exact.push_back(*sit); - else if (sock->_listen.ip == (*it).ip) + continue; + } + bool is_inrange = true; + ip_list = split((*it).ip, '.'); + std::vector::iterator r = ip_requ.begin(); + for (std::vector::iterator l = ip_list.begin(); + l < ip_list.end(); l++) { + if (*l != *r && *l != "0") + is_inrange = false; + } + if (is_inrange == true) inrange.push_back(*sit); // else if (is_ip_into(sock->_listen.ip, (*it).ip)) } } - if (exact.at(0)) - return (exact.at(0)); - else - return (inrange.at(0)); + if (exact.size() == 0) { + for (std::vector::iterator sit = inrange.begin(); + sit < inrange.end(); sit++) { + if (host == (*sit)->getName()) + return *sit; + } + return inrange.front(); + } else + return exact.front(); } -void Env::set_fds() { +void Env::set_fds(void) { for (std::vector::iterator it = _sockets.begin(); - it < _sockets.end(); it++) { + it < _sockets.end(); it++) (*it)->set_fds(); - } } -void Env::refresh() { +void Env::refresh(void) { for (std::vector::iterator it = _sockets.begin(); - it < _sockets.end(); it++) { + it < _sockets.end(); it++) (*it)->refresh(this); - } } diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 1d8be95..35cb04f 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -11,8 +11,7 @@ Route::Route(string location, JSONNode *datas) : _location(location) { _autoindex = tmp->boo(); if ((tmp = object["indexs"])) { JSONList indexs = tmp->lst(); - for (JSONList::iterator it = indexs.begin(); - it < indexs.end(); it++) { + for (JSONList::iterator it = indexs.begin(); it < indexs.end(); it++) { _indexs.push_back((*it)->str()); } } @@ -20,7 +19,7 @@ Route::Route(string location, JSONNode *datas) : _location(location) { Route::~Route(void) {} -string Route::getLocation(void) {return _location; } +string Route::getLocation(void) { return _location; } string Route::getRoot(void) { return _root; } string Route::getReturn(void) { return _ret; } std::vector Route::getIndexs(void) { return _indexs; } @@ -55,14 +54,13 @@ string Route::getAutoindex(string uri) { string Route::correctUri(string uri) { std::stringstream ret; - //int slash_pos; - //string root = _root; - //int i = 0; + // int slash_pos; + // string root = _root; + // int i = 0; std::vector::iterator it; std::vector::iterator it2; - cout << "Correcting request: " << uri - << " with root: " << _root << "\n"; + cout << "Correcting request: " << uri << " with root: " << _root << "\n"; ret << _root; std::vector loc_split = split(_location, '/'); std::vector uri_split = split(uri, '/'); @@ -94,4 +92,3 @@ string Route::correctUri(string uri) { cout << "resutlt: " << ret.str() << "\n"; return ret.str(); } - diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index f6d3e67..caf5839 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -14,6 +14,10 @@ Server::Server(JSONNode *server) : Route("/", server) { } } +Server::~Server(void) { cout << "Server destroyed!\n"; } + +string Server::getName(void) { return _name; } + std::vector Server::get_sockets(JSONNode *server) { JSONObject datas = server->obj(); std::vector ret; @@ -46,8 +50,6 @@ std::vector Server::get_sockets(JSONNode *server) { return ret; } -Server::~Server(void) { cout << "Server destroyed!\n"; } - Route *Server::get_route(string uri) { cout << uri << "\n"; std::vector req = split(uri, '/'); diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 9821852..98aa176 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -4,12 +4,12 @@ Socket::Socket(listen_t listen) : _listen(listen) { _clients_amount = 0; } -Socket::~Socket() { +Socket::~Socket(void) { close(_master_socket); cout << "Socket destroyed!\n"; } -int Socket::launch() { +int Socket::launch(void) { int opt = 1; string ip = _listen.ip; int port = _listen.port; @@ -48,7 +48,7 @@ int Socket::launch() { return (EXIT_SUCCESS); } -void Socket::set_fds() { +void Socket::set_fds(void) { FD_SET(_master_socket, &_readfds); for (std::vector::iterator it = _clients.begin(); it < _clients.end(); diff --git a/srcs/tools.cpp b/srcs/tools.cpp index 2db8e22..635e796 100644 --- a/srcs/tools.cpp +++ b/srcs/tools.cpp @@ -20,20 +20,21 @@ bool isInt(string str) { std::vector split(string str, char delim) { std::vector tokens; - std::string token; - std::stringstream ss(str); - while (getline(ss, token, delim)){ - tokens.push_back(token); - } + std::string token; + std::stringstream ss(str); + while (getline(ss, token, delim)) { + tokens.push_back(token); + } return tokens; } + listen_t get_listen_t(string listen) { - listen_t ret; + listen_t ret; size_t sep_pos = listen.rfind(':'); - string tmp = listen.substr(0, sep_pos); + ret.ip = isInt(tmp) ? "0.0.0.0" : (tmp == "localhost" ? "127.0.0.1" : tmp); tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str(); ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); - return ret; + return ret; } diff --git a/srcs/webserv.cpp b/srcs/webserv.cpp index 112fcc5..5d087c4 100644 --- a/srcs/webserv.cpp +++ b/srcs/webserv.cpp @@ -16,11 +16,13 @@ int main(int ac, char **av) { cout << "Setting environment...\n"; Env env(conf); + cout << "Environement setup.\n"; + while (1) { - cout << "|===|===|===| CYCLE |===|===|===|\n"; FD_ZERO(&Socket::_readfds); Socket::_max_fd = Socket::_min_fd; env.set_fds(); + cout << "|===|===|===| SELECT |===|===|===|\n"; int activity = select(Socket::_max_fd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL); if ((activity < 0) && (errno != EINTR))