diff --git a/config/simple.toml b/config/simple.toml index 31d9ecb..fba62df 100644 --- a/config/simple.toml +++ b/config/simple.toml @@ -8,17 +8,18 @@ [[server.location]] location = "/" root = "/var/www/html/jopa.html" + redirect = ["301", "http://localhost/secret"] methods = ["GET", "POST"] - directory_list = true - directory_fallback = "its_a_directory.html" + autoindex = true + directory_file = "its_a_directory.html" upload_accept = true upload_dir = "/var/www/html/upload" [[server.location]] location = "/secret/" root = "/var/www/html/secret.html" methods = ["GET"] - directory_list = false - directory_fallback = "oops.html" + autoindex = false + directory_file = "oops.html" [[server]] name = "2222" host = "10.0.0.1" @@ -33,7 +34,7 @@ location = "/root2/" root = "/var/www/html/jopa.html" methods = ["GET", "POST"] - directory_list = true - directory_fallback = "its_a_directory.html" + autoindex = true + directory_file = "its_a_directory.html" upload_accept = false upload_dir = "/var/www/html/upload" diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 6161153..dbade16 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -35,10 +35,6 @@ void Server::readConfig(void) _configs.push_back(new ServerConfig(*it)); ++it; } - - - - } void Server::setupConfig(void) @@ -132,7 +128,15 @@ void Server::start(void) void Server::end(void) { + std::vector::iterator pri; + pri = _configs.begin(); + while (pri != _configs.end()) + { + (*pri)->printFields(); + delete *pri; + pri++; + } } //----------------------------------------------Other------------------------------------------------------------------------------------------------ void Server::checkError(int fd, std::string str) diff --git a/src/Server/ServerConfig.cpp b/src/Server/ServerConfig.cpp index 7680d0d..10c2e84 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -88,34 +88,140 @@ void ServerConfig::setRoot(TOMLMap * data) int ServerConfig::putBodySizeLimit(toml_node *node) { - if (node->get_type() != toml_node::e_type::NUM) + if (node->get_type() != toml_node::NUM) return (1); - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + _clientBodySize = node->getNum(); return (0); } int ServerConfig::putErrorPage(toml_node *node) { - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + 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) { - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + if (node->get_type() != toml_node::STRING) + return (1); + _host = *node->getString(); return (0); } int ServerConfig::putName(toml_node *node) { - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + if (node->get_type() != toml_node::STRING) + return (1); + _serverName = *node->getString(); return (0); } int ServerConfig::putPort(toml_node *node) { - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + if (node->get_type() != toml_node::NUM) + return (1); + _port = node->getNum(); return (0); } int ServerConfig::putLocation(toml_node *node) { - std::cout << TURGUOISE << *node->toString() << ZERO_C << std::endl; + 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); } @@ -146,10 +252,56 @@ void ServerConfig::fillFields(void) block = server->begin(); while (block != server->end() && ret == 0) { - std::cout << GREEN << block->first << ZERO_C << std::endl; ret = identify(block); ++block; } + // printFields(); +} + +void ServerConfig::printFields(void) +{ + std::vector::iterator it; + std::map::iterator it1; + std::vector::iterator it2; + std::map::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 <first << " " << it3->second << std::endl; + ++it; + std::cout << PINK << "------------------------------------------------\n"; + } + std::cout << GREEN << "error pages" << std::endl; + while (it1 != _errorPages.end()) + { + std::cout << BLUE << it1->first << " " << it1->second << std::endl; + ++it1; + } + std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C; } ServerConfig::~ServerConfig() diff --git a/src/Server/ServerConfig.hpp b/src/Server/ServerConfig.hpp index b42fe79..39be2d4 100644 --- a/src/Server/ServerConfig.hpp +++ b/src/Server/ServerConfig.hpp @@ -61,6 +61,7 @@ private: public: void fillFields(void); + void printFields(void); ~ServerConfig(); }; diff --git a/src/main.cpp b/src/main.cpp index d0fc116..31ee019 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,9 @@ int main(int argc, char **argv) Server server; server.readConfig(); - // server.setupConfig(); - // server.start(); + server.setupConfig(); + server.start(); + // server.end(); return (0);