some cleanup of config stuff

This commit is contained in:
Me
2022-08-07 22:54:58 +02:00
parent 94852babc6
commit cf69168a84
2 changed files with 18 additions and 20 deletions

View File

@@ -31,8 +31,6 @@ ConfigParser::ConfigParser(const char* path)
file.open(path); file.open(path);
if (file.is_open()) if (file.is_open())
{ {
// are there more throws i need to add in case of errors, what would
// those errors be?
while (!file.eof()) while (!file.eof())
{ {
getline(file, buf); getline(file, buf);
@@ -45,7 +43,7 @@ ConfigParser::ConfigParser(const char* path)
} }
else if (comment > 0 && (buf.find_first_not_of(" \t")) < comment) else if (comment > 0 && (buf.find_first_not_of(" \t")) < comment)
{ {
// is there a comment at the end of the line // check for comment at the end of the line
std::string tmp = buf.substr(0, comment - 1); std::string tmp = buf.substr(0, comment - 1);
_content.append(tmp + '\n'); _content.append(tmp + '\n');
} }
@@ -53,7 +51,7 @@ ConfigParser::ConfigParser(const char* path)
file.close(); file.close();
} }
else else
throw std::invalid_argument("open config"); throw std::invalid_argument("failed to open config");
} }
ConfigParser::~ConfigParser() ConfigParser::~ConfigParser()
@@ -147,8 +145,11 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
ret.allow_methods = 0; ret.allow_methods = 0;
ret.path = _get_first_word(&curr); ret.path = _get_first_word(&curr);
// if (ret.path[0] != '/') if (ret.path[0] != '/')
ret.path.insert(0, "/");
// throw std::invalid_argument("Location path require a leading /"); // throw std::invalid_argument("Location path require a leading /");
if (ret.path.back() != '/')
ret.path.push_back('/');
// in theory now curr should be right after the "path" // in theory now curr should be right after the "path"
curr = _content.find_first_not_of(" \t\n", curr); curr = _content.find_first_not_of(" \t\n", curr);
@@ -235,6 +236,10 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
// if (tmp_val[0][0] != '/') // if (tmp_val[0][0] != '/')
// throw std::invalid_argument("Root requires leading /"); // throw std::invalid_argument("Root requires leading /");
// remove trailing /
if (tmp_val[0].back() == '/')
tmp_val[0].erase(tmp_val[0].size(), 1);
// std::cout << "root: " << tmp_val[0] << '\n'; // std::cout << "root: " << tmp_val[0] << '\n';
//might not even do these checks here... //might not even do these checks here...
// if (path_is_valid(tmp_val[0]) == 1) // if (path_is_valid(tmp_val[0]) == 1)
@@ -307,6 +312,9 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
// if (tmp_val[0][0] != '/') // if (tmp_val[0][0] != '/')
// throw std::invalid_argument("Root requires leading /"); // throw std::invalid_argument("Root requires leading /");
// remove trailing /
if (tmp_val[0].back() == '/')
tmp_val[0].erase(tmp_val[0].size(), 1);
// if (path_is_valid(tmp_val[0]) == 1) // if (path_is_valid(tmp_val[0]) == 1)
location->root = tmp_val[0]; location->root = tmp_val[0];
// else // else

View File

@@ -17,21 +17,18 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
if (it->root == "") if (it->root == "")
throw std::invalid_argument("Config file needs a root"); throw std::invalid_argument("Config file needs a root");
if (it->client_body_limit == 0) // index is mandatory in Server
it->client_body_limit = 5000; // what is the recomended size?
if (it->index.empty()) if (it->index.empty())
throw std::invalid_argument("Config file needs an Index"); throw std::invalid_argument("Config file needs an Index");
if (it->client_body_limit == 0)
it->client_body_limit = 5000; // what is the recomended size?
// if error_pages is left empty, we'll use the defaults which // if error_pages is left empty, we'll use the defaults which
// i believe are set elsewhere... // i believe are set elsewhere...
// actually do this at the end, once we know if there aren't any locations
// with path /
if (!_find_root_path_location(it->locations)) if (!_find_root_path_location(it->locations))
{ {
LocationConfig tmp; LocationConfig tmp;
@@ -55,10 +52,8 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
if (it_l->allow_methods == UNKNOWN) if (it_l->allow_methods == UNKNOWN)
it_l->allow_methods = ANY_METHODS; it_l->allow_methods = ANY_METHODS;
// fill out index from Server?
// or do a bunch of checks on what is in there...
if (it_l->index.empty()) if (it_l->index.empty())
it_l->index = it->index; // right? it_l->index = it->index;
// same for redirect status i think // same for redirect status i think
@@ -74,11 +69,6 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
++it_l; ++it_l;
} }
// std::cout << "made it to sorting...\n";
// std::sort(it->locations.begin(), it->locations.end(), compareLocationConfigs);
std::sort(it->locations.begin(), it->locations.end()); std::sort(it->locations.begin(), it->locations.end());
std::reverse(it->locations.begin(), it->locations.end()); std::reverse(it->locations.begin(), it->locations.end());