Browse Source

syntax modifications and comments added

master
Walid Bekkal 2 years ago
parent
commit
b36e5af2bc
  1. 1
      includes/webserv.hpp
  2. 76
      srcs/load/Env.cpp
  3. 44
      srcs/load/Server.cpp
  4. 29
      srcs/sock/Client.cpp
  5. 6
      srcs/webserv.cpp

1
includes/webserv.hpp

@ -29,6 +29,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
using std::cout; using std::cout;
using std::strerror; using std::strerror;
using std::string; using std::string;

76
srcs/load/Env.cpp

@ -1,21 +1,5 @@
#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:
* *
@ -23,33 +7,54 @@ Env::~Env() {
* Output: The env object containing servers and sockets vectors defined inside * Output: The env object containing servers and sockets vectors defined inside
* conf file by servers blocks and listens. * conf file by servers blocks and listens.
*/ */
Env::Env(JSONNode *conf) { Env::Env(JSONNode *conf)
{
try { try {
JSONNode *node; JSONNode *node;
JSONList lst; JSONList lst;
if ((node = conf->obj()["servers"])) if ((node = conf->obj()["servers"]))
{ {
lst = conf->obj()["servers"]->lst(); lst = conf->obj()["servers"]->lst();
for (std::vector< JSONNode * >::iterator it = lst.begin(); for (std::vector< JSONNode * >::iterator it = lst.begin(); it < lst.end(); it++)
it < lst.end(); it++) { {
Server *server = new Server(*it); Server *server = new Server(*it);
_servers.push_back(server); this->_servers.push_back(server);
std::vector< Master * > tmp_s = server->get_sockets(*it); std::vector< Master * > tmp_s = server->get_sockets(*it);
_masters.insert(_masters.end(), tmp_s.begin(), tmp_s.end()); this->_masters.insert(this->_masters.end(), tmp_s.begin(), tmp_s.end());
} }
} }
if ((node = conf->obj()["allowed_methods"])) { if ((node = conf->obj()["allowed_methods"]))
{
JSONList lst = node->lst(); JSONList lst = node->lst();
for (JSONList::iterator it = lst.begin(); it < lst.end(); for (JSONList::iterator it = lst.begin(); it < lst.end(); it++)
it++) { {
_allowed_methods.push_back((*it)->str()); this->_allowed_methods.push_back((*it)->str());
} }
} }
} catch (std::exception &e) { } catch (std::exception &e)
{
std::cerr << e.what() << "\n"; std::cerr << e.what() << "\n";
} }
delete conf; delete conf;
} }
/*|==========|
* Environment destructor:
*
* The destructor call all servers and sockets destructors.
*/
Env::~Env()
{
for (std::vector< Server * >::iterator it = this->_servers.begin();
it < this->_servers.end(); it++) {
delete *it;
}
for (std::vector< Master * >::iterator it = this->_masters.begin();
it < this->_masters.end(); it++) {
delete *it;
}
}
/*|==========| /*|==========|
* One server cycle * One server cycle
* - append sockets to listen to select list * - append sockets to listen to select list
@ -57,14 +62,14 @@ Env::Env(JSONNode *conf) {
* - refresh and handle requests * - refresh and handle requests
*/ */
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;
cout << "==> Check sockets still alive to listen\n"; cout << "==> Check sockets still alive to listen\n";
set_fds(); set_fds();
cout << "|===||===| Waiting some HTTP request... |===||===|\n"; cout << "|===||===| Waiting some HTTP request... |===||===|\n";
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))
std::cerr << "Select: " << strerror(errno) << "\n"; std::cerr << "Select: " << strerror(errno) << "\n";
cout << "==> Handle requests and answers:\n"; cout << "==> Handle requests and answers:\n";
@ -75,9 +80,10 @@ void Env::cycle(void) {
* at. * at.
*/ */
void Env::set_fds(void) { void Env::set_fds(void)
for (std::vector< Master * >::iterator it = _masters.begin(); {
it < _masters.end(); it++) { for (std::vector< Master * >::iterator it = this->_masters.begin(); it < this->_masters.end(); it++)
{
(*it)->set_fds(); (*it)->set_fds();
} }
} }
@ -86,9 +92,9 @@ void Env::set_fds(void) {
* connection, etc..) and parse requests recieved. * connection, etc..) and parse requests recieved.
*/ */
void Env::refresh(void) { void Env::refresh(void)
for (std::vector< Master * >::iterator it = _masters.begin(); {
it < _masters.end(); it++) for (std::vector< Master * >::iterator it = this->_masters.begin(); it < this->_masters.end(); it++)
try { try {
(*it)->refresh(this); (*it)->refresh(this);
} catch (std::exception &e) { } catch (std::exception &e) {

44
srcs/load/Server.cpp

@ -5,9 +5,9 @@
* delete all routes owned by the server; * delete all routes owned by the server;
*/ */
Server::~Server(void) { Server::~Server(void)
for (std::map< string, Route * >::iterator it = _routes.begin(); {
it != _routes.end(); it++) for (std::map< string, Route * >::iterator it = _routes.begin(); it != _routes.end(); it++)
delete (*it).second; delete (*it).second;
cout << "Server destroyed!\n"; cout << "Server destroyed!\n";
} }
@ -20,14 +20,16 @@ Server::~Server(void) {
* autoindex ...) and the Server one the others ones (server_name, sub-routes) * autoindex ...) and the Server one the others ones (server_name, sub-routes)
*/ */
Server::Server(JSONNode *server) : Route(NULL, "/", server) { Server::Server(JSONNode *server) : Route(NULL, "/", server)
{
JSONObject datas = server->obj(); JSONObject datas = server->obj();
if (datas["server_name"]) if (datas["server_name"])
_name = datas["server_name"]->str(); _name = datas["server_name"]->str();
if (datas["locations"]) { if (datas["locations"])
{
JSONObject locations = datas["locations"]->obj(); JSONObject locations = datas["locations"]->obj();
for (JSONObject::iterator it = locations.begin(); it != locations.end(); for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++)
it++) { {
Route *route = new Route(this, (*it).first, (*it).second); Route *route = new Route(this, (*it).first, (*it).second);
_routes[(*it).first] = route; _routes[(*it).first] = route;
} }
@ -43,9 +45,11 @@ string Server::getName(void) { return _name; }
* Output: a Master socket or NULL if creation failed * Output: a Master socket or NULL if creation failed
*/ */
Master *Server::create_master(string str) { Master *Server::create_master(string str)
{
ip_port_t listen = get_ip_port_t(str); ip_port_t listen = get_ip_port_t(str);
if (listen.ip.at(0) != '[') { if (listen.ip.at(0) != '[')
{
try { try {
_listens.push_back(listen); _listens.push_back(listen);
Master *sock = new Master(listen); Master *sock = new Master(listen);
@ -64,16 +68,17 @@ Master *Server::create_master(string str) {
* Output: A vector containing all the succesfull created sockets using * Output: A vector containing all the succesfull created sockets using
* listens from the server block. * listens 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;
Master *tmp; Master *tmp;
ip_port_t listen; ip_port_t listen;
if (datas["listens"]) { if (datas["listens"])
{
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++) { {
if ((tmp = create_master((*it)->str()))) if ((tmp = create_master((*it)->str())))
ret.push_back(tmp); ret.push_back(tmp);
} }
@ -89,15 +94,16 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
* block is adapted. * block is adapted.
*/ */
Route *Server::choose_route(string uri) { Route *Server::choose_route(string uri)
{
vec_string uri_words, loc_words; vec_string uri_words, loc_words;
uri_words = split(uri, "/"); uri_words = split(uri, "/");
for (std::map< string, Route * >::iterator loc_it = _routes.begin(); for (std::map< string, Route * >::iterator loc_it = _routes.begin(); loc_it != _routes.end(); loc_it++)
loc_it != _routes.end(); loc_it++) { {
loc_words = split((*loc_it).first, "/"); loc_words = split((*loc_it).first, "/");
vec_string::iterator loc_word = loc_words.begin(); vec_string::iterator loc_word = loc_words.begin();
for (vec_string::iterator uri_word = uri_words.begin(); for (vec_string::iterator uri_word = uri_words.begin(); uri_word < uri_words.end(); uri_word++)
uri_word < uri_words.end(); uri_word++) { {
while (uri_word != uri_words.end() && *uri_word == "") while (uri_word != uri_words.end() && *uri_word == "")
uri_word++; uri_word++;
if (*uri_word != *(loc_word++)) if (*uri_word != *(loc_word++))

29
srcs/sock/Client.cpp

@ -2,26 +2,23 @@
inline string get_extension(string str) { return str.substr(str.rfind('.')); } inline string get_extension(string str) { return str.substr(str.rfind('.')); }
Client::Client(int fd, ip_port_t ip_port, Master *parent) Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip_port), _parent(parent) {
: _fd(fd), _ip_port(ip_port), _parent(parent) {
clean(); clean();
cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip << ", port : " << _ip_port.port << "\n";
<< ", port : " << _ip_port.port << "\n";
} }
Client::~Client(void) { Client::~Client(void) {
close(_fd); close(_fd);
cout << "Host disconnected, ip " << _ip_port.ip << ", port " cout << "Host disconnected, ip " << this->_ip_port.ip << ", port " << this->_ip_port.port << "\n";
<< _ip_port.port << "\n";
} }
void Client::clean(void) { void Client::clean(void) {
_server = NULL; this->_server = NULL;
_route = NULL; this->_route = NULL;
_method = _uri = _host = _header = _body = ""; this->_method = this->_uri = this->_host = this->_header = this->_body = "";
_len = 0; this->_len = 0;
_last_chunk = false; this->_last_chunk = false;
_request.clear(); this->_request.clear();
} }
bool Client::getHeader(Env *env, string paquet) { bool Client::getHeader(Env *env, string paquet) {
@ -238,11 +235,13 @@ void Client::send_error(int error_code) {
} }
void Client::send_answer(string msg) { void Client::send_answer(string msg) {
#ifdef __linux__ #ifdef DEBUG
print_block("ANSWER: ", msg); print_block("ANSWER: ", msg);
send(_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL); #endif
#ifdef __linux__
send(this->_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__
send(_fd, msg.c_str(), msg.length(), 0); send(this->_fd, msg.c_str(), msg.length(), 0);
#endif #endif
clean(); clean();
} }

6
srcs/webserv.cpp

@ -7,22 +7,20 @@ int Master::_amount = 0;
int main(int ac, char **av) { int main(int ac, char **av) {
try { try {
// Parse config file
if (ac > 2) if (ac > 2)
throw std::runtime_error("Too many arguments"); throw std::runtime_error("Too many arguments");
std::string config_file = "default.json"; std::string config_file = "default.json";
if (ac == 2) if (ac == 2)
config_file = av[1]; config_file = av[1];
std::ifstream file(config_file.c_str()); std::ifstream file(config_file.c_str());
if (!file.good()) if (!file.good())
throw std::runtime_error("File not found"); throw std::runtime_error("File not found");
cout << "Parsing configuration file from JSON conf file.\n"; cout << "Parsing configuration file from JSON conf file.\n";
cout << "You must be sure the syntax is correct\n"; cout << "You must be sure the syntax is correct\n";
JSONParser parser(config_file); JSONParser parser(config_file);
JSONNode *conf = parser.parse(); JSONNode *conf = parser.parse();
// Here we start the server and his environment using conf
cout << "Initialization of server...\n"; cout << "Initialization of server...\n";
Env env(conf); Env env(conf);
while (1) while (1)

Loading…
Cancel
Save