diff --git a/config/default.conf b/config/default.conf index 34d4c35..43c62f1 100644 --- a/config/default.conf +++ b/config/default.conf @@ -1,17 +1,17 @@ autoindex on -error_page 400 ./error_pages/400.html -error_page 500 ./error_pages/500.html +error_page 400 ./htdocs/error_pages/400.html +error_page 500 ./htdocs/error_pages/500.html server { listen 8080; host 0.0.0.0; server_name localhost; - root ./www/; + root ./htdocs/site-1/; index index.html index.htm; - error_page 400 /400.html; - error_page 404 ./error_pages/404.html; + error_page 400 ./htdocs/error_pages/400.html; + error_page 404 ./htdocs/error_pages/404.html; error_page 403 /403.html; error_page 404 /404.html; error_page 405 /405.html; @@ -24,7 +24,7 @@ server { location / { autoindex off; - root ./static_site/; + root ./htdocs/site-1/; index index2.html index.html; allowed_methods GET POST DELETE; } @@ -36,7 +36,7 @@ server { # } location /images { - root ./static_site/img; + root ./htdocs/site-1/img; autoindex off; index index.jpg; allowed_methods GET; @@ -51,7 +51,7 @@ server { host 127.0.0.1; server_name localhost; - root ./www; + root ./htdocs/site-2/; index index.html index2.htm; error_page 400; @@ -67,7 +67,7 @@ server { location / { autoindex off; - root www; + root ./htdocs/site-2/; index index.html; allowed_methods GET POST DELETE; } diff --git a/error_pages/400.html b/htdocs/error_pages/400.html similarity index 100% rename from error_pages/400.html rename to htdocs/error_pages/400.html diff --git a/error_pages/404.html b/htdocs/error_pages/404.html similarity index 100% rename from error_pages/404.html rename to htdocs/error_pages/404.html diff --git a/static_site/img/nginy.jpg b/htdocs/site-1/img/nginy.jpg similarity index 100% rename from static_site/img/nginy.jpg rename to htdocs/site-1/img/nginy.jpg diff --git a/static_site/img/nginy.png b/htdocs/site-1/img/nginy.png similarity index 100% rename from static_site/img/nginy.png rename to htdocs/site-1/img/nginy.png diff --git a/static_site/index.html b/htdocs/site-1/index.html similarity index 100% rename from static_site/index.html rename to htdocs/site-1/index.html diff --git a/www/404.html b/htdocs/site-2/404.html similarity index 100% rename from www/404.html rename to htdocs/site-2/404.html diff --git a/www/50x.html b/htdocs/site-2/50x.html similarity index 100% rename from www/50x.html rename to htdocs/site-2/50x.html diff --git a/www/api/health.json b/htdocs/site-2/api/health.json similarity index 100% rename from www/api/health.json rename to htdocs/site-2/api/health.json diff --git a/www/api/info.json b/htdocs/site-2/api/info.json similarity index 100% rename from www/api/info.json rename to htdocs/site-2/api/info.json diff --git a/www/api/users.json b/htdocs/site-2/api/users.json similarity index 100% rename from www/api/users.json rename to htdocs/site-2/api/users.json diff --git a/www/css/style.css b/htdocs/site-2/css/style.css similarity index 100% rename from www/css/style.css rename to htdocs/site-2/css/style.css diff --git a/www/docs/configuration.html b/htdocs/site-2/docs/configuration.html similarity index 100% rename from www/docs/configuration.html rename to htdocs/site-2/docs/configuration.html diff --git a/www/docs/getting-started.html b/htdocs/site-2/docs/getting-started.html similarity index 100% rename from www/docs/getting-started.html rename to htdocs/site-2/docs/getting-started.html diff --git a/www/examples/README.md b/htdocs/site-2/examples/README.md similarity index 100% rename from www/examples/README.md rename to htdocs/site-2/examples/README.md diff --git a/www/examples/sample.html b/htdocs/site-2/examples/sample.html similarity index 100% rename from www/examples/sample.html rename to htdocs/site-2/examples/sample.html diff --git a/www/examples/test.txt b/htdocs/site-2/examples/test.txt similarity index 100% rename from www/examples/test.txt rename to htdocs/site-2/examples/test.txt diff --git a/www/examples/webserv.conf b/htdocs/site-2/examples/webserv.conf similarity index 100% rename from www/examples/webserv.conf rename to htdocs/site-2/examples/webserv.conf diff --git a/www/index.html b/htdocs/site-2/index.html similarity index 100% rename from www/index.html rename to htdocs/site-2/index.html diff --git a/www/js/script.js b/htdocs/site-2/js/script.js similarity index 100% rename from www/js/script.js rename to htdocs/site-2/js/script.js diff --git a/tests/log/test_log.cpp b/tests/log/test_log.cpp index 9f4f233..37e59d1 100644 --- a/tests/log/test_log.cpp +++ b/tests/log/test_log.cpp @@ -78,7 +78,7 @@ TEST_F(LogTest, LogLevelToColoredString) TEST_F(LogTest, StdoutChannelConstruction) { - StdoutChannel channel(); + StdoutChannel channel; // If we reach here without exception, construction was successful SUCCEED(); } diff --git a/webserv/client/Client.cpp b/webserv/client/Client.cpp index 281d993..4f677c5 100644 --- a/webserv/client/Client.cpp +++ b/webserv/client/Client.cpp @@ -15,8 +15,8 @@ #include // for ssize_t Client::Client(std::unique_ptr socket, Server &server) - : httpRequest_(std::make_unique(this)), httpResponse_(std::make_unique()), client_socket_(std::move(socket)), - server_(std::ref(server)) + : httpRequest_(std::make_unique(this)), httpResponse_(std::make_unique()), + client_socket_(std::move(socket)), server_(std::ref(server)) { Log::trace(LOCATION); Log::info("New client connected, fd: " + std::to_string(client_socket_->getFd())); @@ -45,8 +45,8 @@ void Client::request() { Log::trace(LOCATION); char buffer[bufferSize_] = {}; // NOLINT(cppcoreguidelines-avoid-c-arrays) - ssize_t bytesRead = - client_socket_->recv(buffer, sizeof(buffer) - 1); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay) + ssize_t bytesRead = client_socket_->recv( + buffer, sizeof(buffer) - 1); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay) if (bytesRead < 0) { Log::error("Read error"); @@ -62,8 +62,8 @@ void Client::request() buffer[bytesRead] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) httpRequest_->receiveData(static_cast(buffer), static_cast(bytesRead)); - if (httpRequest_->getState() == HttpRequest::State::Complete || - httpRequest_->getState() == HttpRequest::State::ParseError) + if (httpRequest_->getState() == HttpRequest::State::Complete + || httpRequest_->getState() == HttpRequest::State::ParseError) { Log::info("Received complete request", { diff --git a/webserv/config/AConfig.cpp b/webserv/config/AConfig.cpp index 1f0fc00..12c11a8 100644 --- a/webserv/config/AConfig.cpp +++ b/webserv/config/AConfig.cpp @@ -1,4 +1,5 @@ #include // for AConfig + #include // for ADirective #include // for DirectiveFactory #include // for DirectiveValue @@ -52,7 +53,7 @@ bool AConfig::has(const std::string &name) const { return true; } - + if (parent_ != nullptr) { return parent_->has(name); diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index 5e0628b..8c04ea6 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -18,8 +18,8 @@ ServerConfig::ServerConfig(const std::string &block, const AConfig *parent) : AC std::string ServerConfig::getName() const { - return "server: " + get("server_name").value_or("unnamed") + " (port " + - std::to_string(get("listen").value_or(-1)) + ")"; + return "server: " + get("server_name").value_or("unnamed") + " (port " + + std::to_string(get("listen").value_or(-1)) + ")"; } std::string ServerConfig::getType() const @@ -45,8 +45,8 @@ void ServerConfig::parseBlock(const std::string &block) directives += block.substr(pos); break; } - std::string locationPath = - utils::trim(block.substr(locationPos + 9, bracePos - (locationPos + 9))); // TODO magic numbers + std::string locationPath + = utils::trim(block.substr(locationPos + 9, bracePos - (locationPos + 9))); // TODO magic numbers // Add global declarations before this server block directives += block.substr(pos, locationPos - pos); size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos); diff --git a/webserv/config/directive/DirectiveFactory.hpp b/webserv/config/directive/DirectiveFactory.hpp index 9f6e319..0c0933c 100644 --- a/webserv/config/directive/DirectiveFactory.hpp +++ b/webserv/config/directive/DirectiveFactory.hpp @@ -20,7 +20,6 @@ class DirectiveFactory std::string_view context; }; - constexpr static std::array supportedDirectives = {{ {.name = "listen", .type = "IntDirective", .context = "S"}, {.name = "host", .type = "StringDirective", .context = "S"}, @@ -38,13 +37,10 @@ class DirectiveFactory {.name = "upload_store", .type = "StringDirective", .context = "gsl"}, {.name = "redirect", .type = "VectorDirective", .context = "l"}, }}; + private: using CreatorFunc = std::function(const std::string &, const std::string &arg)>; static const std::unordered_map &getFactories(); static std::unique_ptr create(std::string_view type, const std::string &name, const std::string &arg); - - - - }; \ No newline at end of file diff --git a/webserv/config/validation/ConfigValidator.cpp b/webserv/config/validation/ConfigValidator.cpp index 0165237..95544eb 100644 --- a/webserv/config/validation/ConfigValidator.cpp +++ b/webserv/config/validation/ConfigValidator.cpp @@ -1,18 +1,24 @@ #include -#include -#include // for ValidationEngine -#include // for AValidationRule -#include // for AllowedValuesRule -#include -#include -#include // for PortValidationRule -#include // for structural rules -#include // for LOCATION, Log +#include // for ValidationEngine +#include // for AValidationRule +#include // for AllowedValuesRule +#include // for CgiExtValidationRule +#include // for FolderExistsRule +#include // for HostValidationRule +#include // for PortValidationRule +#include // for AStructuralValidationRule +#include // for MinimumServerBlocksRule +#include // for RequiredDirectivesRule +#include // for RequiredLocationBlocksRule +#include // for UniqueServerNamesRule +#include // for LOCATION, Log -#include +#include // for unique_ptr, make_unique #include // for basic_string, string +class ValidationResult; + ConfigValidator::ConfigValidator(const GlobalConfig *config) : engine_(std::make_unique(config)) { Log::trace(LOCATION); diff --git a/webserv/config/validation/ValidationEngine.cpp b/webserv/config/validation/ValidationEngine.cpp index 429b06e..c7a62b4 100644 --- a/webserv/config/validation/ValidationEngine.cpp +++ b/webserv/config/validation/ValidationEngine.cpp @@ -1,13 +1,16 @@ +#include + #include // for AConfig #include // for GlobalConfig #include // for LocationConfig #include // for ServerConfig -#include -#include // for ValidationResult -#include // for AValidationRule -#include // for Log, LOCATION +#include // for ValidationResult +#include // for AValidationRule +#include // for AStructuralValidationRule +#include // for Log, LOCATION -#include // for move, get +#include // for exception +#include // for move, get void ValidationEngine::addGlobalRule(const std::string &directiveName, std::unique_ptr rule) { @@ -136,8 +139,8 @@ void ValidationEngine::validateLocationConfig(const std::string &path, const Loc } catch (const std::exception &e) { - results_.push_back(ValidationResult::error("Structural rule '" + rule->getRuleName() + - "' threw exception for location '" + path + "': " + e.what())); + results_.push_back(ValidationResult::error("Structural rule '" + rule->getRuleName() + + "' threw exception for location '" + path + "': " + e.what())); } } diff --git a/webserv/config/validation/directive_rules/AValidationRule.cpp b/webserv/config/validation/directive_rules/AValidationRule.cpp index 3f4747c..45b01d8 100644 --- a/webserv/config/validation/directive_rules/AValidationRule.cpp +++ b/webserv/config/validation/directive_rules/AValidationRule.cpp @@ -1,6 +1,7 @@ +#include + #include // for AConfig #include // for ValidationResult -#include #include // for LOCATION, Log #include // for move diff --git a/webserv/config/validation/directive_rules/AValidationRule.hpp b/webserv/config/validation/directive_rules/AValidationRule.hpp index 7ef4465..845434f 100644 --- a/webserv/config/validation/directive_rules/AValidationRule.hpp +++ b/webserv/config/validation/directive_rules/AValidationRule.hpp @@ -17,8 +17,8 @@ class AValidationRule [[nodiscard]] ValidationResult validate(const AConfig *config, const std::string &directiveName) const; [[nodiscard]] bool isRequired() const; - [[nodiscard]] virtual ValidationResult validateValue(const AConfig *config, - const std::string &directiveName) const = 0; + [[nodiscard]] virtual ValidationResult validateValue(const AConfig *config, const std::string &directiveName) const + = 0; [[nodiscard]] std::string getRuleName() const; [[nodiscard]] std::string getDescription() const; diff --git a/webserv/config/validation/directive_rules/AllowedValuesRule.cpp b/webserv/config/validation/directive_rules/AllowedValuesRule.cpp index efc9c58..b1aef9b 100644 --- a/webserv/config/validation/directive_rules/AllowedValuesRule.cpp +++ b/webserv/config/validation/directive_rules/AllowedValuesRule.cpp @@ -6,11 +6,9 @@ #include // for ValidationResult #include // for AValidationRule -#include // for identity -#include // for __find_fn, find -#include // for basic_string, allocator, operator+, char_traits, string, operator== -#include // for move -#include // for vector +#include // for __find_fn, find +#include // for basic_string, allocator, operator+, char_traits, string +#include // for vector AllowedValuesRule::AllowedValuesRule(const std::vector &allowedValues, bool requiresValue) : AValidationRule("AllowedValuesRule", "Ensures that the directive's value is within the allowed set", diff --git a/webserv/config/validation/directive_rules/CgiExtValidationRule.cpp b/webserv/config/validation/directive_rules/CgiExtValidationRule.cpp index 5abc5ab..fd210f5 100644 --- a/webserv/config/validation/directive_rules/CgiExtValidationRule.cpp +++ b/webserv/config/validation/directive_rules/CgiExtValidationRule.cpp @@ -1,11 +1,14 @@ #include -#include -#include -#include +#include // for AConfig +#include // for ADirective +#include // for DirectiveValue +#include // for ValidationResult +#include // for AValidationRule +#include // for isFile -#include -#include +#include // for __any_of_fn, any_of +#include // for vector CgiExtValidationRule::CgiExtValidationRule(bool requiresValue) : AValidationRule("CgiExt", "Ensure CGI extension is valid", requiresValue) @@ -29,20 +32,20 @@ ValidationResult CgiExtValidationRule::validateValue(const AConfig *config, cons auto cgiExt = directive->getValue().get>(); if (cgiExt.size() != 2) { - return ValidationResult::error("Directive '" + directive->getName() + - "' has invalid format, expected extension and path"); + return ValidationResult::error("Directive '" + directive->getName() + + "' has invalid format, expected extension and path"); } auto extension = std::string(cgiExt[0]); auto path = std::string(cgiExt[1]); if (extension.empty() || extension[0] != '.') { - return ValidationResult::error("Directive '" + directive->getName() + "' has invalid extension '" + extension + - "'"); + return ValidationResult::error("Directive '" + directive->getName() + "' has invalid extension '" + extension + + "'"); } if (!isAllowedCGIExtension(extension)) { - return ValidationResult::error("Directive '" + directive->getName() + "' has unsupported extension '" + - extension + "'"); + return ValidationResult::error("Directive '" + directive->getName() + "' has unsupported extension '" + + extension + "'"); } if (!FileUtils::isFile(path)) { diff --git a/webserv/config/validation/directive_rules/CgiExtValidationRule.hpp b/webserv/config/validation/directive_rules/CgiExtValidationRule.hpp index 45afbf8..6e0f6a6 100644 --- a/webserv/config/validation/directive_rules/CgiExtValidationRule.hpp +++ b/webserv/config/validation/directive_rules/CgiExtValidationRule.hpp @@ -1,7 +1,9 @@ #pragma once -#include -#include // for ValidationResult +#include // for ValidationResult +#include // for AValidationRule + +#include // for string class CgiExtValidationRule : public AValidationRule { diff --git a/webserv/config/validation/directive_rules/DirectiveRules.hpp b/webserv/config/validation/directive_rules/DirectiveRules.hpp index fb2666a..39b1227 100644 --- a/webserv/config/validation/directive_rules/DirectiveRules.hpp +++ b/webserv/config/validation/directive_rules/DirectiveRules.hpp @@ -1,4 +1,5 @@ -#include +#pragma once +#include #include #include diff --git a/webserv/config/validation/directive_rules/FolderExistsRule.cpp b/webserv/config/validation/directive_rules/FolderExistsRule.cpp index fb44455..46ea9b2 100644 --- a/webserv/config/validation/directive_rules/FolderExistsRule.cpp +++ b/webserv/config/validation/directive_rules/FolderExistsRule.cpp @@ -1,9 +1,12 @@ #include -#include -#include -#include -#include +#include // for AConfig +#include // for ADirective +#include // for DirectiveValue +#include // for ValidationResult +#include // for AValidationRule +#include // for Log +#include // for isDirectory FolderExistsRule::FolderExistsRule(bool requiresValue) : AValidationRule("FolderExists", "Ensures the specified folder exists", requiresValue) diff --git a/webserv/config/validation/directive_rules/FolderExistsRule.hpp b/webserv/config/validation/directive_rules/FolderExistsRule.hpp index 942b00f..271ebcd 100644 --- a/webserv/config/validation/directive_rules/FolderExistsRule.hpp +++ b/webserv/config/validation/directive_rules/FolderExistsRule.hpp @@ -2,7 +2,9 @@ #include #include -#include +#include // for AValidationRule + +#include // for string class FolderExistsRule : public AValidationRule { diff --git a/webserv/config/validation/directive_rules/HostValidationRule.cpp b/webserv/config/validation/directive_rules/HostValidationRule.cpp index 7341952..cc67379 100644 --- a/webserv/config/validation/directive_rules/HostValidationRule.cpp +++ b/webserv/config/validation/directive_rules/HostValidationRule.cpp @@ -1,13 +1,15 @@ #include -#include -#include -#include -#include +#include // for AConfig +#include // for ADirective +#include // for DirectiveValue +#include // for ValidationResult +#include // for AValidationRule +#include // for split -#include -#include -#include +#include // for isdigit +#include // for basic_string, allocator, char_traits, operator+, string, stoi +#include // for vector HostValidationRule::HostValidationRule(bool requiresValue) : AValidationRule("HostValidationRule", "Validates that the host is a valid domain name", requiresValue) diff --git a/webserv/config/validation/structural_rules/MinimumServerBlocksRule.cpp b/webserv/config/validation/structural_rules/MinimumServerBlocksRule.cpp index f864675..973fe84 100644 --- a/webserv/config/validation/structural_rules/MinimumServerBlocksRule.cpp +++ b/webserv/config/validation/structural_rules/MinimumServerBlocksRule.cpp @@ -1,13 +1,16 @@ #include -#include -#include +#include // for GlobalConfig +#include // for ValidationResult +#include // for AStructuralValidationRule +#include // for LOCATION, Log -#include +#include // for allocator, operator+, char_traits, to_string, basic_string +#include // for vector MinimumServerBlocksRule::MinimumServerBlocksRule(size_t minimumServers) - : AStructuralValidationRule("MinimumServerBlocksRule", "Ensures global config has at least " + - std::to_string(minimumServers) + " server block(s)"), + : AStructuralValidationRule("MinimumServerBlocksRule", "Ensures global config has at least " + + std::to_string(minimumServers) + " server block(s)"), minimumServers_(minimumServers) { } @@ -25,8 +28,8 @@ ValidationResult MinimumServerBlocksRule::validateGlobal(const GlobalConfig *con if (serverCount < minimumServers_) { - return ValidationResult::error("Global configuration must have at least " + std::to_string(minimumServers_) + - " server block(s), but found " + std::to_string(serverCount)); + return ValidationResult::error("Global configuration must have at least " + std::to_string(minimumServers_) + + " server block(s), but found " + std::to_string(serverCount)); } return ValidationResult::success(); diff --git a/webserv/config/validation/structural_rules/MinimumServerBlocksRule.hpp b/webserv/config/validation/structural_rules/MinimumServerBlocksRule.hpp index 4c24c3f..4be40da 100644 --- a/webserv/config/validation/structural_rules/MinimumServerBlocksRule.hpp +++ b/webserv/config/validation/structural_rules/MinimumServerBlocksRule.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include // for AStructuralValidationRule -#include +#include // for size_t class GlobalConfig; diff --git a/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp b/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp index f15f826..507c45a 100644 --- a/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp +++ b/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp @@ -1,13 +1,18 @@ -#include -#include -#include -#include -#include #include -#include -#include -#include +#include // for AConfig +#include // for GlobalConfig +#include // for LocationConfig +#include // for ServerConfig +#include // for DirectiveFactory +#include // for ValidationResult +#include // for AStructuralValidationRule +#include // for implode + +#include // for array +#include // for toupper, tolower +#include // for basic_string, allocator, char_traits, string, operator+ +#include // for vector RequiredDirectivesRule::RequiredDirectivesRule() : AStructuralValidationRule("RequiredDirectivesRule", "Ensures required directives are present in each context") @@ -21,14 +26,16 @@ ValidationResult validateUniversal(const AConfig *config, std::string configType for (const auto &directive : DirectiveFactory::supportedDirectives) { - if (directive.context.find(static_cast(std::toupper(configType[0]))) != std::string::npos && - !config->owns(std::string(directive.name))) + if (directive.context.find(static_cast(std::toupper(configType[0]))) != std::string::npos + && !config->owns(std::string(directive.name))) { missingDirectives.emplace_back(directive.name); } - if ((directive.context.find(static_cast(std::toupper(static_cast(configType[0])))) == std::string::npos && - directive.context.find(static_cast(std::tolower(static_cast(configType[0])))) == std::string::npos) && - config->owns(std::string(directive.name))) + if ((directive.context.find(static_cast(std::toupper(static_cast(configType[0])))) + == std::string::npos + && directive.context.find(static_cast(std::tolower(static_cast(configType[0])))) + == std::string::npos) + && config->owns(std::string(directive.name))) { prohibitedDirectives.emplace_back(directive.name); } diff --git a/webserv/config/validation/structural_rules/RequiredDirectivesRule.hpp b/webserv/config/validation/structural_rules/RequiredDirectivesRule.hpp index d5bb8c3..8c67645 100644 --- a/webserv/config/validation/structural_rules/RequiredDirectivesRule.hpp +++ b/webserv/config/validation/structural_rules/RequiredDirectivesRule.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include // for AStructuralValidationRule #include diff --git a/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.cpp b/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.cpp index 3203fe6..6d3d8cd 100644 --- a/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.cpp +++ b/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.cpp @@ -1,14 +1,17 @@ #include -#include -#include +#include // for ServerConfig +#include // for ValidationResult +#include // for AStructuralValidationRule +#include // for LOCATION, Log -#include +#include // for allocator, operator+, char_traits, to_string, basic_string +#include // for vector RequiredLocationBlocksRule::RequiredLocationBlocksRule(size_t minimumLocations) - : AStructuralValidationRule("RequiredLocationBlocksRule", "Ensures server has at least " + - std::to_string(minimumLocations) + - " location block(s)"), + : AStructuralValidationRule("RequiredLocationBlocksRule", "Ensures server has at least " + + std::to_string(minimumLocations) + + " location block(s)"), minimumLocations_(minimumLocations) { } @@ -26,8 +29,8 @@ ValidationResult RequiredLocationBlocksRule::validateServer(const ServerConfig * if (locationCount < minimumLocations_) { - return ValidationResult::error("Server block must have at least " + std::to_string(minimumLocations_) + - " location block(s), but found " + std::to_string(locationCount)); + return ValidationResult::error("Server block must have at least " + std::to_string(minimumLocations_) + + " location block(s), but found " + std::to_string(locationCount)); } return ValidationResult::success(); diff --git a/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.hpp b/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.hpp index c7779ba..b3acf13 100644 --- a/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.hpp +++ b/webserv/config/validation/structural_rules/RequiredLocationBlocksRule.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include // for AStructuralValidationRule -#include +#include // for size_t class ServerConfig; diff --git a/webserv/config/validation/structural_rules/StructuralRules.hpp b/webserv/config/validation/structural_rules/StructuralRules.hpp index a9b93c1..2d6bc6a 100644 --- a/webserv/config/validation/structural_rules/StructuralRules.hpp +++ b/webserv/config/validation/structural_rules/StructuralRules.hpp @@ -5,6 +5,6 @@ // Concrete structural validation rules #include +#include #include #include -#include diff --git a/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp b/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp index 2e61c4b..19c5fbd 100644 --- a/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp +++ b/webserv/config/validation/structural_rules/UniqueServerNamesRule.cpp @@ -1,12 +1,15 @@ #include -#include -#include -#include +#include // for GlobalConfig +#include // for ServerConfig +#include // for ValidationResult +#include // for AStructuralValidationRule +#include // for LOCATION, Log -#include -#include -#include +#include // for optional +#include // for set +#include // for basic_string, operator+, to_string, allocator, char_traits, string, operator<=> +#include // for vector UniqueServerNamesRule::UniqueServerNamesRule() : AStructuralValidationRule("UniqueServerNamesRule", "Ensures all server blocks have unique server names") diff --git a/webserv/config/validation/structural_rules/UniqueServerNamesRule.hpp b/webserv/config/validation/structural_rules/UniqueServerNamesRule.hpp index 5d6f104..1efe76a 100644 --- a/webserv/config/validation/structural_rules/UniqueServerNamesRule.hpp +++ b/webserv/config/validation/structural_rules/UniqueServerNamesRule.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include // for AStructuralValidationRule class GlobalConfig; diff --git a/webserv/handler/ErrorHandler.cpp b/webserv/handler/ErrorHandler.cpp index 78477ec..221e525 100644 --- a/webserv/handler/ErrorHandler.cpp +++ b/webserv/handler/ErrorHandler.cpp @@ -1,16 +1,16 @@ +#include // for ErrorHandler + #include // for AConfig #include // for ConfigManager #include // for GlobalConfig -#include // for ErrorHandler -#include // for StatusCodeInfo, CRLF, DOUBLE_CRLF, INTERNAL_SERVER_ERROR, statusCodeInfos -#include -#include // for Log +#include // for getStatusCodeReason, INTERNAL_SERVER_ERROR +#include // for HttpResponse +#include // for Log, LOCATION #include // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream -#include +#include // for allocator, make_unique, unique_ptr #include // for basic_stringstream -#include // for basic_string, operator+, allocator, char_traits, string, to_string -#include +#include // for basic_string, char_traits, operator+, string, to_string std::unique_ptr ErrorHandler::getErrorResponse(uint16_t statusCode, const AConfig *config) { @@ -51,9 +51,9 @@ std::string ErrorHandler::generateDefaultErrorPage(uint16_t statusCode) { Log::info("Generating default error page"); std::string statusMessage = Http::getStatusCodeReason(statusCode); - std::string html = "" + std::to_string(statusCode) + " " + statusMessage + - "

" + std::to_string(statusCode) + " " + statusMessage + - "


webserv

"; + std::string html = "" + std::to_string(statusCode) + " " + statusMessage + + "

" + std::to_string(statusCode) + " " + statusMessage + + "


webserv

"; return html; } diff --git a/webserv/handler/ErrorHandler.hpp b/webserv/handler/ErrorHandler.hpp index f367f69..cc5886b 100644 --- a/webserv/handler/ErrorHandler.hpp +++ b/webserv/handler/ErrorHandler.hpp @@ -1,11 +1,16 @@ #pragma once #include -#include +#include // for HttpResponse -#include +#include // for unique_ptr +#include // for string + +#include // for uint16_t #include +class AConfig; + class ErrorHandler { public: diff --git a/webserv/handler/FileHandler.cpp b/webserv/handler/FileHandler.cpp index d6f6512..4597a8b 100644 --- a/webserv/handler/FileHandler.cpp +++ b/webserv/handler/FileHandler.cpp @@ -1,7 +1,7 @@ -#include "webserv/config/AConfig.hpp" - -#include // for ErrorHandler #include + +#include // for AConfig +#include // for ErrorHandler #include // for MIMETypes #include // for URI #include // for NOT_FOUND, FORBIDDEN, OK @@ -9,10 +9,11 @@ #include // for Log, LOCATION #include // for joinPath, getExtension, isFile, readBinaryFile -#include // for unique_ptr, allocator, make_unique -#include -#include // for basic_string, string, operator+, char_traits -#include // for vector +#include // for unique_ptr, allocator, make_unique +#include // for optional +#include // for __find_if_fn, find_if +#include // for basic_string, string, operator+, char_traits +#include // for vector FileHandler::FileHandler(const AConfig *config, const URI &URI) : config_(config), uri_(URI) { diff --git a/webserv/handler/FileHandler.hpp b/webserv/handler/FileHandler.hpp index 3f3a333..aa6af63 100644 --- a/webserv/handler/FileHandler.hpp +++ b/webserv/handler/FileHandler.hpp @@ -1,7 +1,6 @@ #pragma once -#include "webserv/config/AConfig.hpp" - +#include #include #include #include // for HttpResponse diff --git a/webserv/handler/MIMETypes.hpp b/webserv/handler/MIMETypes.hpp index ba7af99..e7be5f6 100644 --- a/webserv/handler/MIMETypes.hpp +++ b/webserv/handler/MIMETypes.hpp @@ -1,5 +1,4 @@ -#ifndef MIMETYPES_HPP -#define MIMETYPES_HPP +#pragma once #include #include @@ -20,5 +19,3 @@ class MIMETypes void initializeDefaults(); }; - -#endif // MIMETYPES_HPP \ No newline at end of file diff --git a/webserv/handler/URI.cpp b/webserv/handler/URI.cpp index 3c55320..6dc89b7 100644 --- a/webserv/handler/URI.cpp +++ b/webserv/handler/URI.cpp @@ -1,14 +1,15 @@ -#include "webserv/config/AConfig.hpp" -#include "webserv/utils/FileUtils.hpp" -#include "webserv/utils/utils.hpp" - -#include // for LocationConfig -#include // for ServerConfig #include -#include // for optional +#include // for AConfig +#include // for LocationConfig +#include // for ServerConfig +#include // for HttpHeaders +#include // for joinPath, getExtension, isDirectory, isFile, isValidPath +#include // for trim, split -#include // for size_t +#include // for size_t +#include // for optional, operator!= +#include // for vector URI::URI(const HttpRequest &request, const ServerConfig &serverConfig) : uriTrimmed_(utils::trim(request.getTarget(), "/")), config_(matchConfig(uriTrimmed_, serverConfig)) diff --git a/webserv/handler/URI.hpp b/webserv/handler/URI.hpp index 2fda922..6a17b13 100644 --- a/webserv/handler/URI.hpp +++ b/webserv/handler/URI.hpp @@ -1,16 +1,17 @@ #pragma once -#include "webserv/config/AConfig.hpp" -#include "webserv/http/HttpRequest.hpp" - +#include #include #include +#include // for HttpRequest #include +#include // for map #include // for string, basic_string class LocationConfig; class ServerConfig; +class AConfig; class URI { diff --git a/webserv/http/HttpConstants.hpp b/webserv/http/HttpConstants.hpp index e3ff592..7dda7b0 100644 --- a/webserv/http/HttpConstants.hpp +++ b/webserv/http/HttpConstants.hpp @@ -52,19 +52,19 @@ struct StatusCodeInfo std::string_view reason; }; -static constexpr std::array statusCodeInfos = { - {{.code = StatusCode::OK, .reason = "OK"}, - {.code = StatusCode::CREATED, .reason = "Created"}, - {.code = StatusCode::NO_CONTENT, .reason = "No Content"}, - {.code = StatusCode::BAD_REQUEST, .reason = "Bad Request"}, - {.code = StatusCode::UNAUTHORIZED, .reason = "Unauthorized"}, - {.code = StatusCode::FORBIDDEN, .reason = "Forbidden"}, - {.code = StatusCode::NOT_FOUND, .reason = "Not Found"}, - {.code = StatusCode::METHOD_NOT_ALLOWED, .reason = "Method Not Allowed"}, - {.code = StatusCode::INTERNAL_SERVER_ERROR, .reason = "Internal Server Error"}, - {.code = StatusCode::NOT_IMPLEMENTED, .reason = "Not Implemented"}, - {.code = StatusCode::BAD_GATEWAY, .reason = "Bad Gateway"}, - {.code = StatusCode::SERVICE_UNAVAILABLE, .reason = "Service Unavailable"}}}; +static constexpr std::array statusCodeInfos + = {{{.code = StatusCode::OK, .reason = "OK"}, + {.code = StatusCode::CREATED, .reason = "Created"}, + {.code = StatusCode::NO_CONTENT, .reason = "No Content"}, + {.code = StatusCode::BAD_REQUEST, .reason = "Bad Request"}, + {.code = StatusCode::UNAUTHORIZED, .reason = "Unauthorized"}, + {.code = StatusCode::FORBIDDEN, .reason = "Forbidden"}, + {.code = StatusCode::NOT_FOUND, .reason = "Not Found"}, + {.code = StatusCode::METHOD_NOT_ALLOWED, .reason = "Method Not Allowed"}, + {.code = StatusCode::INTERNAL_SERVER_ERROR, .reason = "Internal Server Error"}, + {.code = StatusCode::NOT_IMPLEMENTED, .reason = "Not Implemented"}, + {.code = StatusCode::BAD_GATEWAY, .reason = "Bad Gateway"}, + {.code = StatusCode::SERVICE_UNAVAILABLE, .reason = "Service Unavailable"}}}; std::string getStatusCodeReason(uint16_t statusCode); diff --git a/webserv/http/HttpResponse.cpp b/webserv/http/HttpResponse.cpp index 03984c5..eedce15 100644 --- a/webserv/http/HttpResponse.cpp +++ b/webserv/http/HttpResponse.cpp @@ -1,10 +1,9 @@ #include -#include +#include // for getStatusCodeReason -#include -#include -#include +#include // for basic_string, operator+, string, char_traits, to_string +#include // for vector HttpResponse::HttpResponse() : headers_(std::make_unique()) {} diff --git a/webserv/http/HttpResponse.hpp b/webserv/http/HttpResponse.hpp index 35b5130..b531301 100644 --- a/webserv/http/HttpResponse.hpp +++ b/webserv/http/HttpResponse.hpp @@ -3,7 +3,7 @@ #include // for HttpHeaders #include // for LOCATION, Log -#include // for uint8_t +#include // for uint8_t, uint16_t #include // for unique_ptr #include // for string #include // for vector diff --git a/webserv/log/Channel.cpp b/webserv/log/Channel.cpp index 54dbb0a..5c14b55 100644 --- a/webserv/log/Channel.cpp +++ b/webserv/log/Channel.cpp @@ -4,7 +4,6 @@ #include // for basic_ostream, operator<<, basic_stringstream, basic_istream, basic_istringstream, right, istringstream, stringstream #include // for get - std::string Channel::printContext(const std::map &context) { std::stringstream ss; diff --git a/webserv/log/Channel.hpp b/webserv/log/Channel.hpp index a8f7915..dc357d0 100644 --- a/webserv/log/Channel.hpp +++ b/webserv/log/Channel.hpp @@ -15,13 +15,11 @@ class Channel Channel &operator=(const Channel &other) = delete; Channel &operator=(Channel &&other) = delete; virtual void log(const Log::Level &logLevel, const std::string &message, - const std::map &context = {}) = 0; + const std::map &context = {}) + = 0; protected: - [[nodiscard]] static std::string printContext(const std::map &context); - private: - }; \ No newline at end of file diff --git a/webserv/log/Log.cpp b/webserv/log/Log.cpp index 02ca2c0..5ea84d6 100644 --- a/webserv/log/Log.cpp +++ b/webserv/log/Log.cpp @@ -1,6 +1,7 @@ +#include + #include // for Channel #include // for FileChannel -#include #include // for StdoutChannel #include // for duration_cast, operator-, steady_clock, duration, seconds diff --git a/webserv/log/Log.hpp b/webserv/log/Log.hpp index 532a58a..9ac20b1 100644 --- a/webserv/log/Log.hpp +++ b/webserv/log/Log.hpp @@ -29,7 +29,6 @@ constexpr const char *extractFilename(const char *path) #define LOCATION \ (std::string(extractFilename(__FILE__)) + ":" + std::to_string(__LINE__) + " (" + std::string(__FUNCTION__) + ")") - class Log { public: @@ -38,7 +37,7 @@ class Log Log &operator=(const Log &other) = delete; Log &operator=(Log &&other) = delete; - enum class Level : uint8_t + enum class Level : uint8_t { Trace = 0, Debug = 1, @@ -85,15 +84,13 @@ class Log const char *color; }; - constexpr static std::array LOG_LEVEL_MAP = { - {{.level = Log::Level::Trace, .name = "TRACE", .color = "\033[36m"}, - {.level = Log::Level::Debug, .name = "DEBUG", .color = "\033[90m"}, - {.level = Log::Level::Info, .name = "INFO", .color = "\033[37m"}, - {.level = Log::Level::Warn, .name = "WARN", .color = "\033[33m"}, - {.level = Log::Level::Error, .name = "ERROR", .color = "\033[31m"}, - {.level = Log::Level::Fatal, .name = "FATAL", .color = "\033[1;31m"}}}; + constexpr static std::array LOG_LEVEL_MAP + = {{{.level = Log::Level::Trace, .name = "TRACE", .color = "\033[36m"}, + {.level = Log::Level::Debug, .name = "DEBUG", .color = "\033[90m"}, + {.level = Log::Level::Info, .name = "INFO", .color = "\033[37m"}, + {.level = Log::Level::Warn, .name = "WARN", .color = "\033[33m"}, + {.level = Log::Level::Error, .name = "ERROR", .color = "\033[31m"}, + {.level = Log::Level::Fatal, .name = "FATAL", .color = "\033[1;31m"}}}; constexpr static const char *RESET_COLOR = "\033[0m"; }; - - diff --git a/webserv/log/StdoutChannel.cpp b/webserv/log/StdoutChannel.cpp index c5e6c8e..bd7c410 100644 --- a/webserv/log/StdoutChannel.cpp +++ b/webserv/log/StdoutChannel.cpp @@ -5,8 +5,6 @@ #include // for operator<<, setfill, setw #include // for basic_ostream, operator<<, basic_ostream::operator<<, cerr, cout, flush, ostream - - void StdoutChannel::log(const Log::Level &logLevel, const std::string &message, const std::map &context) { diff --git a/webserv/main.cpp b/webserv/main.cpp index aead3f8..ed2a042 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -8,9 +8,6 @@ #include // for allocator, basic_string, char_traits, operator+, string #include // for vector - - - int main(int argc, char **argv) { if (argc < 2) diff --git a/webserv/router/Router.cpp b/webserv/router/Router.cpp index b32effc..080bd23 100644 --- a/webserv/router/Router.cpp +++ b/webserv/router/Router.cpp @@ -1,22 +1,22 @@ -#include "webserv/config/AConfig.hpp" - -#include // for ConfigManager -#include // for ServerConfig -#include -#include // for ErrorHandler -#include // for FileHandler -#include // for URI -#include // for HttpHeaders -#include // for LOCATION, Log #include -#include -#include // for unique_ptr -#include // for optional -#include // for basic_string, string -#include +#include // for AConfig +#include // for ConfigManager +#include // for ADirective +#include // for DirectiveValue +#include // for ErrorHandler +#include // for FileHandler +#include // for URI +#include // for HttpHeaders +#include // for LOCATION, Log -class LocationConfig; +#include // for identity +#include // for unique_ptr +#include // for optional +#include // for __find_fn, find +#include // for basic_string, string, operator== +#include // for move +#include // for vector bool Router::isMethodSupported(const std::string &method, const AConfig &config) { @@ -33,8 +33,8 @@ std::unique_ptr Router::handleRequest(const HttpRequest &request) { Log::trace(LOCATION); - ServerConfig *serverConfig = - ConfigManager::getInstance().getMatchingServerConfig(request.getHeaders().getHost().value_or("")); + ServerConfig *serverConfig + = ConfigManager::getInstance().getMatchingServerConfig(request.getHeaders().getHost().value_or("")); if (serverConfig == nullptr) { diff --git a/webserv/router/Router.hpp b/webserv/router/Router.hpp index 6ae88ee..5f5412d 100644 --- a/webserv/router/Router.hpp +++ b/webserv/router/Router.hpp @@ -1,6 +1,6 @@ #pragma once -#include "webserv/config/AConfig.hpp" +#include // for AConfig #include #include // for HttpRequest #include // for HttpResponse diff --git a/webserv/server/Server.cpp b/webserv/server/Server.cpp index a1449e7..e61c7e4 100644 --- a/webserv/server/Server.cpp +++ b/webserv/server/Server.cpp @@ -224,8 +224,8 @@ void Server::eventLoop() ssize_t bytesSent = send(event.data.fd, httpResponse.data(), httpResponse.size(), 0); if (bytesSent < 0) { - Log::error("Send failed for fd: " + std::to_string(event.data.fd) + - " with error: " + std::strerror(errno)); + Log::error("Send failed for fd: " + std::to_string(event.data.fd) + + " with error: " + std::strerror(errno)); } else { diff --git a/webserv/socket/Socket.cpp b/webserv/socket/Socket.cpp index 572215e..2ab2511 100644 --- a/webserv/socket/Socket.cpp +++ b/webserv/socket/Socket.cpp @@ -68,10 +68,11 @@ void Socket::bind(const std::string &host, const int port) const address.sin_addr.s_addr = inet_addr(host.c_str()); address.sin_port = htons(port); - if (::bind(fd_, reinterpret_cast(&address), sizeof(address)) < 0) //NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + if (::bind(fd_, reinterpret_cast(&address), sizeof(address)) + < 0) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) { - Log::fatal("Cannot bind to " + host + ":" + std::to_string(port) + - " - address already in use or permission denied"); + Log::fatal("Cannot bind to " + host + ":" + std::to_string(port) + + " - address already in use or permission denied"); throw std::runtime_error("Bind failed"); } } diff --git a/webserv/utils/FileUtils.cpp b/webserv/utils/FileUtils.cpp index 91cbabf..63fa4ed 100644 --- a/webserv/utils/FileUtils.cpp +++ b/webserv/utils/FileUtils.cpp @@ -1,6 +1,7 @@ -#include // for Log, LOCATION #include +#include // for Log, LOCATION + #include // for size_t #include // for basic_ifstream, basic_ios, basic_istream, ios, ifstream, operator|, basic_istream::read, basic_istream::seekg, basic_istream::tellg, streamsize #include // for istreambuf_iterator, operator==