Refactor ConfigManager and LocationConfig: comment out unused methods, improve parsing logic, and clean up includes

This commit is contained in:
whaffman 2025-09-14 21:19:22 +02:00
parent cb88b9dc13
commit 0ae78577e7
6 changed files with 27 additions and 35 deletions

View File

@ -115,9 +115,9 @@ void ConfigManager::parseConfigFile(const std::string &filePath)
file.close(); file.close();
} }
void ConfigManager::parseGlobalDeclarations(const std::string &declarations) // void ConfigManager::parseGlobalDeclarations(const std::string &declarations)
{ // {
// Placeholder for actual global declarations parsing logic // // Placeholder for actual global declarations parsing logic
std::cout << "Parsing global declarations:\n" << declarations << '\n'; // std::cout << "Parsing global declarations:\n" << declarations << '\n';
// Implement the parsing logic here // // Implement the parsing logic here
} // }

View File

@ -23,6 +23,5 @@ class ConfigManager
std::vector<ServerConfig> serverConfigs; std::vector<ServerConfig> serverConfigs;
void parseConfigFile(const std::string &filePath); void parseConfigFile(const std::string &filePath);
void parseGlobalDeclarations(const std::string &declarations); // void parseGlobalDeclarations(const std::string &declarations);
}; };

View File

@ -1,13 +1,11 @@
#include <webserv/config/LocationConfig.hpp> #include <webserv/config/LocationConfig.hpp>
#include <webserv/config/utils.hpp> #include <webserv/config/utils.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <sstream> #include <sstream>
#include <string>
LocationConfig::LocationConfig(const std::string &locationBlock) LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false)
: path(""), autoIndex(false), indexFile("")
{ {
parseLocationBlock(locationBlock); parseLocationBlock(locationBlock);
} }
@ -18,7 +16,6 @@ void LocationConfig::parseLocationBlock(const std::string &block)
std::cout << "Parsing location block:\n" << block << '\n'; std::cout << "Parsing location block:\n" << block << '\n';
// Implement the parsing logic here // Implement the parsing logic here
parseDirectives(block); parseDirectives(block);
} }
void LocationConfig::parseDirectives(const std::string &declarations) void LocationConfig::parseDirectives(const std::string &declarations)
{ {
@ -30,14 +27,14 @@ void LocationConfig::parseDirectives(const std::string &declarations)
while (std::getline(stream, line)) while (std::getline(stream, line))
{ {
std::string directive; std::string directive;
std::istringstream ss{trim(line)}; std::istringstream lineStream{trim(line)};
ss >> directive; lineStream >> directive;
if (!directive.empty()) if (!directive.empty())
{ {
std::cout << "Directive: " << directive << '\n'; std::cout << "Directive: " << directive << '\n';
// Implement the parsing logic here // Implement the parsing logic here
std::string value; std::string value;
ss >> value; lineStream >> value;
if (directive == "autoindex") if (directive == "autoindex")
{ {
autoIndex = (value == "on"); autoIndex = (value == "on");

View File

@ -1,25 +1,22 @@
#include <webserv/config/ServerConfig.hpp>
#include <webserv/config/LocationConfig.hpp> #include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp>
#include <webserv/config/utils.hpp> #include <webserv/config/utils.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <sstream> #include <sstream>
#include <string>
ServerConfig::ServerConfig(std::string const &serverBlock) ServerConfig::ServerConfig(std::string const &serverBlock) : port(80)
: host(""), port(80), root("")
{ {
parseServerBlock(serverBlock); parseServerBlock(serverBlock);
} }
void ServerConfig::parseServerBlock(const std::string &block) void ServerConfig::parseServerBlock(const std::string &block)
{ {
// Placeholder for actual server block parsing logic // Placeholder for actual server block parsing logic
std::cout << "Parsing server block:\n"; std::cout << "Parsing server block:\n";
// Placeholder for actual file parsing logic // Placeholder for actual file parsing logic
std::string serverDeclarations; std::string serverDeclarations;
@ -35,7 +32,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
serverDeclarations += block.substr(pos); serverDeclarations += block.substr(pos);
break; break;
} }
std::string locationPath = trim(block.substr(locationPos, bracePos - (locationPos ))); std::string locationPath = trim(block.substr(locationPos, bracePos - (locationPos)));
// Add global declarations before this server block // Add global declarations before this server block
serverDeclarations += block.substr(pos, locationPos - pos); serverDeclarations += block.substr(pos, locationPos - pos);
size_t closeBrace = findCorrespondingClosingBrace(block, bracePos); size_t closeBrace = findCorrespondingClosingBrace(block, bracePos);
@ -62,14 +59,14 @@ void ServerConfig::parseDirectives(const std::string &declarations)
while (std::getline(stream, line)) while (std::getline(stream, line))
{ {
std::string directive; std::string directive;
std::istringstream ss{trim(line)}; std::istringstream lineStream{trim(line)};
ss >> directive; lineStream >> directive;
if (!directive.empty()) if (!directive.empty())
{ {
std::cout << "Directive: " << directive << '\n'; std::cout << "Directive: " << directive << '\n';
// Implement the parsing logic here // Implement the parsing logic here
std::string value; std::string value;
ss >> value; lineStream >> value;
if (directive == "listen") if (directive == "listen")
{ {
@ -95,16 +92,16 @@ void ServerConfig::parseDirectives(const std::string &declarations)
cgi_pass = value; cgi_pass = value;
std::cout << "Set cgi_pass to " << cgi_pass << '\n'; std::cout << "Set cgi_pass to " << cgi_pass << '\n';
} }
else if(directive =="cgi_ext") else if (directive == "cgi_ext")
{ {
cgi_ext = value; cgi_ext = value;
std::cout << "Set cgi_ext to " << cgi_ext << '\n'; std::cout << "Set cgi_ext to " << cgi_ext << '\n';
} }
else if(directive == "index") else if (directive == "index")
{ {
index_files.clear(); index_files.clear();
std::string indexFile; std::string indexFile;
while (ss >> indexFile) while (lineStream >> indexFile)
{ {
index_files.push_back(indexFile); index_files.push_back(indexFile);
std::cout << "Added index file: " << indexFile << '\n'; std::cout << "Added index file: " << indexFile << '\n';
@ -114,7 +111,7 @@ void ServerConfig::parseDirectives(const std::string &declarations)
{ {
int statusCode = std::stoi(value); int statusCode = std::stoi(value);
std::string errorPagePath; std::string errorPagePath;
ss >> errorPagePath; lineStream >> errorPagePath;
error_page[statusCode] = errorPagePath; error_page[statusCode] = errorPagePath;
std::cout << "Set error_page for status " << statusCode << " to " << errorPagePath << '\n'; std::cout << "Set error_page for status " << statusCode << " to " << errorPagePath << '\n';
} }

View File

@ -2,7 +2,6 @@
#include <string> #include <string>
std::string trim(const std::string &str) std::string trim(const std::string &str)
{ {
size_t first = str.find_first_not_of(" \t\n\r"); size_t first = str.find_first_not_of(" \t\n\r");

View File

@ -8,10 +8,10 @@ int main(int argc, char **argv)
if (argc < 2) if (argc < 2)
{ {
std::cerr << "Usage: " << argv[0] << " <config_file_path>\n"; std::cerr << "Usage: " << argv[0] << " <config_file_path>\n"; //NOLINT
return 1; return 1;
} }
ConfigManager::getInstance().init(argv[1]); ConfigManager::getInstance().init(argv[1]); //NOLINT
return 0; return 0;
} }