Merge remote-tracking branch 'origin/fara' into roman

This commit is contained in:
3lswear
2022-02-19 22:31:32 +03:00
29 changed files with 690 additions and 265 deletions

View File

@@ -3,7 +3,6 @@
#include <cassert>
#include <cstdio>
#include <map>
// #include "parse.hpp"
#define THREAD_NUM 100
#define MAX_EVENTS
@@ -40,22 +39,21 @@ void Server::readConfig(char *filename)
// TOMLMap *root = NULL;
// root = parse(filename);
config::TOMLParser parser(filename);
try
{
parser.parse();
_root = parser.root;
DBOUT << RED << "PARSED !!!" << ENDL;
}
catch (std::domain_error &e)
{
std::cerr << RED << "FATAL: ";
std::cerr << e.what() << RESET << std::endl;
// root->clear();
config::clean_parsed(parser.root);
// delete parser.root;
// exit(-1);
return;
}
// catch (std::domain_error &e)
// {
// std::cerr << RED << "FATAL: ";
// std::cerr << e.what() << RESET << std::endl;
// // root->clear();
// // config::clean_parsed(parser.root);
// // delete parser.root;
// // exit(-1);
// return;
//
// }
// catch (config::Tokenizer::InvalidToken &e)
// {
// std::cerr << RED << "FATAL: ";
@@ -81,11 +79,9 @@ void Server::readConfig(char *filename)
++it;
}
config::clean_parsed(parser.root);
DBOUT << RED << "GONNA CLEAN_PARSED" << ENDL;
}
void Server::sendData(Client &client, int fd)
{
/* std::string tmp = client.getStrToSend(); */
@@ -141,6 +137,7 @@ void Server::readSocket(Client &client, int fd)
// client.setRawData(buf);
client.increaseRecvCounter(bytes_read);
status = client.parseRequest();
(void)status;
// client_map[fd].printClientInfo();
if (client.allRecved())
@@ -197,6 +194,12 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
<< ENDL;
}
void sigHandler(int sig)
{
if (sig == SIGINT)
throw ConfigException("SIGINT called. Server shutdown!");
}
void Server::start(void)
{
/* Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1"); */
@@ -207,7 +210,8 @@ void Server::start(void)
unsigned int client_events = EPOLLIN;
unsigned int server_events = EPOLLIN;
std::signal(SIGINT, sigHandler);
_epoll_fd = epoll_create(1337);
@@ -254,7 +258,7 @@ void Server::start(void)
{
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000);
DBOUT << TURQ << "ready_num " << ready_num << ENDL;
// DBOUT << TURQ << "ready_num " << ready_num << ENDL;
if (ready_num < 0)
throw std::logic_error("epoll_ret");
@@ -323,6 +327,23 @@ void Server::start(void)
void Server::end(void)
{
}
//----------------------------------------------Other------------------------------------------------------------------------------------------------
void Server::checkError(int fd, std::string str)
{
if (fd < 0)
{
DBOUT << RED << "Server ERROR: " << str << ENDL;
exit(1);
}
else
DBOUT << GREEN << "Server SUCCESS: " << str << ENDL;
}
Server::~Server()
{
std::vector<ServerConfig *>::iterator pri;
std::vector<location *>::iterator loc;
@@ -342,23 +363,8 @@ void Server::end(void)
delete *pri;
pri++;
}
}
//----------------------------------------------Other------------------------------------------------------------------------------------------------
void Server::checkError(int fd, std::string str)
{
if (fd < 0)
{
DBOUT << RED << "Server ERROR: " << str << ENDL;
exit(1);
}
else
DBOUT << GREEN << "Server SUCCESS: " << str << ENDL;
}
Server::~Server()
{
config::clean_parsed(_root);
}

View File

@@ -15,6 +15,8 @@ class Server
private:
int _port;
int _epoll_fd;
TOMLMap *_root;
struct epoll_event _events[MAX_CLIENT];
struct sockaddr_in _addres;
std::string _ip;

View File

