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.

98 lines
3.0 KiB

/*
2 years ago
* @file Server.cpp
* @brief The servers object. One is created for each config server.
* @author Narnaud
* @version 0.1
* @date 2023-01-12
*/
2 years ago
#include "webserv.hpp"
/**
* @brief Constructor.
2 years ago
*
2 years ago
* The Route constructor scrap the routing informations (index, root,
2 years ago
* autoindex ...) and the Server one the others ones (server_name, sub-routes)
2 years ago
*
* @param server A server block JSONNode get by parser.
*
2 years ago
*/
2 years ago
Server::Server(JSONNode *server) : Route(NULL, "/", server) {
2 years ago
JSONObject datas = server->obj();
2 years ago
if (datas["server_name"]) _name = datas["server_name"]->str();
if (datas["locations"]) {
JSONObject locations = datas["locations"]->obj();
2 years ago
for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) {
2 years ago
Route *route = new Route(this, (*it).first, (*it).second);
_routes[(*it).first] = route;
}
}
}
2 years ago
/**
* @brief Destructor.
*
2 years ago
* Delete alocated routes.
*/
Server::~Server(void) {
for (std::map<string, Route *>::iterator it = _routes.begin(); it != _routes.end(); it++) delete (*it).second;
cout << "Server destroyed!\n";
}
/**
* @return The server name (server_name)
*/
2 years ago
string Server::getName(void) { return _name; }
2 years ago
/**
* @brief Create server's defined sockets:
*
* @param server A server block node from JSONParser.
2 years ago
*
2 years ago
* @retrn A vector containing all the succesfull created sockets using
2 years ago
* listens from the server block.
2 years ago
*/
std::vector<Master *> Server::create_masters(JSONNode *server) {
2 years ago
JSONObject datas = server->obj();
std::vector<Master *> ret;
2 years ago
if (datas["listens"]) {
JSONList listens = datas["listens"]->lst();
for (JSONList::iterator it = listens.begin(); it != listens.end(); it++)
_listens.push_back(get_ip_port_t((*it)->str()));
} else _listens.push_back(get_ip_port_t("0.0.0.0"));
for (std::vector<ip_port_t>::iterator listen = _listens.begin(); listen < _listens.end(); listen++) {
if (listen->ip.at(0) != '[') try {
ret.push_back(new Master(*listen));
} catch (std::exception &e) {
std::cerr << "Ip: " << listen->ip << ", port: " << listen->port << " -> " << e.what() << '\n';
}
else cout << "Listen: IPv6 isn't supported\n";
}
return ret;
// else if ((tmp = create_master("0.0.0.0"))) ret.push_back(tmp); return ret;
}
2 years ago
/**
* @brief Choose the route an uri asked to the server.
*
* @param uri The uri asked by the client to the server.
2 years ago
*
2 years ago
* @return The route object choosen or the server itself if no location is found
2 years ago
*/
2 years ago
Route *Server::choose_route(string uri) {
2 years ago
vec_string uri_words, loc_words;
uri_words = split(uri, "/");
2 years ago
for (std::map<string, Route *>::iterator loc_it = _routes.begin(); loc_it != _routes.end(); loc_it++) {
2 years ago
loc_words = split((*loc_it).first, "/");
vec_string::iterator loc_word = loc_words.begin();
2 years ago
vec_string::iterator uri_word = uri_words.begin();
while (loc_word != loc_words.end() && uri_word != uri_words.end() && *loc_word == *uri_word) {
while (++loc_word != loc_words.end() && *loc_word == "") {}
while (++uri_word != uri_words.end() && *uri_word == "") {}
}
if (loc_word == loc_words.end()) return ((*loc_it).second);
2 years ago
}
return this;
}