From cf69168a8456cc6146d216c0f9f6debaa6df86f8 Mon Sep 17 00:00:00 2001 From: Me Date: Sun, 7 Aug 2022 22:54:58 +0200 Subject: [PATCH] some cleanup of config stuff --- srcs/config/parser.cpp | 18 +++++++++++++----- srcs/config/postProcessing.cpp | 20 +++++--------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/srcs/config/parser.cpp b/srcs/config/parser.cpp index 38a5c4b..982b160 100644 --- a/srcs/config/parser.cpp +++ b/srcs/config/parser.cpp @@ -31,8 +31,6 @@ ConfigParser::ConfigParser(const char* path) file.open(path); if (file.is_open()) { - // are there more throws i need to add in case of errors, what would - // those errors be? while (!file.eof()) { getline(file, buf); @@ -45,7 +43,7 @@ ConfigParser::ConfigParser(const char* path) } 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); _content.append(tmp + '\n'); } @@ -53,7 +51,7 @@ ConfigParser::ConfigParser(const char* path) file.close(); } else - throw std::invalid_argument("open config"); + throw std::invalid_argument("failed to open config"); } ConfigParser::~ConfigParser() @@ -147,8 +145,11 @@ LocationConfig ConfigParser::_parse_location(size_t *start) ret.allow_methods = 0; 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 /"); + if (ret.path.back() != '/') + ret.path.push_back('/'); // in theory now curr should be right after the "path" 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] != '/') // 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'; //might not even do these checks here... // if (path_is_valid(tmp_val[0]) == 1) @@ -307,6 +312,9 @@ void ConfigParser::_set_location_values(LocationConfig *location, \ // if (tmp_val[0][0] != '/') // 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) location->root = tmp_val[0]; // else diff --git a/srcs/config/postProcessing.cpp b/srcs/config/postProcessing.cpp index 62c63b5..c35968f 100644 --- a/srcs/config/postProcessing.cpp +++ b/srcs/config/postProcessing.cpp @@ -17,21 +17,18 @@ void ConfigParser::_post_processing(std::vector *servers) if (it->root == "") throw std::invalid_argument("Config file needs a root"); - if (it->client_body_limit == 0) - it->client_body_limit = 5000; // what is the recomended size? - - + // index is mandatory in Server if (it->index.empty()) 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 // 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)) { LocationConfig tmp; @@ -55,10 +52,8 @@ void ConfigParser::_post_processing(std::vector *servers) if (it_l->allow_methods == UNKNOWN) 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()) - it_l->index = it->index; // right? + it_l->index = it->index; // same for redirect status i think @@ -74,11 +69,6 @@ void ConfigParser::_post_processing(std::vector *servers) ++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::reverse(it->locations.begin(), it->locations.end());