Files
42_INT_12_webserv/srcs/config/postProcessing.cpp

207 lines
4.7 KiB
C++

#include "ConfigParser.hpp"
// technically doesn't belong to ConfigParser or any other class
// adding static in front doesn't work...
/*
bool compareLocationConfigs(const LocationConfig &a, const LocationConfig &b)
{
int len_a;
int len_b;
size_t tmp = 0;
// consider adding 1 to path that ends in a file not folder.
while ((tmp = a.path.find_first_of("/", tmp)) != std::string::npos)
++len_a;
tmp = 0;
while ((tmp = b.path.find_first_of("/", tmp)) != std::string::npos)
++len_b;
return (len_a < len_b); // right comparison ? not <= ?
}
*/
/*
class spec_int
{
public:
spec_int() {}
spec_int(int i): i(i){}
~spec_int() {}
// spec_int(int start, int num)
// {
// }
int i;
bool operator<(const spec_int &a, const spec_int &b)
{
return (a.i < b.i);
}
}
*/
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();
// first check locations we have
while (it_l != it->locations.end())
{
// opendir() doesn't work for some reason...
// this doens't work yet cuz the path needs to be relative and stat doesn't like / before...
/* const char* folder = it_l->path.c_str();
struct stat sb;
if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode))
{
// std::cout << "is a Dir\n";
// it_l->path = it_lpath;
// yea nothing happens, i guess i can change how the if conditions work...
}
else
throw std::invalid_argument("location dir could not be opened");
*/
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?
++it_l;
}
// Then put locations in order...
// may change how the order is set later
// ok we can sort in order and reverse...
std::cout << "made it to sorting...\n";
// std::sort(it->locations.begin(), it->locations.end(), compareLocationConfigs);
std::sort(it->locations.begin(), it->locations.end());
// for some reason no need to reverse...
// std::reverse(it->locations.begin(), it->locations.end());
/*
// let's test sort
int myints[] = {32,71,12,45,26,80,53,33};
// std::vector<spec_int> myvector (myints, myints+8);
// std::vector<spec_int> myvector(10, 5);
std::vector<spec_int> myvector;
myvector.resize(6);
myvector[0].i = 32;
myvector[1].i = 71;
myvector[2].i = 12;
myvector[3].i = 45;
myvector[4].i = 26;
myvector[5].i = 80;
std::sort(myvector.begin(), myvector.end());
std::cout << "myvector contains:";
// for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
for (std::vector<spec_int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
*/
++it;
}
// do the defaults at the end?
}
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;
}