You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

64 lines
1.7 KiB

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