From 842033b4bcb911c4a9fe0a79bacff39fdd292245 Mon Sep 17 00:00:00 2001 From: Talyx Date: Wed, 2 Feb 2022 14:12:30 +0300 Subject: [PATCH] fix: ServerConfig segv --- src/Server/Server.cpp | 10 +++++++++ src/Server/ServerConfig.cpp | 44 ++++++++++++++++++------------------- src/Server/ServerConfig.hpp | 6 ++--- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 2937470..2bfe5f2 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -225,15 +225,25 @@ void Server::start(void) void Server::end(void) { std::vector::iterator pri; + std::vector::iterator loc; + std::vector< location *> l; pri = _configs.begin(); while (pri != _configs.end()) { (*pri)->printFields(); + l = (*pri)->getLocations(); + loc = l.begin(); + while (loc != l.end()) + { + delete *loc; + loc++; + } 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 947df84..91e531d 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -33,7 +33,7 @@ int ServerConfig::getClientBodySize(void) return (_clientBodySize); } -std::vector ServerConfig::getLocations(void) +std::vector ServerConfig::getLocations(void) { return (_locations); } @@ -73,7 +73,7 @@ void ServerConfig::setErrorPages(std::map pages) _errorPages = pages; } -void ServerConfig::setLocations(std::vector locations) +void ServerConfig::setLocations(std::vector locations) { _locations = locations; } @@ -142,7 +142,7 @@ int ServerConfig::putLocation(toml_node *node) TOMLMapArray::iterator it = arr->begin(); TOMLMap *map; TOMLMap::iterator it1; - location tmp; + location *tmp; TOMLArray::iterator it2; TOMLArray Array; @@ -151,43 +151,44 @@ int ServerConfig::putLocation(toml_node *node) { map = *it; it1 = map->begin(); + tmp = new location; while (it1 != map->end()) { if (it1->first == "location") { if (it1->second->get_type() != toml_node::STRING) return (1); - tmp.location = *it1->second->getString(); + 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(); + 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(); + 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(); + 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(); + 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(); + tmp->directoryFile = *it1->second->getString(); } else if (it1->first == "methods") { @@ -199,7 +200,7 @@ int ServerConfig::putLocation(toml_node *node) { if ((*it2)->get_type() != toml_node::STRING) return (1); - tmp.methods.push_back(*((*it2)->getString())); + tmp->methods.push_back(*((*it2)->getString())); ++it2; } @@ -212,14 +213,13 @@ int ServerConfig::putLocation(toml_node *node) it2 = Array.begin(); str = *(*it2)->getString(); ++it2; - tmp.redirect[atoi(str.c_str())] = *(*it2)->getString(); + tmp->redirect.insert(std::make_pair(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); @@ -259,7 +259,7 @@ void ServerConfig::fillFields(void) void ServerConfig::printFields(void) { - std::vector::iterator it; + std::vector::iterator it; std::map::iterator it1; std::vector::iterator it2; std::map::iterator it3; @@ -275,21 +275,21 @@ void ServerConfig::printFields(void) while (it != _locations.end()) { std::cout << PINK << "------------------------------------------------\n"; - std::cout << YELLOW << "location " << BLUE << (*it).location <location <root <directoryFile <uploadDir <autoindex <uploadAccept <methods.begin(); + while (it2 != (*it)->methods.end()) { std::cout << BLUE << *it2 << " "; it2++; } std::cout << std::endl; - it3 = (*it).redirect.begin(); + it3 = (*it)->redirect.begin(); std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl; ++it; std::cout << PINK << "------------------------------------------------\n"; diff --git a/src/Server/ServerConfig.hpp b/src/Server/ServerConfig.hpp index 39be2d4..9749b5a 100644 --- a/src/Server/ServerConfig.hpp +++ b/src/Server/ServerConfig.hpp @@ -28,7 +28,7 @@ private: int _clientBodySize; std::map _errorPages; - std::vector _locations; + std::vector _locations; public: void setServerName(std::string); @@ -36,14 +36,14 @@ public: void setPort(int); void setClientBodySize(int); void setErrorPages(std::map); - void setLocations(std::vector); + void setLocations(std::vector); void setRoot(TOMLMap *); std::string getServerName(void); std::string getHost(void); int getPort(void); int getClientBodySize(void); - std::vector getLocations(void); + std::vector getLocations(void); std::map getErrorPages(void); TOMLMap *getRoot(void);