I really hope this is the last time we fix Location Picker, i think it's good it's pretty elegant, basically set root and location path to never have / at end and that made things easier

This commit is contained in:
Me
2022-08-11 02:01:24 +02:00
9 changed files with 82 additions and 241 deletions

View File

@@ -169,67 +169,11 @@ ServerConfig *_determine_process_server(Client *client, std::vector<ServerConfig
return (&(*default_server));
}
const LocationConfig *_determine_location_COOP_FIX(const ServerConfig &server, const std::string &path)
{
/* Pseudo-code :
- comparer les size(), si location.path > client.path, stop comparaison.
- client.path.compare(0, location.path.size(), location.path)
if ( == 0)
{
if (location.path.size() == client.path.size())
{
FOUND;
}
else if (client.path[location.path.size()] == '/')
{
FOUND;
}
}
else
{
NOT FOUND;
++next;
}
*/
std::vector<LocationConfig>::const_iterator it = server.locations.begin();
while (it != server.locations.end())
{
if (it->path.size() > path.size())
{
// prendre en compte l'éventuel "/" final si location est un dossier
if (it->path.size()-1 > path.size() || it->path[it->path.size()-1] != '/')
{
++it;
continue;
}
}
if (path.compare(0, it->path.size(), it->path) == 0)
{
if (it->path.size() == path.size())
break;
else if (path[it->path.size()-1] == '/')
break;
}
++it;
}
if (it != server.locations.end())
return (&(*it));
else
return (&(server.locations.back()));
}
// Temporary Global Scope. Probably move to Client in the future.
const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path)
{
std::cout << "determin location path sent: " << path << '\n';
/// NO FUCKING IDEA WHY BUT...
// basically if 2 strings are identical to a point, compare from
// longer one or it'll freak out cuz of \0 or something idk
//// Basically: str.compare() from the larger string...
/* RULES ***
If a path coresponds exactly to a location, use that one
@@ -237,105 +181,39 @@ if no path coresponds then use the most correct one
most correct means the most precise branch that is still above
the point we are aiming for
New Rule for location paths, they never end in /
Sooo
If we get a url that ends in / ignore the last /
*/
// IS THERE A WAY TO SIMPLIFY THIS LOGIC ???
// what if i tack on a / at the end of path if there isn't one
// and then compare it to things that are their normal length?
std::string tmp = path;
if (tmp[tmp.size() - 1] != '/')
tmp.push_back('/');
/test/index.html/
/test/index.html
std::string uri = path;
if (uri[uri.size() - 1] == '/')
uri.erase(uri.size() - 1);
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{
std::cout << it->path << " -- ";
if (it->path.size() > tmp.size())
continue ;
if (tmp.compare(0, it->path.size(), it->path) == 0)
{
std::cout << "checking with last /\n";
if (it->path.size() == tmp.size())
{
std::cout << "path sizes are equal \n";
return (&(*it));
}
else if (tmp[it->path.size() - 1] == '/')
{
std::cout << "ends in /\n";
return (&(*it));
}
}
/*
// std::cout << it->path[it->path.size() - 1] << " ";
// it->path.size() -1 only when path ends in / because
// if path doesn't end in / then we are looking for a file
// meaning all it->paths that end in / are wrong if they >=
// if (it->path[it->path.size() - 1] == '/' ? it->path.size() - 1 > path.size() : it->path.size() > path.size())
if (path[path.size() - 1] == '/' ? it->path.size() > path.size() : it->path.size() - 1 > path.size())
if (it->path.size() > uri.size())
{
std::cout << "skipping this one\n";
continue ;
}
// if (it->path[it->path.size() - 1] == '/')
if (path[path.size() - 1] == '/')
if (uri.compare(0, it->path.size(), it->path) == 0)
{
if (path.compare(0, it->path.size(), it->path) == 0)
{
std::cout << "checking with last /\n";
if (it->path.size() == path.size())
{
std::cout << "path sizes are equal \n";
return (&(*it));
}
else if (path[it->path.size() - 1] == '/')
{
std::cout << "ends in /\n";
return (&(*it));
}
}
if (it->path.size() == uri.size())
return (&(*it));
else if (uri[it->path.size()] == '/')
return (&(*it));
}
else
{
if (path.size() <= it->path.size())
{
std::cout << "path is missing a /\n";
if (it->path.compare(0, path.size(), path) == 0)
return (&(*it));
// means we are looking for /test/test_deeper/
// with /test/test_deeper
}
else
{
// if (it->path.compare(0, it->path.size() - 1, path) == 0)
if (path.compare(0, it->path.size(), it->path) == 0)
{
std::cout << "checking without last /\n";
if (it->path.size() - 1 == path.size())
return (&(*it));
else if (path[it->path.size() - 1] == '/')
return (&(*it));
}
}
}
*/
}
// if (it != server.locations.end())
// return (&(*it));
// else
return (&(server.locations.back()));
return (&(server.locations.back()));
// /test/mdr
// /test/mdr/
// /test/mdrBST
@@ -349,17 +227,16 @@ if no path coresponds then use the most correct one
/test/test_deepei
/test/test_deepei/
/test/test_deeperi
/test/test_deeper/super_deep/
/test/aaaaaaaaaaa/super_deep/
*/
}
std::string Webserv::_determine_file_extension(const std::string &path) const
{
size_t dot_pos = path.rfind(".");