From deeed5de4f205b9521a4f028e5e97a6c201edda8 Mon Sep 17 00:00:00 2001 From: Talyx Date: Sun, 23 Jan 2022 17:37:50 +0300 Subject: [PATCH] TOMLMap parser start --- src/Server/Server.cpp | 15 ++---- src/Server/Server.hpp | 2 +- src/Server/ServerConfig.cpp | 94 +++++++++++++++++++++++++++++-------- src/Server/ServerConfig.hpp | 22 +++++++-- 4 files changed, 95 insertions(+), 38 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index ace698a..9b7b321 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -21,7 +21,7 @@ void Server::readConfig(void) TOMLMap *root = parse(); - TOMLMap *map; + // TOMLMap *map; TOMLMap::iterator it1; TOMLMapArray *arr; TOMLMapArray::iterator it; @@ -32,17 +32,8 @@ void Server::readConfig(void) 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; - } - - + _configs.push_back(new ServerConfig(*it)); + std::cout << "biba\n"; ++it; } diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index d7ff9ab..1615676 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -18,7 +18,7 @@ private: struct epoll_event _events[MAX_CLIENT]; struct sockaddr_in _addres; std::string _ip; - std::vector _configs; + std::vector _configs; private: void checkError(int fd, std::string str); diff --git a/src/Server/ServerConfig.cpp b/src/Server/ServerConfig.cpp index 9661161..dcbf8cb 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -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 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,75 @@ void ServerConfig::setLocations(std::vector 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) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + return (0); +} +int ServerConfig::putErrorPage(toml_node *node) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + return (0); +} +int ServerConfig::putHost(toml_node *node) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + return (0); +} +int ServerConfig::putName(toml_node *node) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + return (0); +} +int ServerConfig::putPort(toml_node *node) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + return (0); +} +int ServerConfig::putLocation(toml_node *node) +{ + std::cout << TURGUOISE << node << ZERO_C << std::endl; + 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 == "loacation") + 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; + } } ServerConfig::~ServerConfig() diff --git a/src/Server/ServerConfig.hpp b/src/Server/ServerConfig.hpp index bf5eedb..b42fe79 100644 --- a/src/Server/ServerConfig.hpp +++ b/src/Server/ServerConfig.hpp @@ -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); void setLocations(std::vector); - // void setRoot(TOMLMap *); + void setRoot(TOMLMap *); std::string getServerName(void); std::string getHost(void); @@ -42,12 +45,21 @@ public: int getClientBodySize(void); std::vector getLocations(void); std::map 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); ~ServerConfig();