cleaned things up

This commit is contained in:
Eric LAZO
2022-08-15 19:40:21 +02:00
parent aa03880601
commit 19da59ecf8
7 changed files with 44 additions and 61 deletions

View File

@@ -2,16 +2,14 @@
#include "ConfigParser.hpp"
// Default
// possibly remove...
ConfigParser::ConfigParser()
{
std::cout << "Default Constructor\n";
// don't use yet, you have no idea what the defaults are
}
ConfigParser::ConfigParser(const char* path)
{
// std::cout << "Param Constructor\n";
std::ifstream file;
std::string buf;
size_t comment;
@@ -43,21 +41,12 @@ ConfigParser::ConfigParser(const char* path)
throw std::invalid_argument("failed to open config");
}
// might not need this...
ConfigParser::~ConfigParser()
{
// do i need to destroy anything, won't it handle itself?
}
/*
ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
{
if (this == rhs) // * & ?
return (*this); // * ?
// make some stuff equal
return (*this);
}
*/
// const?
std::vector<ServerConfig> * ConfigParser::parse()
@@ -78,7 +67,7 @@ std::vector<ServerConfig> * ConfigParser::parse()
throw std::invalid_argument("empty config file");
std::string key = _content.substr(start, curr - start);
if (key != "server")
throw std::invalid_argument("bad config file arguments 1");
throw std::invalid_argument("bad config file arguments");
ret->push_back(_parse_server(&curr));
}
_post_processing(ret);
@@ -92,7 +81,7 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
ret.client_body_limit = 0;
if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 1");
throw std::invalid_argument("bad config file syntax");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax");
@@ -135,13 +124,12 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
ret.path = _get_first_word(&curr);
if (ret.path[0] != '/')
throw std::invalid_argument("Location path require a leading /");
// ret.path.insert(0, "/");
// in theory now curr should be right after the "path"
curr = _content.find_first_not_of(" \t\n", curr);
if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 2");
throw std::invalid_argument("bad config file syntax");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax");
@@ -181,15 +169,18 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
throw std::invalid_argument("missing value");
else if (key == "server_name" && server->server_name.empty())
{
for (std::vector<std::string>::iterator it = server->server_name.begin(); \
it < server->server_name.end(); it++)
for (size_t i = 0; i < size; i++)
{
if (it->compare(tmp_val[0]) == 0)
throw std::invalid_argument("server_name already exists");
for (std::vector<std::string>::const_iterator it = server->server_name.begin();
it < server->server_name.end(); it++)
{
if (it->compare(tmp_val[i]) == 0)
throw std::invalid_argument("server_name already exists");
}
server->server_name.push_back(tmp_val[i]);
}
server->server_name.push_back(tmp_val[0]);
}
else if (key == "listen" && size == 1 && server->host == "" \
else if (key == "listen" && size == 1 && server->host == ""
&& server->port == "")
{
if (tmp_val[0].find_first_of(":") == NPOS)
@@ -225,7 +216,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
// tmp_val[0].push_back('/');
server->root = tmp_val[0];
}
else if (key == "client_body_limit" && size == 1 \
else if (key == "client_body_limit" && size == 1
&& server->client_body_limit == 0)
{
if (!::isNumeric(tmp_val[0]))
@@ -234,13 +225,13 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
}
else if (key == "index")
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
for (size_t i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]);
}
else if (key == "error_page")
{
std::string path = tmp_val[tmp_val.size() - 1];
for (unsigned long i = 0; i != tmp_val.size() - 1; i++)
std::string path = tmp_val[size - 1];
for (size_t i = 0; i < size() - 1; i++)
{
if (!(isNumeric_btw(400, 599, tmp_val[i])))
throw std::invalid_argument("invalid error code");
@@ -279,12 +270,12 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
location->autoindex = (tmp_val[0] == "on" ? true : false);
else if (key == "index")
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
for (size_t i = 0; i < size; i++)
location->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods" && location->allow_methods == 0)
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
for (size_t i = 0; i < size; i++)
{
http_method m = ::str_to_http_method(tmp_val[i]);
if (m == UNKNOWN)
@@ -294,23 +285,22 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
}
else if (key == "cgi_ext")
{
for (size_t i = 0; i < tmp_val.size(); i++)
for (size_t i = 0; i < size; i++)
{
if (tmp_val[i][0] == '.')
throw std::invalid_argument("cgi_ext should not have a leading '.'");
location->cgi_ext.push_back(tmp_val[i]);
}
}
else if (key == "redirect" && location->redirect_status == 0 \
else if (key == "redirect" && location->redirect_status == 0
&& location->redirect_uri == "")
{
if (tmp_val.size() != 2)
if (size != 2)
throw std::invalid_argument("wrong number of values");
if (tmp_val[0] != "301" && tmp_val[0] != "302"
&& tmp_val[0] != "303" && tmp_val[0] != "307"
&& tmp_val[0] != "308")
throw std::invalid_argument("bad redirect status");
// std::cout << tmp_val[1] << '\n';
if (tmp_val[1].compare(0, 7, "http://")
&& tmp_val[1].compare(0, 8, "https://"))
throw std::invalid_argument("bad redirect uri");