Files
42_INT_12_webserv/srcs/config/postProcessing.cpp
2022-08-15 19:54:26 +02:00

95 lines
2.1 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");
// 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 = 1 * MB;
// if error_pages is left empty, we'll use the defaults which
// i believe are set elsewhere...
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->path[it_l->path.size() - 1] == '/'
&& it_l->path.size() > 1)
it_l->path.erase(it_l->path.size() - 1);
std::vector<LocationConfig>::const_iterator tmp = it_l + 1;
while (tmp != it->locations.end())
{
if (it_l->path == tmp->path)
throw std::invalid_argument("Duplicate locations in config file");
++tmp;
}
if (it_l->root == "")
it_l->root = it->root;
if (it_l->allow_methods == UNKNOWN)
it_l->allow_methods = ANY_METHODS;
if (it_l->index.empty() && it_l->autoindex == false)
it_l->index = it->index;
// nothing to be done for cgi_ext, error_pages, redirect
++it_l;
}
std::sort(it->locations.begin(), it->locations.end());
std::reverse(it->locations.begin(), it->locations.end());
++it;
}
}
// const?
bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations) const
{
std::vector<LocationConfig>::const_iterator it = locations.begin();
while (it != locations.end())
{
if (it->path.compare("/") == 0)
return true;
++it;
}
return false;
}