diff --git a/webserv/client/Client.cpp b/webserv/client/Client.cpp index 08e4b99..563fc87 100644 --- a/webserv/client/Client.cpp +++ b/webserv/client/Client.cpp @@ -1,20 +1,18 @@ -#include "webserv/router/Router.hpp" - #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // for HttpHeaders +#include // for Log, LOCATION +#include // for Router +#include // for Server +#include // for Socket -#include +#include // for uint8_t +#include // for ref, reference_wrapper +#include // for map +#include // for basic_string, to_string, operator+, operator<=> +#include // for pair, move + +#include // for ssize_t Client::Client(std::unique_ptr socket, Server &server) : client_socket_(std::move(socket)), server_(std::ref(server)), httpRequest_(std::make_unique(this)), diff --git a/webserv/client/Client.hpp b/webserv/client/Client.hpp index e068e76..05d5228 100644 --- a/webserv/client/Client.hpp +++ b/webserv/client/Client.hpp @@ -1,19 +1,18 @@ #pragma once -// #include "webserv/http/HttpResponse.hpp" - -#include "webserv/http/HttpResponse.hpp" +// #include #include // for ServerConfig #include // for OK #include // for HttpRequest +#include // for HttpResponse #include -#include +#include // for Socket #include // for size_t -#include -#include // for unique_ptr -#include +#include // for uint8_t +#include // for unique_ptr +#include // for vector class Server; class Socket; @@ -39,6 +38,7 @@ class Client [[nodiscard]] int getStatusCode() const; [[nodiscard]] Socket &getSocket() const { return *client_socket_; } + // void setError(int statusCode); void setStatusCode(int code); diff --git a/webserv/config/AConfig.cpp b/webserv/config/AConfig.cpp index 896c7ca..fbdf9f8 100644 --- a/webserv/config/AConfig.cpp +++ b/webserv/config/AConfig.cpp @@ -1,11 +1,13 @@ #include // for AConfig + #include // for ADirective #include // for DirectiveFactory +#include // for DirectiveValue #include // for Log, LOCATION #include // for trim #include // for basic_stringstream, stringstream -#include // for move, pair +#include // for pair, move AConfig::AConfig(const AConfig *parent) : parent_(parent) {} diff --git a/webserv/config/AConfig.hpp b/webserv/config/AConfig.hpp index a194a01..d57213b 100644 --- a/webserv/config/AConfig.hpp +++ b/webserv/config/AConfig.hpp @@ -3,10 +3,10 @@ #include // for ADirective #include -#include // for unique_ptr -#include -#include // for string -#include // for vector +#include // for unique_ptr +#include // for nullopt, optional +#include // for string +#include // for vector class AConfig { @@ -28,8 +28,7 @@ class AConfig [[nodiscard]] const ADirective *getDirective(const std::string &name) const; [[nodiscard]] std::vector getDirectives() const; - template - std::optional get(const std::string &name) const + template std::optional get(const std::string &name) const { const auto *directive = getDirective(name); if (!directive) @@ -39,7 +38,7 @@ class AConfig return directive->getValue().try_get(); } - protected: + protected: virtual void parseBlock(const std::string &block) = 0; void parseDirectives(const std::string &declarations); std::vector> diff --git a/webserv/config/ConfigManager.cpp b/webserv/config/ConfigManager.cpp index 94cd091..b73ce15 100644 --- a/webserv/config/ConfigManager.cpp +++ b/webserv/config/ConfigManager.cpp @@ -1,9 +1,11 @@ #include + #include // for GlobalConfig #include // for Log #include // for removeComments #include // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream +#include // for optional #include // for basic_stringstream #include // for runtime_error #include // for basic_string, char_traits, operator+, string, to_string, operator==, stoi diff --git a/webserv/config/GlobalConfig.cpp b/webserv/config/GlobalConfig.cpp index 3bb8bce..51cb2dc 100644 --- a/webserv/config/GlobalConfig.cpp +++ b/webserv/config/GlobalConfig.cpp @@ -1,4 +1,5 @@ #include + #include // for Log #include // for findCorrespondingClosingBrace diff --git a/webserv/config/GlobalConfig.hpp b/webserv/config/GlobalConfig.hpp index 0a980cf..dd6fdfe 100644 --- a/webserv/config/GlobalConfig.hpp +++ b/webserv/config/GlobalConfig.hpp @@ -19,7 +19,7 @@ class GlobalConfig : public AConfig GlobalConfig &operator=(GlobalConfig &&other) noexcept = delete; ~GlobalConfig() override = default; - + [[nodiscard]] std::string getName() const override; [[nodiscard]] std::string getType() const override; diff --git a/webserv/config/LocationConfig.cpp b/webserv/config/LocationConfig.cpp index 1dfa4a6..ce9e6e6 100644 --- a/webserv/config/LocationConfig.cpp +++ b/webserv/config/LocationConfig.cpp @@ -1,5 +1,6 @@ -#include // for AConfig #include + +#include // for AConfig #include // for Log, LOCATION LocationConfig::LocationConfig(const std::string &block, const std::string &path, const AConfig *parent) diff --git a/webserv/config/LocationConfig.hpp b/webserv/config/LocationConfig.hpp index 3728e91..e86bfb4 100644 --- a/webserv/config/LocationConfig.hpp +++ b/webserv/config/LocationConfig.hpp @@ -19,6 +19,7 @@ class LocationConfig : public AConfig [[nodiscard]] std::string getName() const override; [[nodiscard]] std::string getType() const override; + [[nodiscard]] const std::string &getPath() const { return _path; } private: diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index 75bfb3d..5e0628b 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -1,9 +1,11 @@ +#include + #include // for AConfig #include // for LocationConfig -#include #include // for Log, LOCATION #include // for findCorrespondingClosingBrace, trim +#include // for optional #include // for runtime_error #include // for pair @@ -79,4 +81,3 @@ std::vector ServerConfig::getLocationPaths() const } return paths; } - diff --git a/webserv/config/ServerConfig.hpp b/webserv/config/ServerConfig.hpp index 5e6a7de..19423eb 100644 --- a/webserv/config/ServerConfig.hpp +++ b/webserv/config/ServerConfig.hpp @@ -27,7 +27,6 @@ class ServerConfig : public AConfig [[nodiscard]] const LocationConfig *getLocation(const std::string &path) const; [[nodiscard]] std::vector getLocationPaths() const; - private: std::map> locations_; AConfig *parent_ = nullptr; diff --git a/webserv/config/config_validator/AllowedValuesRule.hpp b/webserv/config/config_validator/AllowedValuesRule.hpp deleted file mode 100644 index e43252f..0000000 --- a/webserv/config/config_validator/AllowedValuesRule.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "webserv/config/config_validator/AValidationRule.hpp" -#include "webserv/config/config_validator/ValidationResult.hpp" -#include -#include - -class AConfig; - -class AllowedValuesRule : public AValidationRule -{ - public: - explicit AllowedValuesRule(const std::vector &allowedValues, bool requiresValue = true); - - private: - [[nodiscard]] ValidationResult validateValue(const AConfig *config, const std::string &directiveName) const override; - std::vector allowedValues_; -}; \ No newline at end of file diff --git a/webserv/config/config_validator/ConfigValidator.cpp b/webserv/config/config_validator/ConfigValidator.cpp deleted file mode 100644 index b2854ee..0000000 --- a/webserv/config/config_validator/ConfigValidator.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "webserv/config/config_validator/ConfigValidator.hpp" - -#include "webserv/config/GlobalConfig.hpp" -#include "webserv/config/config_validator/AllowedValuesRule.hpp" -#include "webserv/config/config_validator/PortValidationRule.hpp" -#include "webserv/log/Log.hpp" -#include - -ConfigValidator::ConfigValidator(const GlobalConfig *config) - : engine_(std::make_unique(config)) -{ - Log::trace(LOCATION); - - engine_->addServerRule("listen", std::make_unique()); - engine_->addLocationRule("allowed_methods", std::make_unique(std::vector{"GET", "POST", "DELETE"})); - engine_->validate(); -} - -std::vector ConfigValidator::getValidationResults() const -{ - return engine_->getValidationResults(); -} - -std::vector ConfigValidator::getErrors() const -{ - return engine_->getErrors(); -} - -std::vector ConfigValidator::getWarnings() const -{ - return engine_->getWarnings(); -} - -bool ConfigValidator::hasErrors() const -{ - return engine_->hasErrors(); -} - diff --git a/webserv/config/config_validator/PortValidationRule.cpp b/webserv/config/config_validator/PortValidationRule.cpp deleted file mode 100644 index a53cbe0..0000000 --- a/webserv/config/config_validator/PortValidationRule.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "webserv/config/config_validator/PortValidationRule.hpp" - -#include "webserv/config/AConfig.hpp" -#include "webserv/config/config_validator/ValidationResult.hpp" -#include "webserv/config/directive/ADirective.hpp" -#include "webserv/config/directive/DirectiveValue.hpp" -#include "webserv/log/Log.hpp" - -#include // for string, basic_string, operator+, char_traits - -PortValidationRule::PortValidationRule(bool requiresValue) - : AValidationRule("PortValidationRule", "Validates that the port number is within the valid range (1-65535)", requiresValue) -{ -} - -ValidationResult PortValidationRule::validateValue(const AConfig *config, const std::string &directiveName) const -{ - Log::trace(LOCATION); - const ADirective *directive = config->getDirective(directiveName); - if (!directive->getValue().holds()) - { - return ValidationResult::error("Directive '" + directive->getName() + "' does not hold an integer value"); - } - - int port = directive->getValue().get(); - if (port < 1 || port > 65535) - { - return ValidationResult::error("Port number " + std::to_string(port) + " is out of valid range (1-65535)"); - } - - return ValidationResult::success(); -} diff --git a/webserv/config/config_validator/PortValidationRule.hpp b/webserv/config/config_validator/PortValidationRule.hpp deleted file mode 100644 index 0960999..0000000 --- a/webserv/config/config_validator/PortValidationRule.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "webserv/config/config_validator/AValidationRule.hpp" -#include - -class AConfig; -class PortValidationRule : public AValidationRule -{ - public: - PortValidationRule(bool requiresValue = true); - - private: - [[nodiscard]] ValidationResult validateValue(const AConfig *config, const std::string &directiveName) const override; -}; \ No newline at end of file diff --git a/webserv/config/config_validator/RequiredDirectiveRule.cpp b/webserv/config/config_validator/RequiredDirectiveRule.cpp deleted file mode 100644 index caedc17..0000000 --- a/webserv/config/config_validator/RequiredDirectiveRule.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "webserv/config/config_validator/ValidationResult.hpp" -#include "webserv/config/config_validator/RequiredDirectiveRule.hpp" -#include -#include - -RequiredDirectiveRule::RequiredDirectiveRule() - : AValidationRule("RequiredDirectiveRule", "Ensures that a required directive is present in the configuration", true) -{ -} - -ValidationResult RequiredDirectiveRule::validateValue(const AConfig *config, const std::string &directiveName) const -{ - return ValidationResult::success(); -} \ No newline at end of file diff --git a/webserv/config/directive/BoolDirective.cpp b/webserv/config/directive/BoolDirective.cpp index b0f8276..c3d30fa 100644 --- a/webserv/config/directive/BoolDirective.cpp +++ b/webserv/config/directive/BoolDirective.cpp @@ -1,5 +1,6 @@ -#include // for ADirective #include // for IntDirective + +#include // for ADirective #include // for DirectiveValueType #include // for trim diff --git a/webserv/config/directive/DirectiveFactory.cpp b/webserv/config/directive/DirectiveFactory.cpp index 23c0893..0158f54 100644 --- a/webserv/config/directive/DirectiveFactory.cpp +++ b/webserv/config/directive/DirectiveFactory.cpp @@ -1,5 +1,6 @@ -#include // for BoolDirective #include // for DirectiveFactory + +#include // for BoolDirective #include // for IntDirective #include // for IntStringDirective #include // for SizeDirective diff --git a/webserv/config/directive/IntStringDirective.cpp b/webserv/config/directive/IntStringDirective.cpp index c2f928f..1b5fb18 100644 --- a/webserv/config/directive/IntStringDirective.cpp +++ b/webserv/config/directive/IntStringDirective.cpp @@ -1,6 +1,7 @@ +#include // for IntDirective + #include // for ADirective #include // for DirectiveValueType -#include // for IntDirective #include // for trim #include // for basic_istringstream, basic_istream::operator>>, istringstream diff --git a/webserv/config/directive/SizeDirective.cpp b/webserv/config/directive/SizeDirective.cpp index 895ef97..53949a8 100644 --- a/webserv/config/directive/SizeDirective.cpp +++ b/webserv/config/directive/SizeDirective.cpp @@ -1,6 +1,7 @@ +#include // for SizeDirective + #include // for ADirective #include // for DirectiveValueType -#include // for SizeDirective #include // for trim #include // for __transform_fn, transform diff --git a/webserv/config/validation/ConfigValidator.cpp b/webserv/config/validation/ConfigValidator.cpp new file mode 100644 index 0000000..3471fe9 --- /dev/null +++ b/webserv/config/validation/ConfigValidator.cpp @@ -0,0 +1,45 @@ +#include + +#include // for ValidationEngine +#include // for AValidationRule +#include // for AllowedValuesRule +#include // for PortValidationRule +#include // for LOCATION, Log + +#include // for basic_string, string + +ConfigValidator::ConfigValidator(const GlobalConfig *config) : engine_(std::make_unique(config)) +{ + Log::trace(LOCATION); + + /*Global Directive Rules*/ + + /*Server Directive Rules*/ + engine_->addServerRule("listen", std::make_unique()); + + /*Location Directive Rules*/ + engine_->addLocationRule("allowed_methods", + std::make_unique(std::vector{"GET", "POST", "DELETE"})); + + engine_->validate(); +} + +std::vector ConfigValidator::getValidationResults() const +{ + return engine_->getValidationResults(); +} + +std::vector ConfigValidator::getErrors() const +{ + return engine_->getErrors(); +} + +std::vector ConfigValidator::getWarnings() const +{ + return engine_->getWarnings(); +} + +bool ConfigValidator::hasErrors() const +{ + return engine_->hasErrors(); +} diff --git a/webserv/config/config_validator/ConfigValidator.hpp b/webserv/config/validation/ConfigValidator.hpp similarity index 76% rename from webserv/config/config_validator/ConfigValidator.hpp rename to webserv/config/validation/ConfigValidator.hpp index 60c9576..cdf03bd 100644 --- a/webserv/config/config_validator/ConfigValidator.hpp +++ b/webserv/config/validation/ConfigValidator.hpp @@ -1,9 +1,13 @@ #pragma once +#include // for ValidationEngine +#include // for ValidationResult + +#include // for unique_ptr +#include // for vector -#include "webserv/config/config_validator/ValidationEngine.hpp" -#include class GlobalConfig; + class ConfigValidator { public: @@ -22,5 +26,4 @@ class ConfigValidator private: std::unique_ptr engine_; - }; \ No newline at end of file diff --git a/webserv/config/config_validator/ValidationEngine.cpp b/webserv/config/validation/ValidationEngine.cpp similarity index 86% rename from webserv/config/config_validator/ValidationEngine.cpp rename to webserv/config/validation/ValidationEngine.cpp index e66d4a6..45145ef 100644 --- a/webserv/config/config_validator/ValidationEngine.cpp +++ b/webserv/config/validation/ValidationEngine.cpp @@ -1,11 +1,14 @@ -#include "webserv/config/config_validator/ValidationEngine.hpp" +#include -#include "webserv/config/AConfig.hpp" -#include "webserv/config/LocationConfig.hpp" -#include "webserv/config/config_validator/AValidationRule.hpp" -#include "webserv/config/config_validator/ValidationResult.hpp" -#include "webserv/config/directive/ADirective.hpp" -#include "webserv/log/Log.hpp" +#include // for AConfig +#include // for GlobalConfig +#include // for LocationConfig +#include // for ServerConfig +#include // for ValidationResult +#include // for AValidationRule +#include // for Log, LOCATION + +#include // for move, get void ValidationEngine::addGlobalRule(const std::string &directiveName, std::unique_ptr rule) { diff --git a/webserv/config/config_validator/ValidationEngine.hpp b/webserv/config/validation/ValidationEngine.hpp similarity index 73% rename from webserv/config/config_validator/ValidationEngine.hpp rename to webserv/config/validation/ValidationEngine.hpp index 50cc901..6b6b000 100644 --- a/webserv/config/config_validator/ValidationEngine.hpp +++ b/webserv/config/validation/ValidationEngine.hpp @@ -1,15 +1,21 @@ #pragma once -#include "webserv/config/GlobalConfig.hpp" -#include "webserv/config/config_validator/AValidationRule.hpp" -#include "webserv/config/config_validator/ValidationResult.hpp" -#include "webserv/config/LocationConfig.hpp" -#include "webserv/config/ServerConfig.hpp" -#include "webserv/config/AConfig.hpp" -#include -#include -#include -#include +#include +#include +#include +#include +#include // for ValidationResult +#include // for AValidationRule + +#include // for map +#include // for unique_ptr +#include // for basic_string, string, operator<=> +#include // for vector + +class AConfig; +class GlobalConfig; +class LocationConfig; +class ServerConfig; class ValidationEngine { diff --git a/webserv/config/config_validator/ValidationResult.cpp b/webserv/config/validation/ValidationResult.cpp similarity index 85% rename from webserv/config/config_validator/ValidationResult.cpp rename to webserv/config/validation/ValidationResult.cpp index a45be6d..4df4df7 100644 --- a/webserv/config/config_validator/ValidationResult.cpp +++ b/webserv/config/validation/ValidationResult.cpp @@ -1,5 +1,8 @@ -#include -#include +#include + +#include // for Log + +#include // for move ValidationResult::ValidationResult(Type type, std::string message) : type_(type), message_(std::move(message)) {} diff --git a/webserv/config/config_validator/ValidationResult.hpp b/webserv/config/validation/ValidationResult.hpp similarity index 93% rename from webserv/config/config_validator/ValidationResult.hpp rename to webserv/config/validation/ValidationResult.hpp index 8874cf5..d8dd04c 100644 --- a/webserv/config/config_validator/ValidationResult.hpp +++ b/webserv/config/validation/ValidationResult.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include +#include class ValidationResult { @@ -25,7 +25,7 @@ class ValidationResult static ValidationResult warning(const std::string &message); [[nodiscard]] bool isValidResult() const; - [[nodiscard]] ValidationResult::Type getType() const ; + [[nodiscard]] ValidationResult::Type getType() const; [[nodiscard]] std::string getMessage() const; private: diff --git a/webserv/config/config_validator/AValidationRule.cpp b/webserv/config/validation/directive_rules/AValidationRule.cpp similarity index 76% rename from webserv/config/config_validator/AValidationRule.cpp rename to webserv/config/validation/directive_rules/AValidationRule.cpp index 262299c..1902610 100644 --- a/webserv/config/config_validator/AValidationRule.cpp +++ b/webserv/config/validation/directive_rules/AValidationRule.cpp @@ -1,8 +1,10 @@ -#include -#include -#include -#include -#include +#include + +#include // for AConfig +#include // for ValidationResult +#include // for LOCATION, Log + +#include // for move AValidationRule::AValidationRule(std::string ruleName, std::string description, bool requiresValue) : ruleName_(std::move(ruleName)), description_(std::move(description)), requiresValue_(requiresValue) diff --git a/webserv/config/config_validator/AValidationRule.hpp b/webserv/config/validation/directive_rules/AValidationRule.hpp similarity index 85% rename from webserv/config/config_validator/AValidationRule.hpp rename to webserv/config/validation/directive_rules/AValidationRule.hpp index 75aeafe..7ef4465 100644 --- a/webserv/config/config_validator/AValidationRule.hpp +++ b/webserv/config/validation/directive_rules/AValidationRule.hpp @@ -1,6 +1,5 @@ #pragma once -#include - +#include // for string, basic_string class ValidationResult; class ADirective; @@ -18,7 +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/config_validator/AllowedValuesRule.cpp b/webserv/config/validation/directive_rules/AllowedValuesRule.cpp similarity index 69% rename from webserv/config/config_validator/AllowedValuesRule.cpp rename to webserv/config/validation/directive_rules/AllowedValuesRule.cpp index 75ab0e4..efc9c58 100644 --- a/webserv/config/config_validator/AllowedValuesRule.cpp +++ b/webserv/config/validation/directive_rules/AllowedValuesRule.cpp @@ -1,12 +1,16 @@ -#include -#include -#include -#include +#include -#include // for find +#include // for AConfig +#include // for ADirective +#include // for DirectiveValue +#include // for ValidationResult +#include // for AValidationRule -#include // for string, basic_string, operator+, char_traits -#include // for vector +#include // for identity +#include // for __find_fn, find +#include // for basic_string, allocator, operator+, char_traits, string, operator== +#include // for move +#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/AllowedValuesRule.hpp b/webserv/config/validation/directive_rules/AllowedValuesRule.hpp new file mode 100644 index 0000000..aa8a7ba --- /dev/null +++ b/webserv/config/validation/directive_rules/AllowedValuesRule.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include // for AValidationRule + +#include // for basic_string, string +#include // for vector + +class AConfig; + +class AllowedValuesRule : public AValidationRule +{ + public: + explicit AllowedValuesRule(const std::vector &allowedValues, bool requiresValue = true); + + private: + [[nodiscard]] ValidationResult validateValue(const AConfig *config, + const std::string &directiveName) const override; + std::vector allowedValues_; +}; \ No newline at end of file diff --git a/webserv/config/validation/directive_rules/PortValidationRule.cpp b/webserv/config/validation/directive_rules/PortValidationRule.cpp new file mode 100644 index 0000000..fc558ad --- /dev/null +++ b/webserv/config/validation/directive_rules/PortValidationRule.cpp @@ -0,0 +1,34 @@ +#include + +#include // for AConfig +#include // for ADirective +#include // for DirectiveValue +#include // for ValidationResult +#include // for AValidationRule +#include // for LOCATION, Log + +#include // for operator+, basic_string, allocator, char_traits, to_string, string + +PortValidationRule::PortValidationRule(bool requiresValue) + : AValidationRule("PortValidationRule", "Validates that the port number is within the valid range (1-65535)", + requiresValue) +{ +} + +ValidationResult PortValidationRule::validateValue(const AConfig *config, const std::string &directiveName) const +{ + Log::trace(LOCATION); + const ADirective *directive = config->getDirective(directiveName); + if (!directive->getValue().holds()) + { + return ValidationResult::error("Directive '" + directive->getName() + "' does not hold an integer value"); + } + + int port = directive->getValue().get(); + if (port < 1 || port > 65535) + { + return ValidationResult::error("Port number " + std::to_string(port) + " is out of valid range (1-65535)"); + } + + return ValidationResult::success(); +} diff --git a/webserv/config/validation/directive_rules/PortValidationRule.hpp b/webserv/config/validation/directive_rules/PortValidationRule.hpp new file mode 100644 index 0000000..e080193 --- /dev/null +++ b/webserv/config/validation/directive_rules/PortValidationRule.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include // for AValidationRule + +#include // for string + +class AConfig; + +class PortValidationRule : public AValidationRule +{ + public: + PortValidationRule(bool requiresValue = true); + + private: + [[nodiscard]] ValidationResult validateValue(const AConfig *config, + const std::string &directiveName) const override; +}; \ No newline at end of file diff --git a/webserv/config/validation/directive_rules/RequiredDirectiveRule.cpp b/webserv/config/validation/directive_rules/RequiredDirectiveRule.cpp new file mode 100644 index 0000000..460c939 --- /dev/null +++ b/webserv/config/validation/directive_rules/RequiredDirectiveRule.cpp @@ -0,0 +1,15 @@ +#include + +#include // for ValidationResult +#include // for AValidationRule + +RequiredDirectiveRule::RequiredDirectiveRule() + : AValidationRule("RequiredDirectiveRule", "Ensures that a required directive is present in the configuration", + true) +{ +} + +ValidationResult RequiredDirectiveRule::validateValue(const AConfig *config, const std::string &directiveName) const +{ + return ValidationResult::success(); +} \ No newline at end of file diff --git a/webserv/config/config_validator/RequiredDirectiveRule.hpp b/webserv/config/validation/directive_rules/RequiredDirectiveRule.hpp similarity index 70% rename from webserv/config/config_validator/RequiredDirectiveRule.hpp rename to webserv/config/validation/directive_rules/RequiredDirectiveRule.hpp index 329c14b..139c6cb 100644 --- a/webserv/config/config_validator/RequiredDirectiveRule.hpp +++ b/webserv/config/validation/directive_rules/RequiredDirectiveRule.hpp @@ -1,8 +1,8 @@ #pragma once -#include "webserv/config/config_validator/AValidationRule.hpp" +#include // for AValidationRule -#include +#include // for string class AConfig; diff --git a/webserv/handler/ErrorHandler.hpp b/webserv/handler/ErrorHandler.hpp index 5fa159f..ffdb9c7 100644 --- a/webserv/handler/ErrorHandler.hpp +++ b/webserv/handler/ErrorHandler.hpp @@ -1,8 +1,8 @@ #pragma once -#include "webserv/http/HttpResponse.hpp" - #include +#include + #include class ErrorHandler diff --git a/webserv/handler/Errorhandler.cpp b/webserv/handler/Errorhandler.cpp index f219ab3..96319bb 100644 --- a/webserv/handler/Errorhandler.cpp +++ b/webserv/handler/Errorhandler.cpp @@ -1,11 +1,10 @@ -#include "webserv/http/HttpResponse.hpp" - #include // for AConfig #include // for ConfigManager #include // for GlobalConfig #include // for ErrorHandler #include // for StatusCodeInfo, CRLF, DOUBLE_CRLF, INTERNAL_SERVER_ERROR, statusCodeInfos -#include // for Log +#include +#include // for Log #include // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream #include diff --git a/webserv/handler/FileHandler.cpp b/webserv/handler/FileHandler.cpp index 16ebe86..fe17c4d 100644 --- a/webserv/handler/FileHandler.cpp +++ b/webserv/handler/FileHandler.cpp @@ -1,20 +1,21 @@ -#include "webserv/handler/ErrorHandler.hpp" -#include "webserv/http/HttpConstants.hpp" -#include "webserv/http/HttpResponse.hpp" - -#include #include -#include -#include -#include -#include -#include -#include // for errno -#include // for strerror, strlen -#include -#include -#include +#include // for LocationConfig +#include // for ErrorHandler +#include // for MIMETypes +#include // for URIParser +#include // for NOT_FOUND, FORBIDDEN, OK +#include // for HttpResponse +#include // for Log, LOCATION +#include // for joinPath, getExtension, isFile, readBinaryFile + +#include // for identity +#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 move +#include // for vector FileHandler::FileHandler(const LocationConfig *location, const URIParser &uriParser) : location_(location), uriParser_(uriParser) diff --git a/webserv/handler/FileHandler.hpp b/webserv/handler/FileHandler.hpp index ca59526..51e714c 100644 --- a/webserv/handler/FileHandler.hpp +++ b/webserv/handler/FileHandler.hpp @@ -1,12 +1,15 @@ #pragma once -#include "webserv/config/LocationConfig.hpp" -#include "webserv/handler/URIParser.hpp" -#include "webserv/http/HttpResponse.hpp" +#include +#include +#include // for HttpResponse -#include -#include -#include +#include // for uint8_t +#include // for unique_ptr +#include // for string + +class LocationConfig; +class URIParser; class FileHandler { diff --git a/webserv/handler/MIMETypes.cpp b/webserv/handler/MIMETypes.cpp index 7a55684..3520508 100644 --- a/webserv/handler/MIMETypes.cpp +++ b/webserv/handler/MIMETypes.cpp @@ -1,37 +1,42 @@ #include +#include // for pair - -MIMETypes::MIMETypes() { - initializeDefaults(); +MIMETypes::MIMETypes() +{ + initializeDefaults(); } -std::string MIMETypes::getType(const std::string& extension) const { - auto it = mimeMap.find(extension); - if (it != mimeMap.end()) { - return it->second; - } - return "application/octet-stream"; // Default MIME type +std::string MIMETypes::getType(const std::string &extension) const +{ + auto it = mimeMap.find(extension); + if (it != mimeMap.end()) + { + return it->second; + } + return "application/octet-stream"; // Default MIME type } -void MIMETypes::setType(const std::string& extension, const std::string& mimeType) { - mimeMap[extension] = mimeType; +void MIMETypes::setType(const std::string &extension, const std::string &mimeType) +{ + mimeMap[extension] = mimeType; } -void MIMETypes::initializeDefaults() { - mimeMap["html"] = "text/html"; - mimeMap["htm"] = "text/html"; - mimeMap["css"] = "text/css"; - mimeMap["js"] = "application/javascript"; - mimeMap["json"] = "application/json"; - mimeMap["png"] = "image/png"; - mimeMap["jpg"] = "image/jpeg"; - mimeMap["jpeg"] = "image/jpeg"; - mimeMap["gif"] = "image/gif"; - mimeMap["svg"] = "image/svg+xml"; - mimeMap["txt"] = "text/plain"; - mimeMap["xml"] = "application/xml"; - mimeMap["pdf"] = "application/pdf"; - // Add more default MIME types as needed +void MIMETypes::initializeDefaults() +{ + mimeMap["html"] = "text/html"; + mimeMap["htm"] = "text/html"; + mimeMap["css"] = "text/css"; + mimeMap["js"] = "application/javascript"; + mimeMap["json"] = "application/json"; + mimeMap["png"] = "image/png"; + mimeMap["jpg"] = "image/jpeg"; + mimeMap["jpeg"] = "image/jpeg"; + mimeMap["gif"] = "image/gif"; + mimeMap["svg"] = "image/svg+xml"; + mimeMap["txt"] = "text/plain"; + mimeMap["xml"] = "application/xml"; + mimeMap["pdf"] = "application/pdf"; + // Add more default MIME types as needed } \ No newline at end of file diff --git a/webserv/handler/MIMETypes.hpp b/webserv/handler/MIMETypes.hpp index 95f653c..ba7af99 100644 --- a/webserv/handler/MIMETypes.hpp +++ b/webserv/handler/MIMETypes.hpp @@ -1,24 +1,24 @@ #ifndef MIMETYPES_HPP #define MIMETYPES_HPP -#include #include +#include -class MIMETypes { -public: - MIMETypes(); +class MIMETypes +{ + public: + MIMETypes(); + // Returns the MIME type for a given file extension (e.g., "html" -> "text/html") + [[nodiscard]] std::string getType(const std::string &extension) const; - // Returns the MIME type for a given file extension (e.g., "html" -> "text/html") - [[nodiscard]] std::string getType(const std::string& extension) const; + // Adds or updates a MIME type mapping + void setType(const std::string &extension, const std::string &mimeType); - // Adds or updates a MIME type mapping - void setType(const std::string& extension, const std::string& mimeType); + private: + std::map mimeMap; -private: - std::map mimeMap; - - void initializeDefaults(); + void initializeDefaults(); }; #endif // MIMETYPES_HPP \ No newline at end of file diff --git a/webserv/handler/URIParser.cpp b/webserv/handler/URIParser.cpp index 19a8df2..3b08225 100644 --- a/webserv/handler/URIParser.cpp +++ b/webserv/handler/URIParser.cpp @@ -1,8 +1,13 @@ -#include -#include #include -#include // for stat, S_ISREG, S_ISDIR +#include // for LocationConfig +#include // for ServerConfig + +#include // for optional +#include // for vector + +#include // for size_t +#include // for stat, S_ISDIR, S_ISREG URIParser::URIParser(const std::string &uri, const ServerConfig &serverConfig) : _locationConfig(nullptr) { @@ -22,7 +27,7 @@ URIParser::URIParser(const std::string &uri, const ServerConfig &serverConfig) : } root_ = _locationConfig != nullptr ? _locationConfig->get("root").value_or("") : ""; - if (!root_.empty() && root_.back() == '/' ) + if (!root_.empty() && root_.back() == '/') { root_.pop_back(); // Remove trailing slash to avoid double slashes in path } @@ -60,7 +65,6 @@ std::string URIParser::getExtension() const return filename.substr(lastDot + 1); } - LocationConfig const *URIParser::getLocation() const { return _locationConfig; @@ -91,4 +95,3 @@ bool URIParser::isValid() const struct stat pathStat{}; return stat(getFilePath().c_str(), &pathStat) == 0; } - diff --git a/webserv/handler/URIParser.hpp b/webserv/handler/URIParser.hpp index bf6aa1c..9da9a1a 100644 --- a/webserv/handler/URIParser.hpp +++ b/webserv/handler/URIParser.hpp @@ -1,21 +1,24 @@ #pragma once -#include "webserv/config/LocationConfig.hpp" -#include "webserv/config/ServerConfig.hpp" -#include "webserv/server/Server.hpp" +#include +#include +#include + +#include // for string, basic_string + +class LocationConfig; +class ServerConfig; class URIParser { public: URIParser(const std::string &uri, const ServerConfig &serverConfig); - [[nodiscard]] std::string getFilePath() const; [[nodiscard]] std::string getFilename() const; [[nodiscard]] std::string getExtension() const; - - [[nodiscard]] const LocationConfig *getLocation() const ; + [[nodiscard]] const LocationConfig *getLocation() const; [[nodiscard]] bool isFile() const; diff --git a/webserv/http/HttpConstants.cpp b/webserv/http/HttpConstants.cpp index 6c0b85e..3126281 100644 --- a/webserv/http/HttpConstants.cpp +++ b/webserv/http/HttpConstants.cpp @@ -1,4 +1,4 @@ -#include "HttpConstants.hpp" +#include #include diff --git a/webserv/http/HttpConstants.hpp b/webserv/http/HttpConstants.hpp index 2ac0519..e3ff592 100644 --- a/webserv/http/HttpConstants.hpp +++ b/webserv/http/HttpConstants.hpp @@ -1,8 +1,11 @@ #pragma once -#include -#include -#include +#include // for array +#include // for uint16_t +#include // for string +#include // for string_view + +#include // for size_t namespace Http { diff --git a/webserv/http/HttpHeaders.cpp b/webserv/http/HttpHeaders.cpp index 681dccc..4c1d606 100644 --- a/webserv/http/HttpHeaders.cpp +++ b/webserv/http/HttpHeaders.cpp @@ -1,5 +1,6 @@ -#include // for CRLF #include // for HttpHeaders + +#include // for CRLF #include #include // for trim diff --git a/webserv/http/HttpRequest.cpp b/webserv/http/HttpRequest.cpp index 2a868d5..a16097e 100644 --- a/webserv/http/HttpRequest.cpp +++ b/webserv/http/HttpRequest.cpp @@ -1,6 +1,7 @@ +#include + #include // for Client #include // for CRLF, DOUBLE_CRLF, BAD_REQUEST -#include #include // for Log, LOCATION #include // for stoul diff --git a/webserv/http/HttpResponse.cpp b/webserv/http/HttpResponse.cpp index 244b165..1ef35dc 100644 --- a/webserv/http/HttpResponse.cpp +++ b/webserv/http/HttpResponse.cpp @@ -1,7 +1,7 @@ -#include "webserv/http/HttpConstants.hpp" - #include +#include + #include #include diff --git a/webserv/http/HttpResponse.hpp b/webserv/http/HttpResponse.hpp index 9f0c45e..ee2930b 100644 --- a/webserv/http/HttpResponse.hpp +++ b/webserv/http/HttpResponse.hpp @@ -1,12 +1,12 @@ #pragma once -#include "webserv/http/HttpHeaders.hpp" -#include "webserv/log/Log.hpp" +#include // for HttpHeaders +#include // for LOCATION, Log -#include -#include -#include -#include +#include // for uint8_t +#include // for unique_ptr +#include // for string +#include // for vector class Client; @@ -20,7 +20,7 @@ class HttpResponse HttpResponse(HttpResponse &&other) noexcept = default; // Move constructor HttpResponse &operator=(HttpResponse &&other) noexcept = default; // Move assignment - ~HttpResponse() {Log::trace(LOCATION);}; + ~HttpResponse() { Log::trace(LOCATION); }; void addHeader(const std::string &key, const std::string &value); diff --git a/webserv/main.cpp b/webserv/main.cpp index 6b13eba..b7aa868 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -1,10 +1,12 @@ -#include // for ConfigManager -#include // for Log, LOCATION -#include // for Server -#include // for ConfigValidator +#include // for ConfigManager +#include // for ConfigValidator +#include // for ValidationResult +#include // for Log +#include // for Server -#include // for basic_ostream, operator<<, cerr, ios_base -#include // for basic_string, char_traits, allocator, operator+, operator<=> +#include // for ios_base +#include // for allocator, basic_string, char_traits, operator+, string +#include // for vector int main(int argc, char **argv) { diff --git a/webserv/router/Router.cpp b/webserv/router/Router.cpp index 9569405..3a8d132 100644 --- a/webserv/router/Router.cpp +++ b/webserv/router/Router.cpp @@ -1,16 +1,18 @@ -#include "webserv/config/AConfig.hpp" -#include "webserv/config/ConfigManager.hpp" -#include "webserv/config/ServerConfig.hpp" -#include "webserv/handler/ErrorHandler.hpp" -#include "webserv/handler/FileHandler.hpp" -#include "webserv/handler/URIParser.hpp" -#include "webserv/http/HttpResponse.hpp" -#include "webserv/log/Log.hpp" - #include -#include -#include +#include // for ConfigManager +#include // for ServerConfig +#include // for ErrorHandler +#include // for FileHandler +#include // for URIParser +#include // for HttpHeaders +#include // for LOCATION, Log + +#include // for unique_ptr +#include // for optional +#include // for basic_string, string + +class LocationConfig; Router::Router() {} diff --git a/webserv/router/Router.hpp b/webserv/router/Router.hpp index 83952ed..04bd855 100644 --- a/webserv/router/Router.hpp +++ b/webserv/router/Router.hpp @@ -1,12 +1,14 @@ #pragma once -#include "webserv/config/LocationConfig.hpp" +#include +#include // for HttpRequest +#include // for HttpResponse -#include -#include +#include // for unique_ptr +#include // for string -#include -#include +class LocationConfig; +class ServerConfig; class Router { diff --git a/webserv/server/Server.cpp b/webserv/server/Server.cpp index 1323dcf..a1449e7 100644 --- a/webserv/server/Server.cpp +++ b/webserv/server/Server.cpp @@ -1,17 +1,19 @@ +#include + #include // for Client #include // for ConfigManager #include // for ServerConfig -#include // for Log -#include +#include // for Log, LOCATION #include // for Socket #include // for errno -#include // for strerror, strlen +#include // for strerror #include // for exception #include // for unique_ptr, allocator, make_unique +#include // for optional #include // for runtime_error #include // for basic_string, operator+, to_string, char_traits, string -#include // for unordered_map, unordered_map<>::container_type +#include // for unordered_map, operator== #include // for move, pair #include // for vector @@ -20,6 +22,8 @@ #include // for ssize_t #include // for close +class Router; + Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(0)), configManager_(configManager), router_() { diff --git a/webserv/server/Server.hpp b/webserv/server/Server.hpp index b16ee89..bd50b9b 100644 --- a/webserv/server/Server.hpp +++ b/webserv/server/Server.hpp @@ -1,10 +1,9 @@ #pragma once -#include "webserv/router/Router.hpp" - #include #include #include // for ServerConfig +#include // for Router #include // for Socket #include // for uint32_t diff --git a/webserv/utils/FileUtils.cpp b/webserv/utils/FileUtils.cpp index 73c78be..2be0677 100644 --- a/webserv/utils/FileUtils.cpp +++ b/webserv/utils/FileUtils.cpp @@ -1,12 +1,13 @@ -#include "webserv/log/Log.hpp" - #include -#include // for strlen -#include -#include // for string +#include // for Log, LOCATION -#include // for stat, S_ISREG, S_ISDIR +#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== +#include // for basic_string, string, char_traits, operator+ + +#include // for stat, S_ISDIR, S_ISREG namespace FileUtils {