Skip to content

Commit

Permalink
Merge pull request #63 from oph-design/config2.0
Browse files Browse the repository at this point in the history
Config2.0
  • Loading branch information
migrae authored Aug 29, 2023
2 parents 33016f9 + 0e10d50 commit 77a4d9e
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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) \
Expand Down Expand Up @@ -31,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 GetContentDisposition.cpp Trim.cpp FindFile.cpp
UTILS_FILES = GetContentDisposition.cpp Trim.cpp FindFile.cpp

ALL_SRC = $(SRC) $(CORE) $(UTILS) $(HTTP) $(CONFIG)

Expand Down
2 changes: 1 addition & 1 deletion conf/webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/config/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class Config {

friend std::ostream& operator<<(std::ostream& stream, const Config& config);

const std::set<int>& 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);
Expand All @@ -52,6 +52,7 @@ class Config {
static void deleteEmptyServer_(ConfigVector& configs);

std::set<int> listen_;
int port_;
int clientMaxBodySize_;
std::string serverName_;
std::string index_;
Expand Down
15 changes: 15 additions & 0 deletions src/config/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -133,6 +134,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<int>::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";
Expand Down
1 change: 1 addition & 0 deletions src/config/ConfigFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ConfigFile {
friend std::ostream& operator<<(std::ostream& stream, ConfigFile& config);

private:
static ConfigVector splitUpListens_(ConfigVector& configvector);
void cleanContent_();
void vaildateConfigFile_();
ConfigVector createConfigVector_();
Expand Down
4 changes: 2 additions & 2 deletions src/config/ConfigGet.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Config.hpp"

const std::set<int>& Config::getListen() const { return this->listen_; }

const int& Config::getClientMaxBodySize() const {
return this->clientMaxBodySize_;
}
Expand All @@ -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_; }
8 changes: 2 additions & 6 deletions src/core/ServerCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
ServerCluster::ServerCluster(ConfigVector configs) {
for (ConfigVector::iterator configIter = configs.begin();
configIter != configs.end(); ++configIter) {
for (std::set<int>::iterator listenIter = configIter->getListen().begin();
listenIter != configIter->getListen().end();
std::advance(listenIter, 1)) {
TcpServer server(*configIter, *listenIter);
this->cluster_.push_back(server);
}
TcpServer server(*configIter);
this->cluster_.push_back(server);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,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) {}
Expand Down
2 changes: 1 addition & 1 deletion src/core/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);
Expand Down
7 changes: 3 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +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();
ServerCluster cluster(configs);
cluster.boot();
}
return 0;
return EXIT_SUCCESS;
}
8 changes: 0 additions & 8 deletions src/utils/IsNumber.cpp

This file was deleted.

11 changes: 8 additions & 3 deletions src/utils/IsNumber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

#include <cctype>
#include <iterator>
#include <string>

bool isNumber(std::string str);

template <typename Type>
bool isNumber(Type cont) {
for (typename Type::iterator iter = cont.begin(); iter != cont.end();
++iter) {
if (!std::isdigit(*iter)) return false;
}
return true;
}
#endif // ISNUMBER_HPP_

0 comments on commit 77a4d9e

Please sign in to comment.