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

This commit is contained in:
3lswear
2022-01-28 20:39:39 +03:00
16 changed files with 861 additions and 410 deletions

View File

@@ -37,25 +37,11 @@ void Server::readConfig(void)
arr = root->find("server")->second->getMapArray();
it = arr->begin();
/* while (it != arr->end()) */
/* { */
/* std::cout << BLUE << *it << std::endl; */
/* map = *it; */
/* it1 = map->begin(); */
/* while (it1 != map->end()) */
/* { */
/* std::cout << TURGUOISE << it1->first << it1->second << ZERO_C << std::endl; */
/* ++it1; */
/* } */
/* ++it; */
/* } */
while (it != arr->end())
{
_configs.push_back(new ServerConfig(*it));
++it;
}
}
void Server::setupConfig(void)
@@ -97,6 +83,11 @@ void Server::add_to_epoll_list(int fd)
void Server::start(void)
{
Socket serverSocket(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
char buff[BUFFSIZE + 1] = {0};
Header header;
int fd_accept;
int code;
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
char buf[BUFFSIZE + 1] = {0};
@@ -183,7 +174,15 @@ void Server::start(void)
void Server::end(void)
{
std::vector<ServerConfig *>::iterator pri;
pri = _configs.begin();
while (pri != _configs.end())
{
(*pri)->printFields();
delete *pri;
pri++;
}
}
//----------------------------------------------Other------------------------------------------------------------------------------------------------
void Server::checkError(int fd, std::string str)

View File

@@ -2,12 +2,15 @@
ServerConfig::ServerConfig()
{
ret = 0;
}
// ServerConfig::ServerConfig(TOMLMap *map)
// {
// _root = map;
// }
ServerConfig::ServerConfig(TOMLMap *map)
{
ret = 0;
server = map;
fillFields();
}
//--------------------------------------------------GET/SET---------------------------------------
std::string ServerConfig::getServerName(void)
@@ -40,10 +43,10 @@ std::map<int, std::string> ServerConfig::getErrorPages(void)
return (_errorPages);
}
// TOMLMap ServerConfig::*getRoot(void)
// {
// return (this->_root);
// }
TOMLMap *ServerConfig::getRoot(void)
{
return (server);
}
void ServerConfig::setServerName(std::string name)
{
@@ -75,24 +78,229 @@ void ServerConfig::setLocations(std::vector<location> locations)
_locations = locations;
}
// void ServerConfig::setRoot(TOMLMap * data)
// {
// _root = data;
// }
void ServerConfig::setRoot(TOMLMap * data)
{
server = data;
}
//--------------------------------------------------Parse-Config---------------------------------------
int ServerConfig::putBodySizeLimit(toml_node *node)
{
if (node->get_type() != toml_node::NUM)
return (1);
_clientBodySize = node->getNum();
return (0);
}
int ServerConfig::putErrorPage(toml_node *node)
{
if (node->get_type() != toml_node::MAP)
return (1);
TOMLMap *map = node->getMap();
TOMLMap::iterator it;
std::string s;
it = map->begin();
while (it != map->end())
{
if (it->second->get_type() != toml_node::STRING)
return (1);
s = it->first;
_errorPages[atoi(s.c_str())] = *it->second->getString();
++it;
}
return (0);
}
int ServerConfig::putHost(toml_node *node)
{
if (node->get_type() != toml_node::STRING)
return (1);
_host = *node->getString();
return (0);
}
int ServerConfig::putName(toml_node *node)
{
if (node->get_type() != toml_node::STRING)
return (1);
_serverName = *node->getString();
return (0);
}
int ServerConfig::putPort(toml_node *node)
{
if (node->get_type() != toml_node::NUM)
return (1);
_port = node->getNum();
return (0);
}
int ServerConfig::putLocation(toml_node *node)
{
if (node->get_type() != toml_node::MAPARRAY)
return (1);
TOMLMapArray *arr = node->getMapArray();
TOMLMapArray::iterator it = arr->begin();
TOMLMap *map;
TOMLMap::iterator it1;
location tmp;
TOMLArray::iterator it2;
TOMLArray Array;
std::string str;
while (it != arr->end())
{
map = *it;
it1 = map->begin();
while (it1 != map->end())
{
if (it1->first == "location")
{
if (it1->second->get_type() != toml_node::STRING)
return (1);
tmp.location = *it1->second->getString();
}
else if (it1->first == "root")
{
if (it1->second->get_type() != toml_node::STRING)
return (1);
tmp.root = *it1->second->getString();
}
else if (it1->first == "autoindex")
{
if (it1->second->get_type() != toml_node::BOOL)
return (1);
tmp.autoindex = it1->second->getBool();
}
else if (it1->first == "upload_accept")
{
if (it1->second->get_type() != toml_node::BOOL)
return (1);
tmp.uploadAccept = it1->second->getBool();
}
else if (it1->first == "upload_dir")
{
if (it1->second->get_type() != toml_node::STRING)
return (1);
tmp.uploadDir = *it1->second->getString();
}
else if (it1->first == "directory_file")
{
if (it1->second->get_type() != toml_node::STRING)
return (1);
tmp.directoryFile = *it1->second->getString();
}
else if (it1->first == "methods")
{
if (it1->second->get_type() != toml_node::ARRAY)
return (1);
Array = *it1->second->getArray();
it2 = Array.begin();
while (it2 != Array.end())
{
if ((*it2)->get_type() != toml_node::STRING)
return (1);
tmp.methods.push_back(*((*it2)->getString()));
++it2;
}
}
else if (it1->first == "redirect")
{
if (it1->second->get_type() != toml_node::ARRAY)
return (1);
Array = *it1->second->getArray();
it2 = Array.begin();
str = *(*it2)->getString();
++it2;
tmp.redirect[atoi(str.c_str())] = *(*it2)->getString();
}
else
std::cout << RED << it1->first << ZERO_C << std::endl;
it1++;
}
_locations.push_back(tmp);
memset(&tmp, 0, sizeof(tmp));
it++;
}
return (0);
}
int ServerConfig::identify(TOMLMap::iterator it)
{
if (it->first == "body_size_limit")
putBodySizeLimit(it->second);
else if (it->first == "error_page")
putErrorPage(it->second);
else if (it->first == "host")
putHost(it->second);
else if (it->first == "location")
putLocation(it->second);
else if (it->first == "name")
putName(it->second);
else if (it->first == "port")
putPort(it->second);
else
return (1);
return (0);
}
void ServerConfig::fillFields(void)
{
// TOMLMap *tmp = _root;
// TOMLMap::iterator it;
// it = tmp->begin();
TOMLMap::iterator block;
// while (it != tmp->end())
// {
// std::cout << it->first << std::endl;
// }
block = server->begin();
while (block != server->end() && ret == 0)
{
ret = identify(block);
++block;
}
}
void ServerConfig::printFields(void)
{
std::vector<location>::iterator it;
std::map<int, std::string>::iterator it1;
std::vector<std::string>::iterator it2;
std::map<int, std::string>::iterator it3;
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;
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 << "methods " << std::endl;
it2 = (*it).methods.begin();
while (it2 != (*it).methods.end())
{
std::cout << BLUE << *it2 << " ";
it2++;
}
std::cout << std::endl;
it3 = (*it).redirect.begin();
std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
++it;
std::cout << PINK << "------------------------------------------------\n";
}
std::cout << GREEN << "error pages" << std::endl;
while (it1 != _errorPages.end())
{
std::cout << YELLOW << it1->first << " " << BLUE << it1->second << std::endl;
++it1;
}
std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;
}
ServerConfig::~ServerConfig()

