diff --git a/includes/Env.hpp b/includes/Env.hpp index 216dae7..e934c44 100644 --- a/includes/Env.hpp +++ b/includes/Env.hpp @@ -5,6 +5,6 @@ class Env { std::vector _servers; public: Env(JSONNode *conf); - void listen(); - void answer(); + void set_fds(); + void refresh(); }; diff --git a/includes/Server.hpp b/includes/Server.hpp index 341f69e..31ec8f5 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -9,6 +9,6 @@ class Server { public: Server(JSONNode *server); ~Server(); - void check(); - void answer(); + void set_fds(); + void refresh(); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index f438907..e81cce2 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -6,18 +6,19 @@ class Socket { int _port; int _master_socket; struct sockaddr_in _address; - int _max_clients; - int _client_socket[30]; + int _clients_amount; + std::vector _clients; public: static fd_set _readfds; - static int _max_sd; - static int _min_sd; + static int _max_fd; + static int _min_fd; static int _amount; Socket(string def); ~Socket(); int launch(); - void check(); - void answer(); + void set_fds(); + void refresh(); + void answer(int fd, string request); /* Socket& operator=(Socket &src) { _ip = src._ip; diff --git a/includes/webserv.hpp b/includes/webserv.hpp index bfc700b..a4cda2e 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -16,9 +16,9 @@ #include #include #include -#include #include #include +#include #include #define DEBUG 0 diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 91a3f80..5bd27c7 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -15,19 +15,16 @@ Env::Env(JSONNode *conf) { } // delete conf; } -void Env::listen() { +void Env::set_fds() { for (std::vector::iterator it = _servers.begin(); it < _servers.end(); it++) { - (*it)->check(); + (*it)->set_fds(); } - cout << "finished env listen\n"; } -void Env::answer() { - cout << "env start answer\n"; +void Env::refresh() { for (std::vector::iterator it = _servers.begin(); it < _servers.end(); it++) { - (*it)->answer(); + (*it)->refresh(); } - cout << "finished env answer\n"; } diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index dae18ed..14f7f1f 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -22,18 +22,16 @@ Server::~Server() { cout << "Server destroyed!\n"; } -void Server::check() { +void Server::set_fds() { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) { - (*it)->check(); + (*it)->set_fds(); } - cout << "finished serv listen\n"; } -void Server::answer() { +void Server::refresh() { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) { - (*it)->answer(); + (*it)->refresh(); } - cout << "finished serv answer\n"; } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index d96a714..2d8dfd5 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -2,20 +2,21 @@ #include "webserv.hpp" Socket::Socket(string def) { - size_t split = def.rfind(':'); + size_t sep_pos = def.rfind(':'); + size_t ip6_endpos = def.rfind(']'); - string tmp = def.substr(0, split); - _ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp; - tmp = def.substr(split + 1, def.length() - split - 1).c_str(); + string tmp = def.substr(0, sep_pos); + if (ip6_endpos > sep_pos) + _ip = def; + else + _ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp; + tmp = def.substr(sep_pos + 1, def.length() - sep_pos - 1).c_str(); _port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); - - _max_clients = 30; - for (int i = 0; i < _max_clients; i++) - _client_socket[i] = 0; + _clients_amount = 0; } Socket::~Socket() { - close(_master_socket); - cout << "Socket destroyed!\n"; + close(_master_socket); + cout << "Socket destroyed!\n"; } int Socket::launch() { @@ -47,43 +48,32 @@ int Socket::launch() { cout << "Listen: " << strerror(errno) << "\n"; return (EXIT_FAILURE); } - cout << "Socket::_master_socket: " << _master_socket << "\n"; - if (_master_socket < _min_sd) - _min_sd = _master_socket; - _amount++; - return (EXIT_SUCCESS); + cout << "Socket::_master_socket: " << _master_socket << "\n"; + if (_master_socket < _min_fd) + _min_fd = _master_socket; + _amount++; + return (EXIT_SUCCESS); } -void Socket::check() { - int sd; - +void Socket::set_fds() { FD_SET(_master_socket, &_readfds); - cout << "sd_set " << _master_socket << "\n"; - for (int i = 0; i < _max_clients; i++) { - sd = _client_socket[i]; - //cout << "id: " << i << " -> sd: " << sd << "\n"; - if (sd > 0) - { - FD_SET(sd, &_readfds); - cout << "fd_set " << sd << "\n"; - } - if (sd > _max_sd) - _max_sd = sd; + for (std::vector::iterator it = _clients.begin(); it < _clients.end(); + it++) { + FD_SET(*it, &_readfds); + if (*it > _max_fd) + _max_fd = *it; } - cout << "Socket checked\n"; } - -void Socket::answer() { - int i, sd, valread; +void Socket::refresh() { + std::vector::iterator it; + int valread; int addrlen = sizeof(_address); char buffer[1024]; - char r404[72] = - "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; if (FD_ISSET(_master_socket, &_readfds)) { int new_socket = accept(_master_socket, (struct sockaddr *)&_address, - (socklen_t *)&addrlen); + (socklen_t *)&addrlen); if (new_socket < 0) { cout << "Accept: " << strerror(errno) << "\n"; exit(EXIT_FAILURE); @@ -94,36 +84,31 @@ void Socket::answer() { cout << "New connection, socket fd is " << new_socket << ", ip is : " << inet_ntoa(_address.sin_addr) << ", port : " << ntohs(_address.sin_port) << "\n"; - for (i = 0; i < _max_clients; i++) { - if (_client_socket[i] == 0) { - _client_socket[i] = new_socket; - cout << "Adding to list of sockets as " << i << "\n"; - break; - } - } + _clients.push_back(new_socket); } - cout << "Socket: " << _ip << ":" << _port << "\n"; - for (i = 0; i < _max_clients; i++) { - sd = _client_socket[i]; - if (FD_ISSET(sd, &_readfds)) { - cout << "Client " << i << ": set\n"; - valread = read(sd, buffer, 1024); + for (it = _clients.begin(); it < _clients.end(); it++) { + if (FD_ISSET(*it, &_readfds)) { + valread = read(*it, buffer, 1024); if (valread == 0) { - getpeername(sd, (struct sockaddr *)&_address, + getpeername(*it, (struct sockaddr *)&_address, (socklen_t *)&addrlen); cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr) << ", port " << ntohs(_address.sin_port) << "\n"; - close(sd); - _client_socket[i] = 0; - } else { - cout << buffer << "\n"; + close(*it); + _clients.erase(it); + } else + this->answer(*it, buffer); + } + } +} + +void Socket::answer(int fd, string request) { + char r404[72] = + "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; + cout << request << "\n"; #ifdef __linux__ - send(sd, r404, strlen(r404), MSG_NOSIGNAL); + send(fd, r404, strlen(r404), MSG_NOSIGNAL); #elif __APPLE__ - send(sd, r404, strlen(r404), 0); + send(fd, r404, strlen(r404), 0); #endif - } - } - } - cout << "Socket answered\n"; } diff --git a/srcs/webserv.cpp b/srcs/webserv.cpp index f40f562..545ca63 100644 --- a/srcs/webserv.cpp +++ b/srcs/webserv.cpp @@ -2,8 +2,8 @@ #include fd_set Socket::_readfds; -int Socket::_max_sd; -int Socket::_min_sd = INT_MAX; +int Socket::_max_fd; +int Socket::_min_fd = INT_MAX; int Socket::_amount = 0; int main(int ac, char **av) { @@ -18,15 +18,13 @@ int main(int ac, char **av) { cout << "Setting environment...\n"; Env env(conf); while (1) { - cout << "Cycling...\n"; FD_ZERO(&Socket::_readfds); - Socket::_max_sd = Socket::_min_sd; - env.listen(); - cout << "Socket::_max_sd " << Socket::_max_sd << "\n"; - int activity = select(Socket::_max_sd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL); + Socket::_max_fd = Socket::_min_fd; + env.set_fds(); + int activity = select(Socket::_max_fd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL); if ((activity < 0) && (errno != EINTR)) cout << "Select: " << strerror(errno) << "\n"; - env.answer(); + env.refresh(); } } return (0);