|
|
@ -1,5 +1,18 @@ |
|
|
|
#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"; |
|
|
|
} |
|
|
|
|
|
|
|
/*|=======================|
|
|
|
|
* Server constructor: |
|
|
|
* |
|
|
@ -25,6 +38,21 @@ Server::Server(JSONNode *server) : Route(NULL, "/", server) { |
|
|
|
/* Get the server name (_server_name)*/ |
|
|
|
string Server::getName(void) { return _name; } |
|
|
|
|
|
|
|
Master *Server::create_master(string str) { |
|
|
|
listen_t listen = get_listen_t(str); |
|
|
|
if (listen.ip.at(0) == '[') { |
|
|
|
cout << "Listen: IPv6 isn't supported\n"; |
|
|
|
} |
|
|
|
try { |
|
|
|
Master *sock = new Master(listen); |
|
|
|
_listens.push_back(listen); |
|
|
|
return (sock); |
|
|
|
} catch (std::exception &e) { |
|
|
|
std::cerr << e.what() << '\n'; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*|=======================|
|
|
|
|
* Create server's defined sockets: |
|
|
|
* |
|
|
@ -32,6 +60,7 @@ string Server::getName(void) { return _name; } |
|
|
|
* Output: A vector containing all the succesfull created sockets using listens |
|
|
|
* from the server block. |
|
|
|
*/ |
|
|
|
|
|
|
|
std::vector< Master * > Server::get_sockets(JSONNode *server) { |
|
|
|
JSONObject datas = server->obj(); |
|
|
|
std::vector< Master * > ret; |
|
|
@ -40,31 +69,10 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { |
|
|
|
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; |
|
|
|
} |
|
|
|
try { |
|
|
|
Master *sock = new Master(listen); |
|
|
|
ret.push_back(sock); |
|
|
|
_listens.push_back(listen); |
|
|
|
} catch (std::exception &e) { |
|
|
|
cout << e.what() << '\n'; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
listen = get_listen_t("localhost:80"); |
|
|
|
|
|
|
|
try { |
|
|
|
Master *sock = new Master(listen); |
|
|
|
_listens.push_back(listen); |
|
|
|
ret.push_back(sock); |
|
|
|
} catch (std::exception &e) { |
|
|
|
cout << e.what() << '\n'; |
|
|
|
ret.push_back(create_master((*it)->str())); |
|
|
|
} |
|
|
|
} |
|
|
|
} else |
|
|
|
ret.push_back(create_master("localhost:8080")); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
@ -76,7 +84,6 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { |
|
|
|
* adapted. |
|
|
|
*/ |
|
|
|
Route *Server::choose_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(); |
|
|
@ -88,7 +95,6 @@ Route *Server::choose_route(string uri) { |
|
|
|
it++) { |
|
|
|
if (*it == "") |
|
|
|
continue; |
|
|
|
cout << *it << " - " << *root_it << "\n"; |
|
|
|
if (*it != *(root_it++)) |
|
|
|
break; |
|
|
|
if (root_it == root.end()) |
|
|
@ -97,16 +103,3 @@ Route *Server::choose_route(string uri) { |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
/*|=======================|
|
|
|
|
* 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"; |
|
|
|
} |
|
|
|