View File

@@ -2,6 +2,7 @@
#define SERVERCONFIG_HPP
#include "webserv.hpp"
#include "parse.hpp"
struct location
{
@@ -17,8 +18,10 @@ struct location
class ServerConfig
{
public:
int ret;
private:
// TOMLMap *_root;
TOMLMap *server;
std::string _serverName;
std::string _host;
int _port;
@@ -34,7 +37,7 @@ public:
void setClientBodySize(int);
void setErrorPages(std::map<int, std::string>);
void setLocations(std::vector<location>);
// void setRoot(TOMLMap *);
void setRoot(TOMLMap *);
std::string getServerName(void);
std::string getHost(void);
@@ -42,13 +45,23 @@ public:
int getClientBodySize(void);
std::vector<location> getLocations(void);
std::map<int, std::string> getErrorPages(void);
// TOMLMap *getRoot(void);
TOMLMap *getRoot(void);
public:
ServerConfig();
// ServerConfig(TOMLMap *root);
ServerConfig(TOMLMap *root);
private:
int identify(TOMLMap::iterator it);
int putBodySizeLimit(toml_node *);
int putErrorPage(toml_node *);
int putHost(toml_node *);
int putName(toml_node *);
int putPort(toml_node *);
int putLocation(toml_node *);
public:
void fillFields(void);
void printFields(void);
~ServerConfig();
};