Files
42_INT_12_webserv/srcs/config/postProcessing.cpp

105 lines
2.4 KiB
C++

#include "ConfigParser.hpp"
void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
{
std::vector<ServerConfig>::iterator it = servers->begin();
while (it != servers->end())
{
// host and port are Mandatory
if (it->host == "")
throw std::invalid_argument("Config file needs a host and port");
// root is mandatory
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?
if (it->index.empty())
throw std::invalid_argument("Config file needs an Index");
// 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;
tmp.path = "/";
tmp.root = it->root;
tmp.index = it->index;
tmp.allow_methods = ANY_METHODS;
tmp.autoindex = false;
tmp.redirect_status = 0;
it->locations.push_back(tmp);
}
std::vector<LocationConfig>::iterator it_l = it->locations.begin();
while (it_l != it->locations.end())
{
if (it_l->root == "")
it_l->root = it->root;
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?
// same for redirect status i think
// maybe do something for Cgi_info?
// std::cout << "In Post, Root + Path: " << it_l->root + it_l->path << '\n';
/* if (path_is_valid(it_l->root + it_l->path) == 0)
{
//either we throw or we erase
throw std::invalid_argument("location path is invalid");
}
*/
++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());
++it;
}
}
bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations)
{
std::vector<LocationConfig>::iterator it = locations.begin();
while (it != locations.end())
{
if (it->path.compare("/") == 0)
{
// std::cout << "in compare: " << it->path << " -- ";
return true;
}
++it;
}
return false;
}