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.

114 lines
3.1 KiB

#include "webserv.hpp"
/*|===========|
* Server destructor:
*
* delete all routes owned by the server;
*/
Server::~Server(void) {
for (std::map< string, Route * >::iterator it = _routes.begin();
it != _routes.end(); it++)
delete (*it).second;
cout << "Server destroyed!\n";
}
/*|===========|
2 years ago
* Server constructor:
2 years ago
*
2 years ago
* Input: A server block node given by JSONParser.
2 years ago
* Output: A Server class object and also a Route one as Server herite from
* Route. The Route constructor scrap the routing informations (index, root,
* autoindex ...) and the Server one the others ones (server_name, sub-routes)
2 years ago
*/
2 years ago
Server::Server(JSONNode *server) : Route(NULL, "/", server) {
2 years ago
JSONObject datas = server->obj();
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
/* Get the server name (_server_name)*/
string Server::getName(void) { return _name; }
/* |==========|
* Safely create a master socket:
*
* Input: a "ip:port" string
* Output: a Master socket or NULL if creation failed
*/
Master *Server::create_master(string str) {
ip_port_t listen = get_ip_port_t(str);
if (listen.ip.at(0) == '[') {
cout << "Listen: IPv6 isn't supported\n";
}
try {
_listens.push_back(listen);
Master *sock = new Master(listen);
return (sock);
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
return NULL;
}
}
/*|===========|
2 years ago
* Create server's defined sockets:
*
* Input: A server block node from JSONParser.
2 years ago
* Output: A vector containing all the succesfull created sockets using listens
* from the server block.
2 years ago
*/
2 years ago
std::vector< Master * > Server::get_sockets(JSONNode *server) {
JSONObject datas = server->obj();
std::vector< Master * > ret;
ip_port_t listen;
Master *tmp;
if (datas["listens"]) {
JSONList listens = datas["listens"]->lst();
for (JSONList::iterator it = listens.begin(); it != listens.end();
it++) {
if ((tmp = create_master((*it)->str())))
ret.push_back(tmp);
}
} else if ((tmp = create_master("0.0.0.0")))
ret.push_back(tmp);
return ret;
}
/*|===========|
2 years ago
* Choose the route an uri asked to the server must lead to.
*
* Intput: The uri asked by the client to the server.
2 years ago
* Output: The route object choosen or the server itself if no location block is
* adapted.
2 years ago
*/
2 years ago
Route *Server::choose_route(string uri) {
std::vector< string > req = split(uri, "/");
2 years ago
std::vector< string > root;
for (std::map< string, Route * >::iterator rit = _routes.begin();
rit != _routes.end(); rit++) {
root = split((*rit).first, "/");
2 years ago
std::vector< string >::iterator root_it = root.begin();
for (std::vector< string >::iterator it = req.begin(); it < req.end();
it++) {
while (it != req.end() && *it == "")
it++;
if (*it != *(root_it++))
break;
while (root_it != root.end() && *root_it == "")
root_it++;
if (root_it == root.end())
return ((*rit).second);
}
2 years ago
}
return this;
}