#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).second); _routes[(*it).first] = route; } } } 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()); _listens.push_back(listen); Socket *sock; if (listen.type == AF_INET) sock = new Socket(listen); else sock = new Sock6(listen); if (sock->launch() == EXIT_SUCCESS) ret.push_back(sock); else delete sock; } } else { Socket *sock = new Socket(get_listen_t("localhost:80")); if (sock->launch() == EXIT_SUCCESS) { ret.push_back(sock); } } return ret; } Server::~Server(void) { cout << "Server destroyed!\n"; } 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; }