@@ -33,7 +33,7 @@ int &ServerConfig::getPort(void)
return (_port);
}
int &ServerConfig::getClientBodySize(void)
ssize_t &ServerConfig::getClientBodySize(void)
{
return (_clientBodySize);
}
@@ -90,18 +90,42 @@ void ServerConfig::setRoot(TOMLMap * data)
//--------------------------------------------------Parse-Config---------------------------------------
std::string ServerConfig::getTypestr(toml_node::e_type type)
{
if (type == toml_node::NUM)
return ("NUM");
else if (type == toml_node::ARRAY)
return ("ARRAY");
else if (type == toml_node::BOOL)
return ("BOOL");
else if (type == toml_node::STRING)
return ("STRING");
else if (type == toml_node::MAP)
return ("MAP");
else if (type == toml_node::MAPARRAY)
return ("MAPARRAY");
else
return ("UNKNOWN");
}
std::string ServerConfig::getWrongTypeErrorMSG(std::string field, toml_node::e_type expected, toml_node::e_type received)
{
std::string out = "Wrong type specified in " + field + " field, expected "
+ getTypestr(expected) + ", but received " + getTypestr(received) + "!";
return (out);
}
int ServerConfig::putBodySizeLimit(toml_node *node)
{
if (node->get_type() != toml_node::NUM)
return (1);
throw ConfigException(getWrongTypeErrorMSG("body_size_limit", toml_node::NUM, node->get_type()));
_clientBodySize = node->getNum();
if (_clientBodySize < 0)
throw ConfigException("Invalid body_size_limit specified!");
return (0);
}
int ServerConfig::putErrorPage(toml_node *node)
{
if (node->get_type() != toml_node::MAP)
return (1);
throw ConfigException(getWrongTypeErrorMSG("error_page", toml_node::MAP, node->get_type()));
TOMLMap *map = node->getMap();
TOMLMap::iterator it;
std::string s;
@@ -110,7 +134,7 @@ int ServerConfig::putErrorPage(toml_node *node)
while (it != map->end())
{
if (it->second->get_type() != toml_node::STRING)
return (1);
throw ConfigException(getWrongTypeErrorMSG("error_page path", toml_node::STRING, node->get_type()));
s = it->first;
_errorPages[atoi(s.c_str())] = *it->second->getString();
++it;
@@ -120,22 +144,27 @@ int ServerConfig::putErrorPage(toml_node *node)
int ServerConfig::putHost(toml_node *node)
{
if (node->get_type() != toml_node::STRING)
return (1);
throw ConfigException(getWrongTypeErrorMSG("host", toml_node::STRING, node->get_type()));
_host = *node->getString();
int ret = inet_addr(_host.c_str());
if (ret == -1)
throw ConfigException("Invalid host specified!");
return (0);
}
int ServerConfig::putName(toml_node *node)
{
if (node->get_type() != toml_node::STRING)
return (1);
throw ConfigException(getWrongTypeErrorMSG("server_name", toml_node::STRING, node->get_type()));
_serverName = *node->getString();
return (0);
}
int ServerConfig::putPort(toml_node *node)
{
if (node->get_type() != toml_node::NUM)
return (1);
throw ConfigException(getWrongTypeErrorMSG("server_name", toml_node::NUM, node->get_type()));
_port = node->getNum();
if (_port < 0 || _port > 65536)
throw ConfigException("Invalid port specified!");
return (0);
}
@@ -170,62 +199,61 @@ int ServerConfig::putLocation(toml_node *node)
if (it1->first == "location")
{
if (it1->second->get_type() != toml_node::STRING)
continue;
throw ConfigException(getWrongTypeErrorMSG("location", toml_node::STRING, it1->second->get_type()));
tmp->location = *it1->second->getString();
}
else if (it1->first == "root")
{
if (it1->second->get_type() != toml_node::STRING)
continue ;
throw ConfigException(getWrongTypeErrorMSG("root", toml_node::STRING, it1->second->get_type()));
tmp->root = *it1->second->getString();
}
else if (it1->first == "autoindex")
{
if (it1->second->get_type() != toml_node::BOOL)
continue ;
throw ConfigException(getWrongTypeErrorMSG("autoindex", toml_node::BOOL, it1->second->get_type()));
tmp->autoindex = it1->second->getBool();
}
else if (it1->first == "upload_accept")
{
if (it1->second->get_type() != toml_node::BOOL)
continue ;
throw ConfigException(getWrongTypeErrorMSG("upload_accept", toml_node::BOOL, it1->second->get_type()));
tmp->uploadAccept = it1->second->getBool();
}
else if (it1->first == "upload_dir")
{
if (it1->second->get_type() != toml_node::STRING)
continue ;
throw ConfigException(getWrongTypeErrorMSG("upload_dir", toml_node::STRING, it1->second->get_type()));
tmp->uploadDir = *it1->second->getString();
}
else if (it1->first == "cgi_pass")
{
if (it1->second->get_type() != toml_node::STRING)
continue ;
throw ConfigException(getWrongTypeErrorMSG("cgi_pass", toml_node::STRING, it1->second->get_type()));
tmp->cgi_pass = *it1->second->getString();
}
else if (it1->first == "body_size_limit")
{
DBOUT << "BodySize in locaton" << ENDL;
if (node->get_type() != toml_node::NUM)
continue;
if (it1->second->get_type() != toml_node::NUM)
throw ConfigException(getWrongTypeErrorMSG("body_size_limit", toml_node::NUM, it1->second->get_type()));
tmp->clientBodySize = it1->second->getNum();
}
else if (it1->first == "directory_file")
{
if (it1->second->get_type() != toml_node::STRING)
continue ;
throw ConfigException(getWrongTypeErrorMSG("directory_file", toml_node::STRING, it1->second->get_type()));
tmp->directoryFile = *it1->second->getString();
}
else if (it1->first == "methods")
{
if (it1->second->get_type() != toml_node::ARRAY)
continue ;
throw ConfigException(getWrongTypeErrorMSG("methods", toml_node::ARRAY, it1->second->get_type()));
Array = *it1->second->getArray();
it2 = Array.begin();
while (it2 != Array.end())
{
if ((*it2)->get_type() != toml_node::STRING)
continue ;
throw ConfigException(getWrongTypeErrorMSG("methods elem", toml_node::STRING, (*it2)->get_type()));
tmp->methods.push_back(*((*it2)->getString()));
++it2;
}
@@ -233,15 +261,21 @@ int ServerConfig::putLocation(toml_node *node)
else if (it1->first == "redirect")
{
if (it1->second->get_type() != toml_node::ARRAY)
continue ;
throw ConfigException(getWrongTypeErrorMSG("redirect", toml_node::ARRAY, it1->second->get_type()));
Array = *it1->second->getArray();
if (Array.size() != 2)
throw ConfigException("The redirect field specified the wrong number of arguments!");
it2 = Array.begin();
if ((*it2)->get_type() != toml_node::STRING)
throw ConfigException(getWrongTypeErrorMSG("redirect elem", toml_node::STRING, (*it2)->get_type()));
str = *(*it2)->getString();
++it2;
if ((*it2)->get_type() != toml_node::STRING)
throw ConfigException(getWrongTypeErrorMSG("redirect elem", toml_node::STRING, (*it2)->get_type()));
tmp->redirect.insert(std::make_pair(atoi(str.c_str()), *(*it2)->getString()));
}
else
std::cout << RED << it1->first << ZERO_C << std::endl;
std::cerr << RED << "Warning: unknown parameter: "<< it1->first << ZERO_C << std::endl;
}
_locations.push_back(tmp);
it++;
@@ -279,6 +313,86 @@ void ServerConfig::fillFields(void)
ret = identify(block);
++block;
}
checkConfig();
}
int ServerConfig::isFile(std::string path)
{
struct stat s;
if (stat(path.c_str(), &s) == 0)
{
if (s.st_mode & S_IFDIR)
return (-1);
else if (s.st_mode & S_IFREG)
return (0);
}
else
return (-1);
return (-1);
}
int ServerConfig::isDir(std::string path)
{
struct stat s;
if (stat(path.c_str(), &s) == 0)
{
if (s.st_mode & S_IFDIR)
return (0);
else if (s.st_mode & S_IFREG)
return (-1);
}
else
return (-1);
return (-1);
}
bool ServerConfig::checkFileAndDir(location *loc)
{
std::string root = loc->root;
std::string upload_dir = loc->uploadDir;
std::string directory_file = loc->directoryFile;
if (!root.empty())
{
if (isDir(root) != 0)
throw ConfigException("Directory " + root + " not found!");
}
if (!upload_dir.empty())
{
if (isDir(upload_dir) != 0)
throw ConfigException("Directory " + upload_dir + " not found!");
}
if (!directory_file.empty())
{
directory_file = root + "/" + directory_file;
if (isFile(directory_file) != 0)
throw ConfigException("File " + directory_file + " not found!");
}
return (true);
}
void ServerConfig::checkConfig(void)
{
if (_host.empty())
throw ConfigException("Host field not set!");
else if (_port == 0)
throw ConfigException("Port field not set!");
std::vector<location *>::iterator it;
it = _locations.begin();
location *tmp;
if (it == _locations.end())
throw ConfigException("Required routing settings are missing!");
while (it != _locations.end())
{
tmp = *it;
if (tmp->location.empty())
throw ConfigException("Location field not set!");
checkFileAndDir(tmp);
it++;
}
}
void ServerConfig::printFields(void)
@@ -290,43 +404,43 @@ void ServerConfig::printFields(void)
it1 = _errorPages.begin();
it = _locations.begin();
std::cout << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C;
std::cout << GREEN << "name" << " " << BLUE << _serverName << std::endl;
std::cout << GREEN << "host" << " " << BLUE << _host << std::endl;
std::cout << GREEN << "port" << " " << BLUE << _port << std::endl;
std::cout << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl;
std::cout << GREEN << "location" << std::endl;
DBOUT << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C;
DBOUT << GREEN << "name" << " " << BLUE << _serverName << std::endl;
DBOUT << GREEN << "host" << " " << BLUE << _host << std::endl;
DBOUT << GREEN << "port" << " " << BLUE << _port << std::endl;
DBOUT << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl;
DBOUT << GREEN << "location" << std::endl;
while (it != _locations.end())
{
std::cout << PINK << "------------------------------------------------\n";
std::cout << YELLOW << "location " << BLUE << (*it)->location <<std::endl;
std::cout << YELLOW << "root " << BLUE << (*it)->root <<std::endl;
std::cout << YELLOW << "directoryFile " << BLUE << (*it)->directoryFile <<std::endl;
std::cout << YELLOW << "uploadDir " << BLUE << (*it)->uploadDir <<std::endl;
std::cout << YELLOW << "autoindex " << BLUE << (*it)->autoindex <<std::endl;
std::cout << YELLOW << "uploadAccept " << BLUE << (*it)->uploadAccept <<std::endl;
std::cout << YELLOW << "cgi_pass " << BLUE << (*it)->cgi_pass <<std::endl;
std::cout << YELLOW << "client_body_size " << BLUE << (*it)->clientBodySize <<std::endl;
std::cout << YELLOW << "methods " << std::endl;
DBOUT << PINK << "------------------------------------------------\n";
DBOUT << YELLOW << "location " << BLUE << (*it)->location <<std::endl;
DBOUT << YELLOW << "root " << BLUE << (*it)->root <<std::endl;
DBOUT << YELLOW << "directoryFile " << BLUE << (*it)->directoryFile <<std::endl;
DBOUT << YELLOW << "uploadDir " << BLUE << (*it)->uploadDir <<std::endl;
DBOUT << YELLOW << "autoindex " << BLUE << (*it)->autoindex <<std::endl;
DBOUT << YELLOW << "uploadAccept " << BLUE << (*it)->uploadAccept <<std::endl;
DBOUT << YELLOW << "cgi_pass " << BLUE << (*it)->cgi_pass <<std::endl;
DBOUT << YELLOW << "client_body_size " << BLUE << (*it)->clientBodySize <<std::endl;
DBOUT << YELLOW << "methods " << std::endl;
it2 = (*it)->methods.begin();
while (it2 != (*it)->methods.end())
{
std::cout << BLUE << *it2 << " ";
DBOUT << BLUE << *it2 << " ";
it2++;
}
std::cout << std::endl;
DBOUT << std::endl;
it3 = (*it)->redirect.begin();
std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
DBOUT << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
++it;
std::cout << PINK << "------------------------------------------------\n";
DBOUT << PINK << "------------------------------------------------\n";
}
std::cout << GREEN << "error pages" << std::endl;
DBOUT << GREEN << "error pages" << std::endl;
while (it1 != _errorPages.end())
{
std::cout << YELLOW << it1->first << " " << BLUE << it1->second << std::endl;
DBOUT << YELLOW << it1->first << " " << BLUE << it1->second << std::endl;
++it1;
}
std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;
DBOUT << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;
}
ServerConfig::~ServerConfig()

