mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
update: func for find location
This commit is contained in:
@@ -4,6 +4,38 @@ Config::Config()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int isFile(std::string path)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
|
||||||
|
if (stat(path.c_str(), &s) == 0)
|
||||||
|
{
|
||||||
|
if (s.st_mode & S_IFDIR)
|
||||||
|
return (-1);
|
||||||
|
else if (s.st_mode & S_IFREG)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (-1);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int isDir(std::string path)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
|
||||||
|
if (stat(path.c_str(), &s) == 0)
|
||||||
|
{
|
||||||
|
if (s.st_mode & S_IFDIR)
|
||||||
|
return (0);
|
||||||
|
else if (s.st_mode & S_IFREG)
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (-1);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
int Config::calcLen(std::string &s1, std::string &s2)
|
int Config::calcLen(std::string &s1, std::string &s2)
|
||||||
{
|
{
|
||||||
unsigned long len = 0;
|
unsigned long len = 0;
|
||||||
@@ -18,27 +50,60 @@ int Config::calcLen(std::string &s1, std::string &s2)
|
|||||||
|
|
||||||
location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||||
{
|
{
|
||||||
int max = 0;
|
unsigned int tryLen = URI.size();
|
||||||
int len = 0;
|
std::string tryLocation;
|
||||||
location *tmp;
|
location *tmp;
|
||||||
|
std::string suffix;
|
||||||
|
std::string suffix1;
|
||||||
std::vector<location *>::iterator it;
|
std::vector<location *>::iterator it;
|
||||||
std::map<int, location *> compare;
|
std::vector<location *> step_1;
|
||||||
|
suffix = URI.substr(URI.rfind(".") + 1, URI.size() - URI.rfind("."));
|
||||||
|
|
||||||
|
while (tryLen)
|
||||||
|
{
|
||||||
|
it = arr.begin();
|
||||||
|
while (it != arr.end())
|
||||||
|
{
|
||||||
|
tmp = *it;
|
||||||
|
tryLocation = URI.substr(0, tryLen);
|
||||||
|
if (tmp->location == tryLocation)
|
||||||
|
step_1.push_back(tmp);
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
if (!step_1.empty())
|
||||||
|
break;
|
||||||
|
tryLen = URI.rfind("/", tryLen - 1);
|
||||||
|
}
|
||||||
|
if (!step_1.empty())
|
||||||
|
{
|
||||||
|
it = step_1.begin();
|
||||||
|
tmp = *it;
|
||||||
|
if (tmp->location == URI || tmp->location.size() > 1)
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
it = arr.begin();
|
it = arr.begin();
|
||||||
while (it != arr.end())
|
while (it != arr.end())
|
||||||
{
|
{
|
||||||
tmp = *it;
|
tmp = *it;
|
||||||
if (tmp->location == URI)
|
if (tmp->location[0] == '*')
|
||||||
|
{
|
||||||
|
suffix1 = tmp->location.substr(2);
|
||||||
|
if (suffix1 == suffix)
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
it = arr.begin();
|
||||||
|
while (it != arr.end())
|
||||||
|
{
|
||||||
|
tmp = *it;
|
||||||
|
if (tmp->location == "/")
|
||||||
return (tmp);
|
return (tmp);
|
||||||
len = calcLen(tmp->location, URI);
|
|
||||||
if (compare.find(len) == compare.end())
|
|
||||||
compare[len] = tmp;
|
|
||||||
if (max < len)
|
|
||||||
max = len;
|
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
return (compare[max]);
|
|
||||||
|
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data)
|
ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data)
|
||||||
|
|||||||
Reference in New Issue
Block a user