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();
}
void ConfigManager::parseGlobalDeclarations(const std::string &declarations)
{
// Placeholder for actual global declarations parsing logic
std::cout << "Parsing global declarations:\n" << declarations << '\n';
// Implement the parsing logic here
}
// void ConfigManager::parseGlobalDeclarations(const std::string &declarations)
// {
// // Placeholder for actual global declarations parsing logic
// std::cout << "Parsing global declarations:\n" << declarations << '\n';
// // Implement the parsing logic here
// }

View File

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

View File

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

View File

@ -2,7 +2,6 @@
#include <string>
std::string trim(const std::string &str)
{
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)
{
std::cerr << "Usage: " << argv[0] << " <config_file_path>\n";
std::cerr << "Usage: " << argv[0] << " <config_file_path>\n"; //NOLINT
return 1;
}
ConfigManager::getInstance().init(argv[1]);
ConfigManager::getInstance().init(argv[1]); //NOLINT
return 0;
}