Browse Source

correct server pick I guess

master
nicolas-arnaud 2 years ago
parent
commit
bbb2c281bc
  1. 1
      default.json
  2. 4
      includes/Env.hpp
  3. 3
      includes/Server.hpp
  4. 6
      includes/Socket.hpp
  5. 50
      srcs/load/Env.cpp
  6. 15
      srcs/load/Route.cpp
  7. 6
      srcs/load/Server.cpp
  8. 6
      srcs/load/Socket.cpp
  9. 5
      srcs/tools.cpp
  10. 4
      srcs/webserv.cpp

1
default.json

@ -24,7 +24,6 @@
"root": "images/", "root": "images/",
"autoindex": true "autoindex": true
} }
} }
} }
] ]

4
includes/Env.hpp

@ -7,7 +7,7 @@ class Env {
public: public:
Env(JSONNode *conf); Env(JSONNode *conf);
void set_fds(); void set_fds(void);
void refresh(); void refresh(void);
Server *choose_server(Socket *sock, string host); Server *choose_server(Socket *sock, string host);
}; };

3
includes/Server.hpp

@ -7,7 +7,8 @@ class Server : public Route {
public: public:
std::vector<listen_t> _listens; std::vector<listen_t> _listens;
Server(JSONNode *server); Server(JSONNode *server);
~Server(); ~Server(void);
std::vector<Socket *> get_sockets(JSONNode *server); std::vector<Socket *> get_sockets(JSONNode *server);
Route *get_route(string uri); Route *get_route(string uri);
string getName(void);
}; };

6
includes/Socket.hpp

@ -14,9 +14,9 @@ class Socket {
static int _min_fd; static int _min_fd;
static int _amount; static int _amount;
Socket(listen_t listen); Socket(listen_t listen);
~Socket(); ~Socket(void);
int launch(); int launch(void);
void set_fds(); void set_fds(void);
void refresh(Env *env); void refresh(Env *env);
void answer(Env *env, int fd, string request); void answer(Env *env, int fd, string request);
void send_answer(int fd, string msg); void send_answer(int fd, string msg);

50
srcs/load/Env.cpp

@ -25,42 +25,60 @@ Env::Env(JSONNode *conf) {
Server *Env::choose_server(Socket *sock, string host) { Server *Env::choose_server(Socket *sock, string host) {
std::vector<Server *> exact; std::vector<Server *> exact;
std::vector<Server *> inrange; std::vector<Server *> inrange;
string ip = inet_ntoa(sock->_address.sin_addr); std::vector<string> ip_list;
int port = ntohs(sock->_address.sin_port); std::vector<string> ip_requ;
// string ip = inet_ntoa(sock->_address.sin_addr);
// int port = ntohs(sock->_address.sin_port);
cout << "Which server for " << ip << ":" << port << "?\n"; // cout << "Which server for " << ip << ":" << port << "?\n";
cout << "Socket: " << sock->_listen.ip << ":" << sock->_listen.port << "\n"; cout << "Socket: " << sock->_listen.ip << ":" << sock->_listen.port << "\n";
(void)host; ip_requ = split(sock->_listen.ip, '.');
for (std::vector<Server *>::iterator sit = _servers.begin(); for (std::vector<Server *>::iterator sit = _servers.begin();
sit < _servers.end(); sit++) { sit < _servers.end(); sit++) {
std::vector<listen_t> serv_listens = (*sit)->_listens; std::vector<listen_t> serv_listens = (*sit)->_listens;
for (std::vector<listen_t>::iterator it = serv_listens.begin(); for (std::vector<listen_t>::iterator it = serv_listens.begin();
it < serv_listens.end(); it++) { it < serv_listens.end(); it++) {
ip_list = split((*it).ip, '.');
if (sock->_listen.port != (*it).port) if (sock->_listen.port != (*it).port)
continue; continue;
if (sock->_listen.ip == (*it).ip) if (sock->_listen.ip == (*it).ip) {
exact.push_back(*sit); exact.push_back(*sit);
else if (sock->_listen.ip == (*it).ip) continue;
}
bool is_inrange = true;
ip_list = split((*it).ip, '.');
std::vector<string>::iterator r = ip_requ.begin();
for (std::vector<string>::iterator l = ip_list.begin();
l < ip_list.end(); l++) {
if (*l != *r && *l != "0")
is_inrange = false;
}
if (is_inrange == true)
inrange.push_back(*sit); inrange.push_back(*sit);
// else if (is_ip_into(sock->_listen.ip, (*it).ip)) // else if (is_ip_into(sock->_listen.ip, (*it).ip))
} }
} }
if (exact.at(0)) if (exact.size() == 0) {
return (exact.at(0)); for (std::vector<Server *>::iterator sit = inrange.begin();
else sit < inrange.end(); sit++) {
return (inrange.at(0)); if (host == (*sit)->getName())
return *sit;
}
return inrange.front();
} else
return exact.front();
} }
void Env::set_fds() { void Env::set_fds(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++)
(*it)->set_fds(); (*it)->set_fds();
}
} }
void Env::refresh() { void Env::refresh(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++)
(*it)->refresh(this); (*it)->refresh(this);
}
} }

15
srcs/load/Route.cpp

