#include "webserv.hpp" Server::Server(JSONNode *server) : Route("/", server) { JSONObject datas = server->obj(); if (datas["server_name"]) _name = datas["server_name"]->str(); if (datas["locations"]) { JSONObject locations = datas["locations"]->obj(); for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) { Route *route = new Route((*it).first, (*it).second); _routes[(*it).first] = route; } } } 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; listen_t listen; if (datas["listens"]) { 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; } _listens.push_back(listen); Socket *sock = new Socket(listen); if (sock->launch() == EXIT_SUCCESS) ret.push_back(sock); else delete sock; } } else { listen = get_listen_t("localhost:80"); Socket *sock = new Socket(listen); _listens.push_back(listen); if (sock->launch() == EXIT_SUCCESS) { ret.push_back(sock); } } return ret; } Route *Server::get_route(string uri) { cout << uri << "\n"; std::vector req = split(uri, '/'); std::vector root; for (std::map::iterator rit = _routes.begin(); rit != _routes.end(); rit++) { root = split((*rit).first, '/'); std::vector::iterator root_it = root.begin(); for (std::vector::iterator it = req.begin(); it < req.end(); it++) { if (*it == "") continue; cout << *it << " - " << *root_it << "\n"; if (*it != *(root_it++)) break; if (root_it == root.end()) return ((*rit).second); } } return this; }