diff --git a/config/default.conf b/config/default.conf index 7fc3642..9c3e470 100644 --- a/config/default.conf +++ b/config/default.conf @@ -38,7 +38,7 @@ server { cgi_pass /cgi-bin/; cgi_ext .py .php; } - +asdfasdfasdf server { listen 80; server_name mylocal; diff --git a/webserv/config/ConfigManager.hpp b/webserv/config/ConfigManager.hpp index 0014f88..3938b15 100644 --- a/webserv/config/ConfigManager.hpp +++ b/webserv/config/ConfigManager.hpp @@ -15,6 +15,7 @@ class ConfigManager void init(const std::string &filePath); static ConfigManager &getInstance(); + const std::vector &getServerConfigs() const { return serverConfigs; } private: bool _initialized; diff --git a/webserv/config/LocationConfig.cpp b/webserv/config/LocationConfig.cpp index 7751c73..20d3f45 100644 --- a/webserv/config/LocationConfig.cpp +++ b/webserv/config/LocationConfig.cpp @@ -5,23 +5,17 @@ #include #include -LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false) +LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false) { parseLocationBlock(locationBlock); } void LocationConfig::parseLocationBlock(const std::string &block) { - // Placeholder for actual location block parsing logic - std::cout << "Parsing location block:\n" << block << '\n'; - // Implement the parsing logic here parseDirectives(block); } void LocationConfig::parseDirectives(const std::string &declarations) { - // Placeholder for actual directives parsing logic - std::cout << "Parsing location directives:\n" << declarations << '\n'; - // Implement the parsing logic here std::istringstream stream(declarations); std::string line; while (std::getline(stream, line)) @@ -31,8 +25,6 @@ void LocationConfig::parseDirectives(const std::string &declarations) lineStream >> directive; if (!directive.empty()) { - std::cout << "Directive: " << directive << '\n'; - // Implement the parsing logic here std::string value; lineStream >> value; if (directive == "autoindex") diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index 25e3410..1d47f21 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -41,8 +41,9 @@ void ServerConfig::parseServerBlock(const std::string &block) throw std::runtime_error("Malformed block in config file."); } // Optionally parse the server block here - std::string serverBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1); - locations.emplace(locationPath, LocationConfig(serverBlock)); + std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1); + std::cout << "Added location: " << locationPath << '\n'; + locations.emplace(locationPath, LocationConfig(locationBlock)); pos = closeBrace + 1; } @@ -52,8 +53,7 @@ void ServerConfig::parseServerBlock(const std::string &block) void ServerConfig::parseDirectives(const std::string &declarations) { - // Placeholder for actual directives parsing logic - std::cout << "Parsing directives:\n" << declarations << '\n'; + std::cout << "Parsing server directives:\n"; std::string line; std::istringstream stream(declarations); while (std::getline(stream, line)) @@ -63,8 +63,6 @@ void ServerConfig::parseDirectives(const std::string &declarations) lineStream >> directive; if (!directive.empty()) { - std::cout << "Directive: " << directive << '\n'; - // Implement the parsing logic here std::string value; lineStream >> value; if (directive == "listen") @@ -115,6 +113,30 @@ void ServerConfig::parseDirectives(const std::string &declarations) error_page[statusCode] = errorPagePath; std::cout << "Set error_page for status " << statusCode << " to " << errorPagePath << '\n'; } + else + { + std::cout << "Unknown directive: " << directive << '\n'; + } } } +} + +const LocationConfig &ServerConfig::getLocation(const std::string &path) const +{ + if (locations.count(path) > 0 ) // NOLINT + { + return locations.at(path); + } + throw std::runtime_error("Location not found: " + path); +} + +std::vector ServerConfig::getLocationPaths() const +{ + std::vector paths; + paths.reserve(locations.size()); + for (const auto &pair : locations) + { + paths.push_back(pair.first); + } + return paths; } \ No newline at end of file diff --git a/webserv/config/ServerConfig.hpp b/webserv/config/ServerConfig.hpp index 64b3864..292ee04 100644 --- a/webserv/config/ServerConfig.hpp +++ b/webserv/config/ServerConfig.hpp @@ -11,7 +11,15 @@ class ServerConfig public: ServerConfig(const std::string &serverBlock); - [[nodiscard]] LocationConfig getLocation(const std::string &path) const; + [[nodiscard]] const std::string &getHost() const { return host; } + [[nodiscard]] int getPort() const { return port; } + [[nodiscard]] const std::string &getRoot() const { return root; } + [[nodiscard]] const std::string &getCgiPass() const { return cgi_pass; } + [[nodiscard]] const std::string &getCgiExt() const { return cgi_ext; } + [[nodiscard]] const std::map &getErrorPages() const { return error_page; } + [[nodiscard]] const std::vector &getIndexFiles() const { return index_files; } + [[nodiscard]] const LocationConfig &getLocation(const std::string &path) const; + [[nodiscard]] std::vector getLocationPaths() const; private: std::string host; diff --git a/webserv/main.cpp b/webserv/main.cpp index 0410462..1cf7d52 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include @@ -8,10 +10,21 @@ int main(int argc, char **argv) if (argc < 2) { - std::cerr << "Usage: " << argv[0] << " \n"; //NOLINT + std::cerr << "Usage: " << argv[0] << " \n"; // NOLINT return 1; } - ConfigManager::getInstance().init(argv[1]); //NOLINT + ConfigManager::getInstance().init(argv[1]); // NOLINT + + const auto &serverConfigs = ConfigManager::getInstance().getServerConfigs(); + for (const auto &serverConfig : serverConfigs) + { + std::cout << "Server " << serverConfig.getHost() << " listening on port: " << serverConfig.getPort() << '\n'; + + for (const auto &path : serverConfig.getLocationPaths()) + { + std::cout << " Location: " << path << '\n'; + } + } return 0; } \ No newline at end of file