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 <vector>
using std::cout;
using std::strerror;
using std::string;

76
srcs/load/Env.cpp

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

44
srcs/load/Server.cpp

@ -5,9 +5,9 @@
* delete all routes owned by the server;
*/
Server::~Server(void) {
for (std::map< string, Route * >::iterator it = _routes.begin();
it != _routes.end(); it++)
Server::~Server(void)
{
for (std::map< string, Route * >::iterator it = _routes.begin(); it != _routes.end(); it++)
delete (*it).second;
cout << "Server destroyed!\n";
}
@ -20,14 +20,16 @@ Server::~Server(void) {
* 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();
if (datas["server_name"])
_name = datas["server_name"]->str();
if (datas["locations"]) {
if (datas["locations"])
{
JSONObject locations = datas["locations"]->obj();
for (JSONObject::iterator it = locations.begin(); it != locations.end();
it++) {
for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++)
{
Route *route = new Route(this, (*it).first, (*it).second);
_routes[(*it).first] = route;
}
@ -43,9 +45,11 @@ string Server::getName(void) { return _name; }
* 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);
if (listen.ip.at(0) != '[') {
if (listen.ip.at(0) != '[')
{
try {
_listens.push_back(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
* 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();
std::vector< Master * > ret;
Master *tmp;
ip_port_t listen;
if (datas["listens"]) {
if (datas["listens"])
{
JSONList listens = datas["listens"]->lst();
for (JSONList::iterator it = listens.begin(); it != listens.end();
it++) {
for (JSONList::iterator it = listens.begin(); it != listens.end(); it++)
{
if ((tmp = create_master((*it)->str())))
ret.push_back(tmp);
}
@ -89,15 +94,16 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
* block is adapted.
*/
Route *Server::choose_route(string uri) {
Route *Server::choose_route(string uri)
{
vec_string uri_words, loc_words;
uri_words = split(uri, "/");
for (std::map< string, Route * >::iterator loc_it = _routes.begin();
loc_it != _routes.end(); loc_it++) {
for (std::map< string, Route * >::iterator loc_it = _routes.begin(); loc_it != _routes.end(); loc_it++)
{
loc_words = split((*loc_it).first, "/");
vec_string::iterator loc_word = loc_words.begin();
for (vec_string::iterator uri_word = uri_words.begin();
uri_word < uri_words.end(); uri_word++) {
for (vec_string::iterator uri_word = uri_words.begin(); uri_word < uri_words.end(); uri_word++)
{
while (uri_word != uri_words.end() && *uri_word == "")
uri_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('.')); }
Client::Client(int fd, ip_port_t ip_port, Master *parent)
: _fd(fd), _ip_port(ip_port), _parent(parent) {
Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip_port), _parent(parent) {
clean();
cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip
<< ", port : " << _ip_port.port << "\n";
cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip << ", port : " << _ip_port.port << "\n";
}
Client::~Client(void) {
close(_fd);
cout << "Host disconnected, ip " << _ip_port.ip << ", port "
<< _ip_port.port << "\n";
cout << "Host disconnected, ip " << this->_ip_port.ip << ", port " << this->_ip_port.port << "\n";
}
void Client::clean(void) {
_server = NULL;
_route = NULL;
_method = _uri = _host = _header = _body = "";
_len = 0;
_last_chunk = false;
_request.clear();
this->_server = NULL;
this->_route = NULL;
this->_method = this->_uri = this->_host = this->_header = this->_body = "";
this->_len = 0;
this->_last_chunk = false;
this->_request.clear();
}
bool Client::getHeader(Env *env, string paquet) {
@ -238,11 +235,13 @@ void Client::send_error(int error_code) {
}
void Client::send_answer(string msg) {
#ifdef __linux__
#ifdef DEBUG
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__
send(_fd, msg.c_str(), msg.length(), 0);
send(this->_fd, msg.c_str(), msg.length(), 0);
#endif
clean();
}

6
srcs/webserv.cpp

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

Loading…
Cancel
Save