Webserv
Loading...
Searching...
No Matches
Server.cpp
Go to the documentation of this file.
1
9#include "webserv.hpp"
10
20Server::Server(JSONNode *server) : Route(NULL, "/", server) {
21 JSONObject datas = server->obj();
22 if (datas["server_name"])
23 _name = datas["server_name"]->str();
24 if (datas["locations"]) {
25 JSONObject locations = datas["locations"]->obj();
26 for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) {
27 Route *route = new Route(this, (*it).first, (*it).second);
28 _routes[(*it).first] = route;
29 }
30 }
31}
32
39 for (std::map<string, Route *>::iterator it = _routes.begin(); it != _routes.end(); it++)
40 delete (*it).second;
41 cout << "Server destroyed!\n";
42}
43
47string Server::getName(void) { return _name; }
48
57 ip_port_t listen = get_ip_port_t(str);
58 if (listen.ip.at(0) != '[') {
59 try {
60 _listens.push_back(listen);
61 Master *sock = new Master(listen);
62 return (sock);
63 } catch (std::exception &e) {
64 std::cerr << e.what() << '\n';
65 }
66 } else
67 cout << "Listen: IPv6 isn't supported\n";
68 return NULL;
69}
70
79std::vector<Master *> Server::get_sockets(JSONNode *server) {
80 JSONObject datas = server->obj();
81 std::vector<Master *> ret;
82 Master *tmp;
83 ip_port_t listen;
84 if (datas["listens"]) {
85 JSONList listens = datas["listens"]->lst();
86 for (JSONList::iterator it = listens.begin(); it != listens.end(); it++) {
87 if ((tmp = create_master((*it)->str())))
88 ret.push_back(tmp);
89 }
90 } else if ((tmp = create_master("0.0.0.0")))
91 ret.push_back(tmp);
92 return ret;
93}
94
103 vec_string uri_words, loc_words;
104 uri_words = split(uri, "/");
105 for (std::map<string, Route *>::iterator loc_it = _routes.begin(); loc_it != _routes.end(); loc_it++) {
106 loc_words = split((*loc_it).first, "/");
107 vec_string::iterator loc_word = loc_words.begin();
108 for (vec_string::iterator uri_word = uri_words.begin(); uri_word < uri_words.end(); uri_word++) {
109 while (uri_word != uri_words.end() && *uri_word == "")
110 uri_word++;
111 if (*uri_word != *(loc_word++))
112 break;
113 while (loc_word != loc_words.end() && *loc_word == "")
114 loc_word++;
115 if (loc_word == loc_words.end())
116 return ((*loc_it).second);
117 }
118 }
119 return this;
120}
JSONObject obj()
Definition: Nodes.cpp:3
Definition: Master.hpp:4
Definition: Route.hpp:4
Route * choose_route(string uri)
Choose the route an uri asked to the server.
Definition: Server.cpp:102
std::vector< Master * > get_sockets(JSONNode *server)
Create server's defined sockets:
Definition: Server.cpp:79
Master * create_master(string str)
Master socket safe creation.
Definition: Server.cpp:56
std::vector< ip_port_t > _listens
The list of listens the server which are linked to the server.
Definition: Server.hpp:9
~Server(void)
Destructor.
Definition: Server.cpp:38
string _name
The server name.
Definition: Server.hpp:5
Server(JSONNode *server)
Constructor.
Definition: Server.cpp:20
string getName(void)
Definition: Server.cpp:47
std::map< string, Route * > _routes
The server's routings with a route object as object and his location as key.
Definition: Server.hpp:6
string ip
Definition: webserv.hpp:37
vec_string split(string str, string delim)
Definition: tools.cpp:20
ip_port_t get_ip_port_t(string listen)
Definition: tools.cpp:35
std::vector< JSONNode * > JSONList
Definition: webserv.hpp:49
std::vector< string > vec_string
Definition: webserv.hpp:50
std::map< string, JSONNode * > JSONObject
Definition: webserv.hpp:48