From 7ae79076552eff45ce72e2cad8c6416f3565194e Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Mon, 28 Aug 2023 18:01:36 +0200 Subject: [PATCH 1/8] changed config vector so every port has its own config file --- src/config/Config.cpp | 1 + src/config/Config.hpp | 3 ++- src/config/ConfigFile.cpp | 14 ++++++++++++++ src/config/ConfigFile.hpp | 1 + src/config/ConfigGet.cpp | 4 ++-- src/core/ServerCluster.cpp | 6 +----- src/core/TcpServer.cpp | 4 ++-- src/core/TcpServer.hpp | 2 +- src/main.cpp | 1 + 9 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 94842a3..58c8a40 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -19,6 +19,7 @@ Config& Config::operator=(const Config& obj) { this->root_ = obj.root_; this->locations_ = obj.locations_; this->errorPage_ = obj.errorPage_; + this->port_ = obj.port_; return *this; } diff --git a/src/config/Config.hpp b/src/config/Config.hpp index bcdacc8..68daf57 100644 --- a/src/config/Config.hpp +++ b/src/config/Config.hpp @@ -33,13 +33,13 @@ class Config { friend std::ostream& operator<<(std::ostream& stream, const Config& config); - const std::set& getListen() const; const int& getClientMaxBodySize() const; const std::string& getServerName() const; const std::string& getIndex() const; const std::string& getRoot() const; const LocationVector& getLocations() const; const ErrorMap& getErrorPage() const; + const int& getPort() const; private: static ConfigVector& handleDuplicates_(ConfigVector& configs); @@ -52,6 +52,7 @@ class Config { static void deleteEmptyServer_(ConfigVector& configs); std::set listen_; + int port_; int clientMaxBodySize_; std::string serverName_; std::string index_; diff --git a/src/config/ConfigFile.cpp b/src/config/ConfigFile.cpp index 5ef27ad..e0dcdad 100644 --- a/src/config/ConfigFile.cpp +++ b/src/config/ConfigFile.cpp @@ -133,6 +133,20 @@ ConfigVector ConfigFile::createConfigVector_() { return configVector; } +ConfigVector ConfigFile::splitUpListens_(ConfigVector &configvector) { + ConfigVector newConfigVector; + for (ConfigVector::iterator vectorIter = configvector.begin(); + vectorIter != configvector.end(); ++vectorIter) { + for (std::set::iterator listenIter = vectorIter->listen_.begin(); + listenIter != vectorIter->listen_.end(); ++listenIter) { + Config newConfig(*vectorIter); + newConfig.port_ = *listenIter; + newConfigVector.push_back(newConfig); + } + } + return newConfigVector; +} + std::ostream& operator<<(std::ostream& stream, ConfigFile& config) { config.updateBackup(); stream << "Line\tError\tContent\n"; diff --git a/src/config/ConfigFile.hpp b/src/config/ConfigFile.hpp index 2f4503e..5fe4a65 100644 --- a/src/config/ConfigFile.hpp +++ b/src/config/ConfigFile.hpp @@ -33,6 +33,7 @@ class ConfigFile { bool isValid(); friend std::ostream& operator<<(std::ostream& stream, ConfigFile& config); + static ConfigVector splitUpListens_(ConfigVector &configvector); private: void cleanContent_(); diff --git a/src/config/ConfigGet.cpp b/src/config/ConfigGet.cpp index 352677d..81d1284 100644 --- a/src/config/ConfigGet.cpp +++ b/src/config/ConfigGet.cpp @@ -1,7 +1,5 @@ #include "Config.hpp" -const std::set& Config::getListen() const { return this->listen_; } - const int& Config::getClientMaxBodySize() const { return this->clientMaxBodySize_; } @@ -15,3 +13,5 @@ const std::string& Config::getRoot() const { return this->root_; } const LocationVector& Config::getLocations() const { return this->locations_; } const ErrorMap& Config::getErrorPage() const { return this->errorPage_; } + +const int& Config::getPort() const { return this->port_; } diff --git a/src/core/ServerCluster.cpp b/src/core/ServerCluster.cpp index 23c99c4..dfc93e3 100644 --- a/src/core/ServerCluster.cpp +++ b/src/core/ServerCluster.cpp @@ -3,12 +3,8 @@ ServerCluster::ServerCluster(ConfigVector configs) { for (ConfigVector::iterator configIter = configs.begin(); configIter != configs.end(); ++configIter) { - for (std::set::iterator listenIter = configIter->getListen().begin(); - listenIter != configIter->getListen().end(); - std::advance(listenIter, 1)) { - TcpServer server(*configIter, *listenIter); + TcpServer server(*configIter); this->cluster_.push_back(server); - } } } diff --git a/src/core/TcpServer.cpp b/src/core/TcpServer.cpp index 1372013..6a7fc2d 100644 --- a/src/core/TcpServer.cpp +++ b/src/core/TcpServer.cpp @@ -183,9 +183,9 @@ void TcpServer::boot() { TcpServer::TcpServer(std::string ip_addr, int port) : ip_addr_(ip_addr), port_(port), nfds_(1), socketopt_(1) {} -TcpServer::TcpServer(Config &config, int port) +TcpServer::TcpServer(Config &config) : ip_addr_(config.getRoot()), - port_(port), + port_(config.getPort()), nfds_(1), socketopt_(1), config_(config) {} diff --git a/src/core/TcpServer.hpp b/src/core/TcpServer.hpp index 3bd6e7c..86ca107 100644 --- a/src/core/TcpServer.hpp +++ b/src/core/TcpServer.hpp @@ -26,7 +26,7 @@ class TcpServer { public: TcpServer(std::string ip_addr, int port); - TcpServer(Config &config, int port); + TcpServer(Config &config); TcpServer(const TcpServer &); ~TcpServer(); TcpServer &operator=(const TcpServer &); diff --git a/src/main.cpp b/src/main.cpp index 174f423..005de12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ int main(int argc, char *argv[]) { return 1; } else { configFile.~ConfigFile(); + configs = ConfigFile::splitUpListens_(configs); ServerCluster cluster(configs); cluster.boot(); } From 6763c5a6ed41a306406410582a48d8296aa579ce Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:19:57 +0200 Subject: [PATCH 2/8] cleaned up Makefile --- conf/webserv.conf | 2 +- src/config/ConfigFile.cpp | 3 ++- src/main.cpp | 8 +++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/conf/webserv.conf b/conf/webserv.conf index 7a9517b..d1574db 100644 --- a/conf/webserv.conf +++ b/conf/webserv.conf @@ -10,7 +10,7 @@ server { # root /var/www/html; # index index.html; # error_page 400 hihi; - # index outer; + index outer; # location first { # fastcgi_pass path python; diff --git a/src/config/ConfigFile.cpp b/src/config/ConfigFile.cpp index e0dcdad..e3946b6 100644 --- a/src/config/ConfigFile.cpp +++ b/src/config/ConfigFile.cpp @@ -115,6 +115,7 @@ ConfigVector ConfigFile::createConfig() { ConfigVector configVector = ConfigFile::createConfigVector_(); if (this->isValid() == false) return ConfigVector(); configVector = Config::handleDuplicates_(configVector); + configVector = ConfigFile::splitUpListens_(configVector); return configVector; } @@ -133,7 +134,7 @@ ConfigVector ConfigFile::createConfigVector_() { return configVector; } -ConfigVector ConfigFile::splitUpListens_(ConfigVector &configvector) { +ConfigVector ConfigFile::splitUpListens_(ConfigVector& configvector) { ConfigVector newConfigVector; for (ConfigVector::iterator vectorIter = configvector.begin(); vectorIter != configvector.end(); ++vectorIter) { diff --git a/src/main.cpp b/src/main.cpp index 005de12..c7ae976 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,16 +5,14 @@ int main(int argc, char *argv[]) { ConfigFile configFile; - if (!configFile.openFile(argc, argv)) return 1; + if (!configFile.openFile(argc, argv)) return EXIT_FAILURE; ConfigVector configs = configFile.createConfig(); if (configFile.isValid() == false) { std::cout << configFile << std::endl; - return 1; + return EXIT_FAILURE; } else { - configFile.~ConfigFile(); - configs = ConfigFile::splitUpListens_(configs); ServerCluster cluster(configs); cluster.boot(); } - return 0; + return EXIT_SUCCESS; } From 17fd897517cc46ed457449286de7dd38af189925 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:28:48 +0200 Subject: [PATCH 3/8] templated IsNumber.hpp --- Makefile | 5 +++-- src/utils/IsNumber.cpp | 8 -------- src/utils/IsNumber.hpp | 9 +++++++-- 3 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 src/utils/IsNumber.cpp diff --git a/Makefile b/Makefile index d3bc611..b76d8f6 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ LCFLAGS = -fsanitize=address HEADERFLAGS = -I src/core -I include -I src/utils -I src/http \ -I src/config CFLAGS = $(LCFLAGS) $(HEADERFLAGS) \ - -std=c++98 -Wall -Wextra -Werror -g -pedantic + -std=c++98 -Wall -Wextra -Werror -g -pedantic \ + -Wc++11-extensions LFLAGS = $(LCFLAGS) ################################################################################ @@ -30,7 +31,7 @@ CONFIG_FILES = ConfigFile.cpp Line.cpp Location.cpp Config.cpp \ UTILS = $(addprefix $(UTILS_DIR), $(UTILS_FILES)) UTILS_DIR = src/utils/ -UTILS_FILES = IsNumber.cpp +UTILS_FILES = ALL_SRC = $(SRC) $(CORE) $(UTILS) $(HTTP) $(CONFIG) diff --git a/src/utils/IsNumber.cpp b/src/utils/IsNumber.cpp deleted file mode 100644 index fc4b92f..0000000 --- a/src/utils/IsNumber.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "IsNumber.hpp" - -bool isNumber(std::string str) { - for (std::string::iterator iter = str.begin(); iter != str.end(); ++iter) { - if (!std::isdigit(*iter)) return false; - } - return true; -} diff --git a/src/utils/IsNumber.hpp b/src/utils/IsNumber.hpp index 0fe8c80..2ac227f 100644 --- a/src/utils/IsNumber.hpp +++ b/src/utils/IsNumber.hpp @@ -5,6 +5,11 @@ #include #include -bool isNumber(std::string str); - +template +bool isNumber(Type str) { + for (typename Type::iterator iter = str.begin(); iter != str.end(); ++iter) { + if (!std::isdigit(*iter)) return false; + } + return true; +} #endif // ISNUMBER_HPP_ From a00e557cc1b62c101b2c3eb876ff9e374442f3e4 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:30:02 +0200 Subject: [PATCH 4/8] fixed isNumber Styling --- src/utils/IsNumber.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/IsNumber.hpp b/src/utils/IsNumber.hpp index 2ac227f..d76628a 100644 --- a/src/utils/IsNumber.hpp +++ b/src/utils/IsNumber.hpp @@ -3,11 +3,11 @@ #include #include -#include template -bool isNumber(Type str) { - for (typename Type::iterator iter = str.begin(); iter != str.end(); ++iter) { +bool isNumber(Type cont) { + for (typename Type::iterator iter = cont.begin(); iter != cont.end(); + ++iter) { if (!std::isdigit(*iter)) return false; } return true; From d5e000c8a6aa3d7b1f9babe9ec26fd1781dd9678 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:32:45 +0200 Subject: [PATCH 5/8] fixed norm --- src/config/ConfigFile.hpp | 2 +- src/core/ServerCluster.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/ConfigFile.hpp b/src/config/ConfigFile.hpp index 5fe4a65..53c571a 100644 --- a/src/config/ConfigFile.hpp +++ b/src/config/ConfigFile.hpp @@ -33,9 +33,9 @@ class ConfigFile { bool isValid(); friend std::ostream& operator<<(std::ostream& stream, ConfigFile& config); - static ConfigVector splitUpListens_(ConfigVector &configvector); private: + static ConfigVector splitUpListens(ConfigVector& configvector); void cleanContent_(); void vaildateConfigFile_(); ConfigVector createConfigVector_(); diff --git a/src/core/ServerCluster.cpp b/src/core/ServerCluster.cpp index dfc93e3..8caea8b 100644 --- a/src/core/ServerCluster.cpp +++ b/src/core/ServerCluster.cpp @@ -3,8 +3,8 @@ ServerCluster::ServerCluster(ConfigVector configs) { for (ConfigVector::iterator configIter = configs.begin(); configIter != configs.end(); ++configIter) { - TcpServer server(*configIter); - this->cluster_.push_back(server); + TcpServer server(*configIter); + this->cluster_.push_back(server); } } From 8bc704533a8397fb7da4aff296a59ba573652c51 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:36:55 +0200 Subject: [PATCH 6/8] added missing undescore --- src/config/ConfigFile.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/ConfigFile.hpp b/src/config/ConfigFile.hpp index 53c571a..5dac5c4 100644 --- a/src/config/ConfigFile.hpp +++ b/src/config/ConfigFile.hpp @@ -35,7 +35,7 @@ class ConfigFile { friend std::ostream& operator<<(std::ostream& stream, ConfigFile& config); private: - static ConfigVector splitUpListens(ConfigVector& configvector); + static ConfigVector splitUpListens_(ConfigVector& configvector); void cleanContent_(); void vaildateConfigFile_(); ConfigVector createConfigVector_(); From f13fb5a07c5e58e074f916d4d232a8c37e65c28f Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:38:00 +0200 Subject: [PATCH 7/8] removed compileflag --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d59fb70..b3ecd26 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,7 @@ LCFLAGS = #-fsanitize=address HEADERFLAGS = -I src/core -I include -I src/utils -I src/http \ -I src/config CFLAGS = $(LCFLAGS) $(HEADERFLAGS) \ - -std=c++98 -Wall -Wextra -Werror -g -pedantic \ - -Wc++11-extensions + -std=c++98 -Wall -Wextra -Werror -g -pedantic LFLAGS = $(LCFLAGS) ################################################################################ From 0e10d50865aa01cbf7e6e2388fbc8ca8c67cff1e Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Tue, 29 Aug 2023 15:38:14 +0200 Subject: [PATCH 8/8] readded fsanitize --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b3ecd26..da068e8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME = webserv CC = c++ -LCFLAGS = #-fsanitize=address +LCFLAGS = -fsanitize=address HEADERFLAGS = -I src/core -I include -I src/utils -I src/http \ -I src/config CFLAGS = $(LCFLAGS) $(HEADERFLAGS) \