@ -11,8 +11,7 @@ Route::Route(string location, JSONNode *datas) : _location(location) {
_autoindex = tmp->boo(); _autoindex = tmp->boo();
if ((tmp = object["indexs"])) { if ((tmp = object["indexs"])) {
JSONList indexs = tmp->lst(); JSONList indexs = tmp->lst();
for (JSONList::iterator it = indexs.begin(); for (JSONList::iterator it = indexs.begin(); it < indexs.end(); it++) {
it < indexs.end(); it++) {
_indexs.push_back((*it)->str()); _indexs.push_back((*it)->str());
} }
} }
@ -20,7 +19,7 @@ Route::Route(string location, JSONNode *datas) : _location(location) {
Route::~Route(void) {} Route::~Route(void) {}
string Route::getLocation(void) {return _location; } string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; } string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; } string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexs(void) { return _indexs; } std::vector<string> Route::getIndexs(void) { return _indexs; }
@ -55,14 +54,13 @@ string Route::getAutoindex(string uri) {
string Route::correctUri(string uri) { string Route::correctUri(string uri) {
std::stringstream ret; std::stringstream ret;
//int slash_pos; // int slash_pos;
//string root = _root; // string root = _root;
//int i = 0; // int i = 0;
std::vector<string>::iterator it; std::vector<string>::iterator it;
std::vector<string>::iterator it2; std::vector<string>::iterator it2;
cout << "Correcting request: " << uri cout << "Correcting request: " << uri << " with root: " << _root << "\n";
<< " with root: " << _root << "\n";
ret << _root; ret << _root;
std::vector<string> loc_split = split(_location, '/'); std::vector<string> loc_split = split(_location, '/');
std::vector<string> uri_split = split(uri, '/'); std::vector<string> uri_split = split(uri, '/');
@ -94,4 +92,3 @@ string Route::correctUri(string uri) {
cout << "resutlt: " << ret.str() << "\n"; cout << "resutlt: " << ret.str() << "\n";
return ret.str(); return ret.str();
} }

6
srcs/load/Server.cpp

@ -14,6 +14,10 @@ Server::Server(JSONNode *server) : Route("/", server) {
} }
} }
Server::~Server(void) { cout << "Server destroyed!\n"; }
string Server::getName(void) { return _name; }
std::vector<Socket *> Server::get_sockets(JSONNode *server) { std::vector<Socket *> Server::get_sockets(JSONNode *server) {
JSONObject datas = server->obj(); JSONObject datas = server->obj();
std::vector<Socket *> ret; std::vector<Socket *> ret;
@ -46,8 +50,6 @@ std::vector<Socket *> Server::get_sockets(JSONNode *server) {
return ret; return ret;
} }
Server::~Server(void) { cout << "Server destroyed!\n"; }
Route *Server::get_route(string uri) { Route *Server::get_route(string uri) {
cout << uri << "\n"; cout << uri << "\n";
std::vector<string> req = split(uri, '/'); std::vector<string> req = split(uri, '/');

6
srcs/load/Socket.cpp

@ -4,12 +4,12 @@
Socket::Socket(listen_t listen) : _listen(listen) { Socket::Socket(listen_t listen) : _listen(listen) {
_clients_amount = 0; _clients_amount = 0;
} }
Socket::~Socket() { Socket::~Socket(void) {
close(_master_socket); close(_master_socket);
cout << "Socket destroyed!\n"; cout << "Socket destroyed!\n";
} }
int Socket::launch() { int Socket::launch(void) {
int opt = 1; int opt = 1;
string ip = _listen.ip; string ip = _listen.ip;
int port = _listen.port; int port = _listen.port;
@ -48,7 +48,7 @@ int Socket::launch() {
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
void Socket::set_fds() { void Socket::set_fds(void) {
FD_SET(_master_socket, &_readfds); FD_SET(_master_socket, &_readfds);
for (std::vector<int>::iterator it = _clients.begin(); it < _clients.end(); for (std::vector<int>::iterator it = _clients.begin(); it < _clients.end();

5
srcs/tools.cpp

@ -22,16 +22,17 @@ std::vector<string> split(string str, char delim) {
std::vector<std::string> tokens; std::vector<std::string> tokens;
std::string token; std::string token;
std::stringstream ss(str); std::stringstream ss(str);
while (getline(ss, token, delim)){ while (getline(ss, token, delim)) {
tokens.push_back(token); tokens.push_back(token);
} }
return tokens; return tokens;
} }
listen_t get_listen_t(string listen) { listen_t get_listen_t(string listen) {
listen_t ret; listen_t ret;
size_t sep_pos = listen.rfind(':'); size_t sep_pos = listen.rfind(':');
string tmp = listen.substr(0, sep_pos); string tmp = listen.substr(0, sep_pos);
ret.ip = isInt(tmp) ? "0.0.0.0" : (tmp == "localhost" ? "127.0.0.1" : tmp); ret.ip = isInt(tmp) ? "0.0.0.0" : (tmp == "localhost" ? "127.0.0.1" : tmp);
tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str(); tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str();
ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());

4
srcs/webserv.cpp

@ -16,11 +16,13 @@ int main(int ac, char **av) {
cout << "Setting environment...\n"; cout << "Setting environment...\n";
Env env(conf); Env env(conf);
cout << "Environement setup.\n";
while (1) { while (1) {
cout << "|===|===|===| CYCLE |===|===|===|\n";
FD_ZERO(&Socket::_readfds); FD_ZERO(&Socket::_readfds);
Socket::_max_fd = Socket::_min_fd; Socket::_max_fd = Socket::_min_fd;
env.set_fds(); env.set_fds();
cout << "|===|===|===| SELECT |===|===|===|\n";
int activity = select(Socket::_max_fd + Socket::_amount, int activity = select(Socket::_max_fd + Socket::_amount,
&(Socket::_readfds), NULL, NULL, NULL); &(Socket::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) if ((activity < 0) && (errno != EINTR))

Loading…
Cancel
Save