Well we learned a lot from Nginx, starting to incorporate some, but hoping for feedback before i do most of that, instead worked on improving the location sorter and checking that locations paths are valid, close but not quite done

This commit is contained in:
Me
2022-08-05 03:42:42 +02:00
parent f7e6b61811
commit 9ac14aa1aa
5 changed files with 90 additions and 53 deletions

View File

@@ -146,7 +146,12 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
ret.client_body_limit = 0;
ret.redirect_status = 0;
ret.allow_methods = 0;
// path must have a leading / else move on to next location
// i guess we'll be strict cuz can't return (NULL); or NUL...
ret.path = _get_first_word(&curr);
if (ret.path[0] != '/')
throw std::invalid_argument("Location path require a leading /");
// in theory now curr should be right after the "path"
curr = _content.find_first_not_of(" \t\n", curr);
@@ -178,6 +183,9 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
}
#include <stdio.h>
void ConfigParser::_set_server_values(ServerConfig *server, \
const std::string key, std::string value)
{
@@ -232,22 +240,33 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
}
else if (key == "root" && size == 1 && server->root == "")
{
// consider using lstat if you don't? want symlinks included?
if (tmp_val[0][0] != '/')
throw std::invalid_argument("Root requires leading /");
// unclear if this is always true? might only be true in Server
// but not in Locations? idk...
//i think it does, i must have read wrong somewhere...
// don't bother using lstat
// don't bother with include <errno.h> for now.
// std::cout << tmp_val[0] << '\n';
// should i also check that it's not a file? no i think S_ISDIR does that right?
const char* folder = tmp_val[0].c_str();
const char* folder = tmp_val[0].substr(1).c_str();
struct stat sb;
printf("folder:%s\n", folder);
if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode))
{
// std::cout << "is a Dir\n";
server->root = tmp_val[0];
}
else
throw std::invalid_argument("root dir could not be opened");
throw std::invalid_argument("root dir could not be opened 1");
// do i need to free folder?
/* DIR* dir = opendir(tmp_val[0].c_str());
if (dir)
closedir(dir);
@@ -350,7 +369,10 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
throw std::invalid_argument("missing value");
else if (key == "root" && size == 1 && location->root == "")
{
const char* folder = tmp_val[0].c_str();
if (tmp_val[0][0] != '/')
throw std::invalid_argument("Root requires leading /");
const char* folder = tmp_val[0].substr(1).c_str();
struct stat sb;
if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode))