ok, so we added a lot more checks for the config file, and some post processing, still needs a few more things, but now some defaults are set too, basically it works under most circumstances

This commit is contained in:
Me
2022-08-01 00:49:25 +02:00
parent 19f7493aac
commit 16af16084b
8 changed files with 245 additions and 135 deletions

View File

@@ -12,10 +12,6 @@
#include "ConfigParser.hpp"
// Default
ConfigParser::ConfigParser()
{
@@ -98,6 +94,7 @@ std::vector<ServerConfig> * ConfigParser::parse()
throw std::invalid_argument("bad config file arguments 1");
ret->push_back(_parse_server(&curr));
}
_post_processing(ret);
return (ret);
}
@@ -106,6 +103,8 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
ServerConfig ret;
size_t curr = _content.find_first_not_of(" \t\n", *start);
ret.client_body_limit = 0;
ret.autoindex = false;
if (curr == std::string::npos || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 1");
@@ -143,6 +142,8 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
size_t curr = *start;
// start is after the 1st word aka "location"
ret.client_body_limit = 0;
ret.redirect_status = 0;
ret.path = _get_first_word(&curr);
// in theory now curr should be right after the "path"
@@ -181,39 +182,6 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
void ConfigParser::_set_server_values(ServerConfig *server, \
const std::string key, std::string value)
{
/*
// check key for ;
// check values for ; at end and right number of words depending on key
if (key.find_first_of(";") != std::string::npos)
throw std::invalid_argument("bad config file arguments 2");
// there shouldn't be any tabs, right? not between values...
if (value.find_first_of("\t") != std::string::npos)
{
std::cout << value << "\n";
throw std::invalid_argument("bad config file arguments 3");
}
size_t i = value.find_first_of(";");
// so you can't have no ;
// you can't have just ;
// and you can't have a ; not at the end or several ;
// in theory value_find_last_of should find the only ;
if (i == std::string::npos || (value.find_last_not_of(" \n")) != i \
|| value.compare(";") == 0)
throw std::invalid_argument("bad config file arguments 4");
// we Trim value.
// is this valid?
// would it be better to shove the result directly in tmp_val?
// like call substr in split?
//value = value.substr(0, i - 1);
value = value.substr(0, i);
*/
value = _pre_set_val_check(key, value);
std::vector<std::string> tmp_val = ::split(value, ' ');
@@ -223,69 +191,75 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
throw std::invalid_argument("missing value");
else if (key == "server_name" && size == 1)
{
// should i be checking if the field is already filled
server->server_name = tmp_val[0];
for (size_t i = 0; i < server->server_name.size(); i++)
{
if (server->server_name[i].compare(tmp_val[0]) == 0)
throw std::invalid_argument("server_name already exists");
}
server->server_name.push_back(tmp_val[0]);
}
else if (key == "listen" && size == 1)
else if (key == "listen" && size == 1 && server->host == "" \
&& server->port == "")
{
// should i be checking if field already filled?
// are we saying only 1 possible?
if (tmp_val[0].find_first_of(":") == std::string::npos)
{
if (!(isNumeric(tmp_val[0])))
throw std::invalid_argument("value not a number");
// should i limit which ports can be used?
if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("bad port number");
server->host = "0.0.0.0";
server->port = tmp_val[0];
}
else
{
std::vector<std::string> tmp2 = ::split(tmp_val[0], ':');
if (!(::isNumeric(tmp2[1])))
throw std::invalid_argument("value not a number");
// not sure if this is what we want, means there's only 1 host per
// server...
if (server->host != "" && server->host != tmp2[0])
throw std::invalid_argument("bad listen");
std::vector<std::string> ip = ::split(tmp2[0], '.');
if (ip.size() != 4)
throw std::invalid_argument("bad host ip");
for (size_t i = 0; i < ip.size(); i++)
{
if (!::isNumeric_btw(0, 255, ip[i]))
throw std::invalid_argument("bad host ip");
}
if (!::isNumeric(tmp2[1]))
throw std::invalid_argument("bad port number");
server->host = tmp2[0];
server->port = tmp2[1];
}
}
else if (key == "root" && size == 1)
else if (key == "root" && size == 1 && server->root == "")
{
DIR* dir = opendir(tmp_val[0].c_str());
if (dir)
closedir(dir);
else
throw std::invalid_argument("root dir could not be opened");
server->root = tmp_val[0];
}
else if (key == "autoindex" && size == 1)
{
// autoindex is a bool, there's no good way for me to see if it has
// bet set already
server->autoindex = (tmp_val[0] == "on" ? true : false);
}
else if (key == "client_body_limit" && size == 1)
else if (key == "client_body_limit" && size == 1 \
&& server->client_body_limit == 0)
{
//std::cout << "made it\n";
if (!(::isNumeric(tmp_val[0])))
throw std::invalid_argument("value not a number");
if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("client_body_limit not a number");
server->client_body_limit = atoi(tmp_val[0].c_str());
}
else if (key == "recv_timeout" && size == 1)
{
// what is tv_sec and do i need it?
// ok so i don't fully understand this part but ok, keep for now...
server->recv_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
else if (key == "send_timeout" && size == 1)
{
server->send_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
else if (key == "index")
{
// i think you can call index several times...
// should i be doing an access?
// since index is at the root, but root might not yet be defined
// will check index later in post
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods")
else if (key == "allow_methods" && server->allow_methods.empty())
{
// you need to throw if it's a bad method type
// or should we skip? and see if any others are good?
for (unsigned long i = 0; i != tmp_val.size(); i++)
{
MethodType m = _str_to_method_type(tmp_val[i]);
@@ -294,22 +268,16 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
server->allow_methods.push_back(m);
}
}
else if (key == "return")
{
if (tmp_val.size() != 2)
throw std::invalid_argument("wrong number of values");
// and tmp_val[0] should be a number and tmp_val[1] a string?
if (!(::isNumeric(tmp_val[0])))
throw std::invalid_argument("value not a number");
// something about using access() to see if
server->redirect_status = atoi(tmp_val[0].c_str());
server->redirect_uri = tmp_val[1];
}
else if (key == "error_page")
{
// something more complicated?
// like make sure ints then 1 string?
// so it can either be just a /here/is/the/repo
// or it can be http://some_domain.com/here
// wtf... how should we handle...
// you can definitely call Error_pages several times, i think
std::string path = tmp_val[tmp_val.size() - 1];
for (unsigned long i = 0; i != tmp_val.size() - 1; i++)
{
@@ -324,24 +292,25 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
server->error_pages[status_code] = path;
}
}
else
/* else if (key == "recv_timeout" && size == 1 && server->server_name == "")
{
throw std::invalid_argument("wrong number of values");
// what is tv_sec and do i need it?
// ok so i don't fully understand this part but ok, keep for now...
server->recv_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
else if (key == "send_timeout" && size == 1 && server->server_name == "")
{
server->send_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
*/ else
{
// means either you didn't write the right key, or the value is
// missing, or the value has already been filled.
throw std::invalid_argument("bad key value pair");
}
// std::cout << "End set\n";
}
void ConfigParser::_set_location_values(LocationConfig *location, \
const std::string key, std::string value)
{
@@ -352,26 +321,41 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
if (size < 1)
throw std::invalid_argument("missing value");
else if (key == "root" && size == 1)
else if (key == "root" && size == 1 && location->root == "")
{
DIR* dir = opendir(tmp_val[0].c_str());
if (dir)
closedir(dir);
else
throw std::invalid_argument("root dir could not be opened");
location->root = tmp_val[0];
}
else if (key == "client_body_limit" && size == 1)
else if (key == "client_body_limit" && size == 1 \
&& location->client_body_limit == 0)
{
if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("client_body_limit not a number");
location->client_body_limit = atoi(tmp_val[0].c_str());
}
else if (key == "index")
{
// you can definitely call Index several times, i think
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods")
else if (key == "allow_methods" && location->allow_methods.empty())
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->allow_methods.push_back(_str_to_method_type(tmp_val[i]));
{
MethodType m = _str_to_method_type(tmp_val[i]);
if (m == 3)
throw std::invalid_argument("not a valid method");
location->allow_methods.push_back(m);
}
}
else if (key == "cgi_info")
{
// you can call cgi_info several times i think.
// ok wtf is all this even doing, figure that out
unsigned long i = value.find_first_of(" ");
if (i == std::string::npos)
@@ -380,9 +364,27 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
int j = value.find_first_not_of(" ", i);
location->cgi_info[value.substr(0, i)] = value.substr(j, value.length());
}
else if (key == "return" && location->redirect_status == 0 \
&& location->redirect_uri == "")
{
// actually i think there can only be one per location...
// you can definitely call return several times, i think
if (tmp_val.size() != 2)
throw std::invalid_argument("wrong number of values");
// and tmp_val[0] should be a number and tmp_val[1] a string?
if (!(::isNumeric(tmp_val[0])))
throw std::invalid_argument("value not a number");
// somehow check that tmp_val[1] is a string? or valid? how?
// something about using access() to see if
location->redirect_status = atoi(tmp_val[0].c_str());
location->redirect_uri = tmp_val[1];
}
else
{
throw std::invalid_argument("bad config file arguments 9");
// means either you didn't write the right key, or the value is
// missing, or the value has already been filled.
throw std::invalid_argument("bad key value pair");
}
}