Browse Source

some more simplifications

master
nicolas-arnaud 2 years ago
parent
commit
e0563436c4
  1. 3
      includes/Client.hpp
  2. 1
      includes/Server.hpp
  3. 3
      includes/webserv.hpp
  4. 44
      srcs/load/Env.cpp
  5. 3
      srcs/load/Route.cpp
  6. 71
      srcs/load/Server.cpp
  7. 11
      srcs/sock/Client.cpp
  8. 29
      srcs/sock/Master.cpp
  9. 14
      srcs/tools.cpp

3
includes/Client.hpp

@ -5,13 +5,14 @@ typedef std::map< string, std::vector< string > > request_t;
class Client { class Client {
int _fd; int _fd;
listen_t _listen;
Master *_parent; Master *_parent;
string _header; string _header;
string _content; string _content;
request_t _request; request_t _request;
public: public:
Client(int fd, Master *parent); Client(int fd, listen_t listen, Master *parent);
~Client(void); ~Client(void);
bool getRequest(string paquet); bool getRequest(string paquet);
bool parseHeader(); bool parseHeader();

1
includes/Server.hpp

@ -9,6 +9,7 @@ class Server : public Route {
std::vector< listen_t > _listens; std::vector< listen_t > _listens;
Server(JSONNode *server); Server(JSONNode *server);
~Server(void); ~Server(void);
Master *create_master(string str);
std::vector< Master * > get_sockets(JSONNode *server); std::vector< Master * > get_sockets(JSONNode *server);
Route *choose_route(string uri); Route *choose_route(string uri);
string getName(void); string getName(void);

3
includes/webserv.hpp

@ -34,6 +34,7 @@ using std::strerror;
using std::string; using std::string;
typedef struct listen_s { typedef struct listen_s {
int fd;
string ip; string ip;
int port; int port;
} listen_t; } listen_t;
@ -52,8 +53,8 @@ void *ft_memset(void *b, int c, size_t len);
bool isInt(string str); bool isInt(string str);
std::vector< string > split(string str, char delim); std::vector< string > split(string str, char delim);
listen_t get_listen_t(string listen); listen_t get_listen_t(string listen);
listen_t get_listen_t(string ip, int port);
string getMime(string path); string getMime(string path);
string get_extension(string str);
string read_file(string path); string read_file(string path);
#include "Client.hpp" #include "Client.hpp"

44
srcs/load/Env.cpp

@ -1,5 +1,21 @@
#include "webserv.hpp" #include "webserv.hpp"
/*|=======================|
* Environment destructor:
*
* The destructor call all servers and sockets destructors.
*/
Env::~Env() {
for (std::vector< Server * >::iterator it = _servers.begin();
it < _servers.end(); it++) {
delete *it;
}
for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++) {
delete *it;
}
}
/*|=======================| /*|=======================|
* Environment constructor: * Environment constructor:
* *
@ -18,10 +34,11 @@ Env::Env(JSONNode *conf) {
_masters.insert(_masters.end(), tmp_s.begin(), tmp_s.end()); _masters.insert(_masters.end(), tmp_s.begin(), tmp_s.end());
} }
} catch (std::exception &e) { } catch (std::exception &e) {
cout << e.what(); std::cerr << e.what() << "\n";
} }
delete conf; delete conf;
} }
void Env::cycle(void) { void Env::cycle(void) {
FD_ZERO(&Master::_readfds); FD_ZERO(&Master::_readfds);
Master::_max_fd = Master::_min_fd; Master::_max_fd = Master::_min_fd;
@ -31,10 +48,11 @@ void Env::cycle(void) {
int activity = select(Master::_max_fd + Master::_amount, int activity = select(Master::_max_fd + Master::_amount,
&(Master::_readfds), NULL, NULL, NULL); &(Master::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) if ((activity < 0) && (errno != EINTR))
cout << "Select: " << strerror(errno) << "\n"; std::cerr << "Select: " << strerror(errno) << "\n";
cout << "==> Handle requests and answers:\n"; cout << "==> Handle requests and answers:\n";
refresh(); refresh();
} }
/*|=======================| /*|=======================|
* Append each master_sockets and their clients to list of fds SELECT must look * Append each master_sockets and their clients to list of fds SELECT must look
* at. * at.
@ -54,21 +72,9 @@ void Env::set_fds(void) {
void Env::refresh(void) { void Env::refresh(void) {
for (std::vector< Master * >::iterator it = _masters.begin(); for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++) it < _masters.end(); it++)
(*it)->refresh(this); try {
} (*it)->refresh(this);
} catch (std::exception &e) {
/*|=======================| std::cerr << e.what();
* Environment destructor: }
*
* The destructor call all servers and sockets destructors.
*/
Env::~Env() {
for (std::vector< Server * >::iterator it = _servers.begin();
it < _servers.end(); it++) {
delete *it;
}
for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++) {
delete *it;
}
} }

