Skip to content

Commit

Permalink
chore(ConfigParser.cpp): cleaned up formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Taanviir committed Apr 11, 2024
1 parent 56ab3a9 commit 2f6f321
Showing 1 changed file with 43 additions and 52 deletions.
95 changes: 43 additions & 52 deletions sources/parser/ConfigParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,26 @@ static const string ERR_MISSING_SEMICOLON("Parser: missing semicolon");
static const string ERR_MISSING_CONTEXT("Parser: missing context");

static const string ERR_MISSING_HTTP("Parser: missing HTTP context");
static const string ERR_UNEXPECTED_TOKENS_IN(
"Parser: Unexpected tokens found inside the HTTP context");
static const string ERR_UNEXPECTED_TOKENS_OUT(
"Parser: Unexpected tokens found outside the HTTP context");
static const string ERR_UNEXPECTED_TOKENS_IN("Parser: Unexpected tokens found inside the HTTP context");
static const string ERR_UNEXPECTED_TOKENS_OUT("Parser: Unexpected tokens found outside the HTTP context");

static const string ERR_MISSING_SERVER("Parser: missing server context");
static const string ERR_UNEXPECTED_TOKENS_IN_SERVER(
"Parser: Unexpected tokens found inside the server context");
static const string ERR_UNEXPECTED_TOKENS_IN_SERVER("Parser: Unexpected tokens found inside the server context");
static const string ERR_INVALID_LISTEN("Parser: invalid listen directive");
static const string ERR_INVALID_SERVER_NAME("Parser: invalid server_name directive");
static const string ERR_INVALID_ROOT("Parser: invalid root directive");
static const string ERR_MISSING_ROOT("Parser: missing root directive");
static const string ERR_INVALID_INDEX("Parser: invalid index directive");
static const string ERR_INVALID_AUTOINDEX("Parser: invalid autoindex directive");
static const string ERR_INVALID_BODY_SIZE(
"Parser: invalid client_max_body_size directive");
static const string ERR_INVALID_BODY_SIZE("Parser: invalid client_max_body_size directive");

static const string ERR_ERROR_PATH("Parser: error page path missing");
static const string ERR_ERROR_CODE("Parser: error page code missing");
static const string ERR_INVALID_ERROR_PATH("Parser: invalid error page path");

static const string ERR_INVALID_LOCATION("Parser: invalid location context");
static const string ERR_LOCATION_PATH("Parser: location path missing");
static const string ERR_UNEXPECTED_TOKENS_IN_LOCATION(
"Parser: Unexpected tokens found inside the location context");
static const string ERR_UNEXPECTED_TOKENS_IN_LOCATION("Parser: Unexpected tokens found inside the location context");

#define MAX_PORT 65535

Expand All @@ -66,51 +61,47 @@ const string keywords[NUM_KEYWORDS] = {
"root", "index", "error_page", "client_max_body_size", "autoindex"
};

class ConfigParser
{
public:
ConfigParser(string const &filepath);
~ConfigParser(){};

vector<ServerConfig> parse(void);

private:
string _content;
vector<string> _tokens;
vector<string>::const_iterator _itr;

vector<ServerConfig> _parse_HTTP_context(void);
ServerConfig _parse_server_context(void);
Location _parse_location_context(void);
vector<string> _parse_index(string const &root);
void _parse_error_page(map<STATUS_CODE, string> &errorPages, string const &root);
fd _parse_listen(void);
void _parse_server_name(vector<string> &serverName);
string _parse_root(void);
string _parse_client_max_body_size(void);
bool _parse_autoindex(void);

bool _is_string_number(const string &str)
{
if (str.find_first_not_of("0123456789") != string::npos)
return false;
return true;
}
bool _is_keyword(const string &str)
{
for (size_t i = 0; i < NUM_KEYWORDS; ++i)
{
if (str == keywords[i])
return true;
}
class ConfigParser {
public:
ConfigParser(string const &filepath);
~ConfigParser() {};

vector<ServerConfig> parse(void);

private:
string _content;
vector<string> _tokens;
vector<string>::const_iterator _itr;

vector<ServerConfig> _parse_HTTP_context(void);
ServerConfig _parse_server_context(void);
Location _parse_location_context(void);
vector<string> _parse_index(string const &root);
void _parse_error_page(map<STATUS_CODE, string> &errorPages, string const &root);
fd _parse_listen(void);
void _parse_server_name(vector<string> &serverName);
string _parse_root(void);
string _parse_client_max_body_size(void);
bool _parse_autoindex(void);

bool _is_string_number(const string &str) {
if (str.find_first_not_of("0123456789") != string::npos)
return false;
}
void _check_semicolon(void)
return true;
}
bool _is_keyword(const string &str) {
for (size_t i = 0; i < NUM_KEYWORDS; ++i)
{
if (*(_itr + 1) != ";")
throw runtime_error(ERR_MISSING_SEMICOLON);
++_itr; // move to semicolon
if (str == keywords[i])
return true;
}
return false;
}
void _check_semicolon(void) {
if (*(_itr + 1) != ";")
throw runtime_error(ERR_MISSING_SEMICOLON);
++_itr; // move to semicolon
}
};

#endif // CONFIG_PARSER_HPP

0 comments on commit 2f6f321

Please sign in to comment.