From 09c0fbf3a942240ee4fbeeaed710a9bdecf2ccc8 Mon Sep 17 00:00:00 2001 From: Talyx Date: Sun, 6 Feb 2022 22:06:54 +0300 Subject: [PATCH] add: new Config Class --- src/Client/Config.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++ src/Client/Config.hpp | 25 +++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/Client/Config.cpp create mode 100644 src/Client/Config.hpp diff --git a/src/Client/Config.cpp b/src/Client/Config.cpp new file mode 100644 index 0000000..a962dd1 --- /dev/null +++ b/src/Client/Config.cpp @@ -0,0 +1,85 @@ +#include "Config.hpp" + +Config::Config() +{ +} + +int Config::calcLen(std::string &s1, std::string &s2) +{ + unsigned long len = 0; + while ((len < (s1.size() )) && (len < (s2.size()))) + { + if (s1[len] != s2[len]) + break; + len++; + } + return (len); +} + +location *Config::getLocation(std::vector &arr, std::string &URI) +{ + int max = 0; + int len = 0; + location *tmp; + std::vector::iterator it; + std::map compare; + + it = arr.begin(); + while (it != arr.end()) + { + tmp = *it; + if (tmp->location == URI) + return (tmp); + len = calcLen(tmp->location, URI); + if (compare.find(len) == compare.end()) + compare[len] = tmp; + if (max < len) + max = len; + it++; + } + return (compare[max]); + +} + +ServerConfig *Config::getConfig(std::vector &arr, Request &request, serverListen &data) +{ + ServerConfig *tmp; + std::vector::iterator it; + std::vector step_1; + std::vector step_2; + + it = arr.begin(); + while (it != arr.end()) + { + tmp = *it; + if (tmp->getPort() == data.port && tmp->getHost() == data.ip) + step_1.push_back(tmp); + it++; + } + if (step_1.size() == 1) + return (step_1[0]); + it = step_1.begin(); + while (it != step_1.end()) + { + tmp = *it; + if (!tmp->getServerName().empty()) + step_2.push_back(tmp); + it++; + } + if (step_2.size() == 0) + return (step_1[0]); + it = step_2.begin(); + while (it != step_2.end()) + { + tmp = *it; + if (tmp->getServerName() == request.getHost()) + return (tmp); + it++; + } + return (step_1[0]); +} + + +Config::~Config() +{ +} \ No newline at end of file diff --git a/src/Client/Config.hpp b/src/Client/Config.hpp new file mode 100644 index 0000000..4cb0b31 --- /dev/null +++ b/src/Client/Config.hpp @@ -0,0 +1,25 @@ +#ifndef CONFIG_HPP +#define CONFIG_HPP + +#include "webserv.hpp" +#include "ServerConfig.hpp" +#include "Request.hpp" + +class Request; + +class Config +{ +private: + +public: + Config(); + ~Config(); + + static int calcLen(std::string &, std::string &); + static location *getLocation(std::vector &, std::string &URI); + static ServerConfig *getConfig(std::vector &, Request &request, serverListen &data); +private: + +}; + +#endif \ No newline at end of file