View File

@@ -2,8 +2,9 @@
#define SERVERCONFIG_HPP
#include "webserv.hpp"
#include "ConfigException.hpp"
#include "parse.hpp"
struct location
{
std::string location;
@@ -15,7 +16,7 @@ struct location
std::vector<std::string> methods;
std::map<int, std::string> redirect;
std::string cgi_pass;
unsigned int clientBodySize;
ssize_t clientBodySize;
};
struct serverListen
@@ -33,7 +34,7 @@ private:
std::string _serverName;
std::string _host;
int _port;
int _clientBodySize;
ssize_t _clientBodySize;
std::map<int, std::string> _errorPages;
std::vector<location *> _locations;
@@ -47,11 +48,12 @@ public:
void setErrorPages(std::map<int, std::string>);
void setLocations(std::vector<location *>);
void setRoot(TOMLMap *);
void checkConfig(void);
std::string &getServerName(void);
std::string &getHost(void);
int &getPort(void);
int &getClientBodySize(void);
ssize_t &getClientBodySize(void);
std::vector<location *> &getLocations(void);
std::map<int, std::string> &getErrorPages(void);
TOMLMap *getRoot(void);
@@ -67,11 +69,18 @@ private:
int putName(toml_node *);
int putPort(toml_node *);
int putLocation(toml_node *);
std::string getTypestr(toml_node::e_type);
std::string getWrongTypeErrorMSG(std::string field, toml_node::e_type expected, toml_node::e_type received);
bool checkFileAndDir(location *);
int isFile(std::string path);
int isDir(std::string path);
public:
void fillFields(void);
void printFields(void);
~ServerConfig();
};