Files
42_INT_12_webserv/srcs/config/postProcessing.cpp

118 lines
2.9 KiB
C++

#include "ConfigParser.hpp"
void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
{
// make certain servers default
// fill out empty settings
// if special settings are empty throw
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?
// autoindex is False by Default
// if Allow methodes not specified we set to ALL
if (it->allow_methods == UNKNOWN) // in this case that means nothing...
it->allow_methods = ALL_METHODS;
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.client_body_limit = 5000; // figur out correct amount
tmp.root = it->root;
tmp.index = it->index;
tmp.allow_methods = it->allow_methods;
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->client_body_limit == 0)
it_l->client_body_limit = 5000; // what is the recomended size?
if (it_l->root == "")
it_l->root = it->root;
// if Allow methodes not specified we set to Server methods
if (it_l->allow_methods == UNKNOWN) // in this case that means nothing...
it_l->allow_methods = it->allow_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;
}