Skip to content

Commit

Permalink
feat(ConfigParser): implemented default server
Browse files Browse the repository at this point in the history
- removed index file checking
- changed serverConfig to just server
- now checking for default server
  • Loading branch information
Taanviir committed May 2, 2024
1 parent 3d4287b commit e04a7e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
49 changes: 32 additions & 17 deletions sources/parser/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ void ConfigParser::parse_index(string& indexFile, const string& root)
indexFile = root + *_itr;
else
indexFile = root + "/" + *_itr;
if (get_file_type(indexFile) != FILE)
THROW_EXCEPTION_WITH_INFO(ERR_INVALID_INDEX);
//! removed index file checking

check_semicolon();
}

fd ConfigParser::parse_listen(in_addr_t& host)
fd ConfigParser::parse_listen(in_addr_t& host, bool& defaultServer)
{
++_itr; // move to host:port

Expand Down Expand Up @@ -186,6 +185,11 @@ fd ConfigParser::parse_listen(in_addr_t& host)
THROW_EXCEPTION_WITH_INFO(ERR_PORT);
}

if (*(_itr + 1) == "default_server") {
defaultServer = true;
++_itr; // move to default_server
}

check_semicolon();

return port;
Expand All @@ -204,7 +208,7 @@ void ConfigParser::parse_root(string& root)
{
++_itr; // move to root path
root = *_itr;
if (get_file_type(root) != DIR)
if (get_file_type(root) != DIR) //!
THROW_EXCEPTION_WITH_INFO(ERR_ROOT);
check_semicolon();
}
Expand Down Expand Up @@ -314,7 +318,7 @@ void ConfigParser::parse_location_context(ServerConfig& server)
else if (*_itr == "allow_methods")
parse_allow_methods(location.methods);
else if (*_itr == "index") {
if (get_file_type(location.root + location.uri) != DIR)
if (get_file_type(location.root + location.uri) != DIR) //!
THROW_EXCEPTION_WITH_INFO(ERR_LOCATION_INDEX);
parse_index(location.indexFile, location.root + location.uri);
}
Expand Down Expand Up @@ -344,53 +348,64 @@ ServerConfig ConfigParser::parse_server_context(void)
++_itr; // move to {

set<string> parsedDirectives;
ServerConfig serverConfig;
ServerConfig server;
while (*_itr != "}") {
if (*_itr != ";" && *_itr != "}" && *_itr != "error_page" && *_itr != "location")
check_duplicate_element(parsedDirectives, *_itr, "server");
if (*_itr == "listen")
serverConfig.port = parse_listen(serverConfig.host);
server.port = parse_listen(server.host, server.defaultServer);
else if (*_itr == "root")
parse_root(serverConfig.root);
parse_root(server.root);
else if (*_itr == "client_max_body_size")
serverConfig.maxBodySize = parse_client_max_body_size();
server.maxBodySize = parse_client_max_body_size();
else if (*_itr == "autoindex")
serverConfig.autoindex = parse_autoindex();
server.autoindex = parse_autoindex();
else if (*_itr == "index")
parse_index(serverConfig.indexFile, serverConfig.root);
parse_index(server.indexFile, server.root);
else if (*_itr == "server_name")
parse_server_name(serverConfig.serverName);
parse_server_name(server.serverName);
else if (*_itr == "error_page")
parse_error_page(serverConfig.errorPages, serverConfig.root);
parse_error_page(server.errorPages, server.root);
else if (*_itr == "location")
parse_location_context(serverConfig);
parse_location_context(server);
else if (*_itr == ";" || *_itr == "{")
++_itr;
else
THROW_EXCEPTION_WITH_INFO(ERR_SERVER_TOKENS);
}

if (serverConfig.locations.empty())
if (server.locations.empty())
THROW_EXCEPTION_WITH_INFO(ERR_MISSING_LOCATION);

++_itr; // move to next context

return serverConfig;
return server;
}

vector<ServerConfig> ConfigParser::parse(void)
{
_itr = _tokens.begin(); // setting iterator to the first token

vector<ServerConfig> servers;
while (_itr != _tokens.end())
bool foundDefaultServer = false;
while (_itr != _tokens.end()) {
if (*_itr == "server")
servers.push_back(parse_server_context());
else
THROW_EXCEPTION_WITH_INFO(ERR_TOKENS);

if (servers[servers.size() - 1].defaultServer == true) {
if (foundDefaultServer == true)
THROW_EXCEPTION_WITH_INFO(ERR_MULTIPLE_DEFAULT);
foundDefaultServer = true;
}
}

if (servers.empty())
THROW_EXCEPTION_WITH_INFO(ERR_MISSING_SERVER);

if (foundDefaultServer == false)
servers[0].defaultServer = true;

return servers;
}
16 changes: 8 additions & 8 deletions sources/parser/ConfigParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ static const string ERR_MISSING_CONTEXT("Config: missing context");

// global context error messages
static const string ERR_TOKENS("Config: Unexpected tokens found in the global context");
static const string ERR_MULTIPLE_DEFAULT("Config: multiple default servers found");

// server context error messages
static const string ERR_MISSING_SERVER("Config: missing server context");
static const string ERR_SERVER_TOKENS("Config: Unexpected tokens in the server context");

// location context error messages
static const string ERR_LOCATION("Config: invalid character in location uri");
static const string ERR_MISSING_URI("Config: location URI missing");
static const string ERR_URI_MISSING_SLASH("Config: location uri is missing a / in the beginning");
static const string ERR_URI_DUPLICATE_SLASH("Config: location uri contains multiple /");
static const string ERR_URI_MISSING_SLASH("Config: URI is missing / in the beginning");
static const string ERR_URI_DUPLICATE_SLASH("Config: location URI contains multiple /");
static const string ERR_LOCATION_TOKENS("Config: Unexpected tokens in location context");
static const string ERR_DUPLICATE_LOCATION("Config: duplicate location URI");
static const string ERR_MISSING_LOCATION("Config: location context missing");
Expand All @@ -62,7 +61,6 @@ static const string ERR_MISSING_ROOT("Config: missing root directive");

// index directive error messages
static const string ERR_INDEX("Config: invalid index directive");
static const string ERR_INVALID_INDEX("Config: invalid index file");

// client_max_body_size directive error messages
static const string ERR_INVALID_CHAR("Config: invalid character in client_max_body_size");
Expand All @@ -84,8 +82,10 @@ static const string ERR_EMPTY_METHODS("Config: no allowed methods found");
#define MAX_PORT 65535

// TODO:
// [ ] check for duplicate error pages
// [ ] handle cgi related directives
// check for duplicate error pages
// handle cgi related directives
// parse error codes and pages better
// checking if root and root+index is valid or not

#define NUM_KEYWORDS 15

Expand Down Expand Up @@ -116,7 +116,7 @@ class ConfigParser {
// parsing config directives
void parse_index(string& indexFile, const string& root);
void parse_error_page(map<STATUS_CODE, string>& errorPages, const string& root);
fd parse_listen(in_addr_t& host);
fd parse_listen(in_addr_t& host, bool& defaultServer);
void parse_server_name(string& serverName);
void parse_root(string& root);
size_t parse_client_max_body_size(void);
Expand Down

0 comments on commit e04a7e0

Please sign in to comment.