#include "webserv.hpp" Env::Env(JSONNode *conf) { try { JSONList servers = conf->obj()["servers"]->lst(); int i = 0; string th[8] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eigth"}; for (std::vector::iterator it = servers.begin(); it < servers.end(); it++) { Server *server = new Server(*it); _servers.push_back(server); std::vector tmp_s = server->get_sockets(*it); _sockets.insert(_sockets.end(), tmp_s.begin(), tmp_s.end()); // delete *it; cout << th[i] << " server launched.\n"; i++; } } catch (std::exception &e) { cout << e.what(); } delete conf; } Server *Env::choose_server(Socket *sock, string host) { std::vector exact; std::vector inrange; std::vector ip_listen; std::vector 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: " << sock->_listen.ip << ":" << sock->_listen.port << "\n"; ip_required = 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++) { if (sock->_listen.port != (*it).port) continue; if (sock->_listen.ip == (*it).ip) { exact.push_back(*sit); continue; } bool is_inrange = true; ip_listen = split((*it).ip, '.'); std::vector::iterator r = ip_required.begin(); for (std::vector::iterator l = ip_listen.begin(); l < ip_listen.end(); l++) { if (*l != *r && *l != "0") is_inrange = false; } if (is_inrange == true) inrange.push_back(*sit); } } 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) { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) (*it)->set_fds(); } void Env::refresh(void) { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) (*it)->refresh(this); }