3
srcs/load/Route.cpp

@ -79,7 +79,6 @@ string Route::correctUri(string uri) {
std::vector< string >::iterator it; std::vector< string >::iterator it;
std::vector< string >::iterator it2; std::vector< string >::iterator it2;
cout << "Correcting request: " << uri << " 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, '/');
@ -96,7 +95,5 @@ string Route::correctUri(string uri) {
while (it2 < uri_split.end()) { while (it2 < uri_split.end()) {
ret << "/" << *(it2++); ret << "/" << *(it2++);
} }
cout << "result: " << ret.str() << "\n";
return ret.str(); return ret.str();
} }

71
srcs/load/Server.cpp

@ -1,5 +1,18 @@
#include "webserv.hpp" #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: * Server constructor:
* *
@ -25,6 +38,21 @@ Server::Server(JSONNode *server) : Route(NULL, "/", server) {
/* Get the server name (_server_name)*/ /* Get the server name (_server_name)*/
string Server::getName(void) { return _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: * 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 * Output: A vector containing all the succesfull created sockets using listens
* from the server block. * from the server block.
*/ */
std::vector< Master * > Server::get_sockets(JSONNode *server) { std::vector< Master * > Server::get_sockets(JSONNode *server) {
JSONObject datas = server->obj(); JSONObject datas = server->obj();
std::vector< Master * > ret; std::vector< Master * > ret;
@ -40,31 +69,10 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
JSONList listens = datas["listens"]->lst(); JSONList listens = datas["listens"]->lst();
for (JSONList::iterator it = listens.begin(); it != listens.end(); for (JSONList::iterator it = listens.begin(); it != listens.end();
it++) { it++) {
listen = get_listen_t((*it)->str()); ret.push_back(create_master((*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';
} }
} } else
ret.push_back(create_master("localhost:8080"));
return ret; return ret;
} }
@ -76,7 +84,6 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
* adapted. * adapted.
*/ */
Route *Server::choose_route(string uri) { Route *Server::choose_route(string uri) {
// cout << uri << "\n";
std::vector< string > req = split(uri, '/'); std::vector< string > req = split(uri, '/');
std::vector< string > root; std::vector< string > root;
for (std::map< string, Route * >::iterator rit = _routes.begin(); for (std::map< string, Route * >::iterator rit = _routes.begin();
@ -88,7 +95,6 @@ Route *Server::choose_route(string uri) {
it++) { it++) {
if (*it == "") if (*it == "")
continue; continue;
cout << *it << " - " << *root_it << "\n";
if (*it != *(root_it++)) if (*it != *(root_it++))
break; break;
if (root_it == root.end()) if (root_it == root.end())
@ -97,16 +103,3 @@ Route *Server::choose_route(string uri) {
} }
return this; 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";
}

11
srcs/sock/Client.cpp

@ -1,9 +1,14 @@
#include "webserv.hpp" #include "webserv.hpp"
Client::Client(int fd, Master *parent) : _fd(fd), _parent(parent) {} Client::Client(int fd, listen_t listen, Master *parent)
: _fd(fd), _listen(listen), _parent(parent) {
cout << "New connection, socket fd is " << fd << ", ip is : " << _listen.ip
<< ", port : " << _listen.port << "\n";
}
Client::~Client(void) { Client::~Client(void) {
close(_fd); close(_fd);
cout << "Destroyed client socket\n"; cout << "Host disconnected, ip " << _listen.ip << ", port " << _listen.port
<< "\n";
} }
bool Client::getRequest(string paquet) { bool Client::getRequest(string paquet) {
@ -87,6 +92,8 @@ string Client::header_pick(string key, int id) {
return ret; return ret;
} }
inline string get_extension(string str) { return str.substr(str.rfind('.')); }
void Client::answer(Env *env) { void Client::answer(Env *env) {
string method = header_pick("Method:", 0); string method = header_pick("Method:", 0);
string uri = header_pick("Method:", 1); string uri = header_pick("Method:", 1);

29
srcs/sock/Master.cpp

@ -25,11 +25,11 @@ Master::Master(listen_t list) : _listen(list) {
if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0)
throw std::runtime_error("bind() error: " + string(strerror(errno))); throw std::runtime_error("bind() error: " + string(strerror(errno)));
cout << "Listener " << ip << " on port " << port << "\n";
if (listen(_fd, 3) < 0) if (listen(_fd, 3) < 0)
throw std::runtime_error("listen() error: " + string(strerror(errno))); throw std::runtime_error("listen() error: " + string(strerror(errno)));
cout << "Master: " << _fd << "\n"; cout << "New master socket with fd " << _fd << " which listen " << ip << ":"
<< port << "\n";
if (_fd < _min_fd) if (_fd < _min_fd)
_min_fd = _fd; _min_fd = _fd;
_amount++; _amount++;
@ -55,18 +55,15 @@ void Master::refresh(Env *env) {
if (FD_ISSET(_fd, &_readfds)) { if (FD_ISSET(_fd, &_readfds)) {
int new_socket = int new_socket =
accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen); accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen);
if (new_socket < 0) { if (new_socket < 0)
cout << "Accept: " << strerror(errno) << "\n"; throw std::runtime_error("accept() error:" +
exit(EXIT_FAILURE); string(strerror(errno)));
}
#ifdef __APPLE__ #ifdef __APPLE__
// fcntl(new_socket, F_GETNOSIGPIPE);
fcntl(new_socket, F_SETFL, O_NONBLOCK); fcntl(new_socket, F_SETFL, O_NONBLOCK);
#endif #endif
cout << "New connection, socket fd is " << new_socket listen_t cli_listen = get_listen_t(inet_ntoa(_address.sin_addr),
<< ", ip is : " << inet_ntoa(_address.sin_addr) ntohs(_address.sin_port));
<< ", port : " << ntohs(_address.sin_port) << "\n"; _childs.push_back(new Client(new_socket, cli_listen, this));
_childs.push_back(new Client(new_socket, this));
} }
int child_fd; int child_fd;
for (std::vector< Client * >::iterator it = _childs.begin(); for (std::vector< Client * >::iterator it = _childs.begin();
@ -74,18 +71,14 @@ void Master::refresh(Env *env) {
child_fd = (*it)->_fd; child_fd = (*it)->_fd;
if (FD_ISSET(child_fd, &_readfds)) { if (FD_ISSET(child_fd, &_readfds)) {
valread = read(child_fd, buffer, 10000); valread = read(child_fd, buffer, 10000);
buffer[valread] = '\0';
if (valread == 0) { if (valread == 0) {
getpeername(child_fd, (struct sockaddr *)&_address, getpeername(child_fd, (struct sockaddr *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr)
<< ", port " << ntohs(_address.sin_port) << "\n";
delete (*it); delete (*it);
_childs.erase(it); _childs.erase(it);
} else { } else if ((*it)->getRequest(buffer))
buffer[valread] = '\0'; (*it)->answer(env);
if ((*it)->getRequest(buffer))
(*it)->answer(env);
}
} }
} }
} }

14
srcs/tools.cpp

@ -39,6 +39,13 @@ listen_t get_listen_t(string listen) {
return ret; return ret;
} }
listen_t get_listen_t(string ip, int port) {
listen_t ret;
ret.ip = ip;
ret.port = port;
return ret;
}
string getMime(string path) { string getMime(string path) {
size_t pos = path.rfind('.'); size_t pos = path.rfind('.');
string extension = (pos == string::npos) ? "txt" : path.substr(pos + 1); string extension = (pos == string::npos) ? "txt" : path.substr(pos + 1);
@ -210,13 +217,6 @@ string getMime(string path) {
return ("text/plain"); return ("text/plain");
} }
string get_extension(string str) {
int dot_pos = str.rfind('.');
string ret = str.substr(dot_pos);
cout << ret << "\n";
return ret;
}
string read_file(string path) { string read_file(string path) {
string str; string str;
string content; string content;

Loading…
Cancel
Save