cgi_ext, redirect and upload_repo now functional in config, fixed location picker, some cleanup

This commit is contained in:
Me
2022-08-09 02:37:28 +02:00
parent a44b9b493a
commit 97c90236b9
7 changed files with 79 additions and 47 deletions

View File

@@ -24,13 +24,14 @@ server {
}
location /test/something.html {
allow_methods DELETE;
# allow_methods DELETE;
}
# location /something/long/here {
# }
location /test/test_deeper/ {
# allow_methods
autoindex on;
}
@@ -38,9 +39,9 @@ server {
autoindex on;
}
location /test/test_deeper/something.html {
allow_methods DELETE;
}
# location /test/test_deeper/something.html {
# allow_methods DELETE;
# }
# ok in theory if one were to go to /test they would get the index in www

View File

@@ -38,13 +38,10 @@ public:
std::vector<std::string> cgi_ext; // php not .php
bool autoindex;
std::vector<std::string> upload_repo;
std::string upload_repo; // might change name...
// wait if i can call several times, shouldn't it be a map?
// wait no there can only be 1 and i think it might have to be in
// location only...
int redirect_status;
std::string redirect_uri;
int redirect_status;
std::string redirect_uri;
// au pire you do location / { return 301 http://location; }
// and that's how you get the redirect from the root.

View File

@@ -145,6 +145,7 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
ret.allow_methods = 0;
ret.path = _get_first_word(&curr);
// are you sure about this?
if (ret.path[0] != '/')
ret.path.insert(0, "/");
// throw std::invalid_argument("Location path require a leading /");
@@ -290,18 +291,10 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
throw std::invalid_argument("missing value");
else if (key == "root" && size == 1 && location->root == "")
{
// std::cout << "location root: " << tmp_val[0] << '\n';
// if (tmp_val[0][0] != '/')
// throw std::invalid_argument("Root requires leading /");
// remove trailing /
// if (tmp_val[0].back() == '/')
if (tmp_val[0][tmp_val[0].size() - 1] == '/')
tmp_val[0].erase(tmp_val[0].size() - 1, 1);
// if (path_is_valid(tmp_val[0]) == 1)
location->root = tmp_val[0];
// else
// throw std::invalid_argument("Root dir invalid");
location->root = tmp_val[0];
}
else if (key == "autoindex" && size == 1)
{
@@ -310,6 +303,7 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
}
else if (key == "index")
{
// what about index /index.html; aka at the root? not handle?
// you can definitely call Index several times, i think
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->index.push_back(tmp_val[i]);
@@ -341,14 +335,24 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
if (tmp_val.size() != 2)
throw std::invalid_argument("wrong number of values");
// and tmp_val[0] should be a number and tmp_val[1] a string?
if (!(::isNumeric(tmp_val[0])))
throw std::invalid_argument("value not a number");
if (tmp_val[0] != "301" && tmp_val[0] != "302"
&& tmp_val[0] != "303" && tmp_val[0] != "307"
&& tmp_val[0] != "308")
throw std::invalid_argument("bad redirect status");
// double check this
// it means we aren't allowing internal redirects.
if (tmp_val[1].compare(0, 7, "http://")
|| tmp_val[1].compare(0, 8, "https://"))
throw std::invalid_argument("bad redirect uri");
// somehow check that tmp_val[1] is a string? or valid? how?
// something about using access() to see if
location->redirect_status = atoi(tmp_val[0].c_str());
location->redirect_uri = tmp_val[1];
}
else if (key == "upload_repo" && size == 1 && location->upload_repo == "")
{
// what checks to do?
location->upload_repo = tmp_val[0];
}
else
throw std::invalid_argument("bad key value pair");
}

View File

@@ -74,7 +74,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
++it_l;
}
std::sort(it->locations.begin(), it->locations.end());
std::reverse(it->locations.begin(), it->locations.end());
// std::reverse(it->locations.begin(), it->locations.end());
++it;
}

View File

@@ -9,7 +9,7 @@ if path is a valid dir check if index is specified and serve that
if no index and autoindex, server that
if file, server that!
WHere does cgi fit in in all this ???
Where does cgi fit in in all this ???
*/
@@ -39,15 +39,12 @@ WHere does cgi fit in in all this ???
{
// std::cout << "found a valid index\n";
path.append(client->assigned_location->index[i]);
break ; // what if index and autoindex in a single location?
// does this completely fail?
// do this instead of break?
//_get_file(client, path);
_get_file(client, path);
return ;
}
}
if (client->assigned_location->autoindex == true)
{
// _autoindex(client, client->assigned_location, path);
_autoindex(client, path);
return ;
}
@@ -189,4 +186,4 @@ void Webserv::_autoindex(Client *client, std::string &path)
std::cout << "could not open dir\n";
return ;
}
}
}

View File

@@ -171,25 +171,58 @@ ServerConfig *Webserv::_determine_process_server(Client *client)
const LocationConfig *Webserv::_determine_location(const ServerConfig &server, const std::string &path) const
{
// std::cout << "determin location path sent: " << path << '\n';
std::cout << "determin location path sent: " << path << '\n';
std::vector<LocationConfig>::const_iterator it = server.locations.begin();
while (it != server.locations.end())
/// 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
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
*/
std::vector<LocationConfig>::const_iterator best;
std::cout << "\nMade it to weird location picker case.\n";
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{
// std::cout << it->path << " -- ";
// if (it->path.compare(0, path.size(), path) == 0)
if (it->path.compare(0, it->path.size(), path) == 0)
break;
// kinda gross i know but... have to deal with when there's a / at the end
if (it->path[it->path.size() - 1] == '/' \
std::cout << it->path << " -- ";
// if (rit->path.size() > path.size())
/* if ((rit->path[rit->path.size() - 1] == '/' ? rit->path.size() : rit->path.size() - 1) > path.size())
{
std::cout << "skipping this one\n";
continue ;
}
*/
// OK I REALLY DON"T LOVE THIS PART, BUT IT DOES WORK FOR NOW...
// if (it->path[it->path.size() - 1] == '/'
// && it->path.compare(0, it->path.size(), path + "/") == 0)
// HOLD ON THIS MIGHT BE GOOD, BUT I COULD USE SOME HELP...
if (it->path[it->path.size() - 1] == '/'
&& it->path.compare(0, it->path.size() - 1, path) == 0)
break;
++it;
{
best = it;
std::cout << "Picked a best! 1\n";
}
// int comp = path.compare(0, rit->path.size(), rit->path);
//int comp = rit->path.compare(0, rit->path.size() - 1, path);
// std::cout << "rit path size: " << rit->path.size() << " comp: " << comp << '\n';
// if (rit->path.compare(0, rit->path.size(), path) == 0)
// if (comp == 0)
if (path.compare(0, it->path.size(), it->path) == 0)
{
best = it;
std::cout << "Picked a best! 2\n";
}
}
if (it != server.locations.end())
return (&(*it));
else
return (&(server.locations.back()));
return (&(*best));
}
std::string Webserv::_determine_file_extension(const std::string &path) const

View File

@@ -3,7 +3,7 @@
<head>
<title>Webserv test index</title>
<!-- <link rel="stylesheet" href="stylesheet/style.css"> -->
<link rel="stylesheet" href="/stylesheet/style.css">
<!-- <link rel="stylesheet" href="/stylesheet/style.css"> -->
</head>
<body>