From e04a7e07e86ee4bbd705905c417e6e09db7e77a3 Mon Sep 17 00:00:00 2001 From: Taanviir <66136914+Taanviir@users.noreply.github.com> Date: Fri, 3 May 2024 01:31:48 +0400 Subject: [PATCH] feat(ConfigParser): implemented default server - removed index file checking - changed serverConfig to just server - now checking for default server --- sources/parser/ConfigParser.cpp | 49 +++++++++++++++++++++------------ sources/parser/ConfigParser.hpp | 16 +++++------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/sources/parser/ConfigParser.cpp b/sources/parser/ConfigParser.cpp index 6a00e81..5dda014 100644 --- a/sources/parser/ConfigParser.cpp +++ b/sources/parser/ConfigParser.cpp @@ -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 @@ -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; @@ -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(); } @@ -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); } @@ -344,38 +348,38 @@ ServerConfig ConfigParser::parse_server_context(void) ++_itr; // move to { set 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 ConfigParser::parse(void) @@ -383,14 +387,25 @@ vector ConfigParser::parse(void) _itr = _tokens.begin(); // setting iterator to the first token vector 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; } diff --git a/sources/parser/ConfigParser.hpp b/sources/parser/ConfigParser.hpp index a680198..2758d81 100644 --- a/sources/parser/ConfigParser.hpp +++ b/sources/parser/ConfigParser.hpp @@ -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"); @@ -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"); @@ -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 @@ -116,7 +116,7 @@ class ConfigParser { // parsing config directives void parse_index(string& indexFile, const string& root); void parse_error_page(map& 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);