From cc2ed7af56cf05dccbfa55ccada99d69f3f84d4b Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 9 Oct 2025 21:16:42 +0200 Subject: [PATCH] fix errors from pedantic --- .../validation/structural_rules/RequiredDirectivesRule.cpp | 6 +++--- webserv/handler/ErrorHandler.hpp | 7 ++++--- webserv/handler/Errorhandler.cpp | 7 ++++--- webserv/handler/URI.cpp | 3 +-- webserv/http/HttpResponse.cpp | 3 ++- webserv/http/HttpResponse.hpp | 4 ++-- webserv/log/Channel.cpp | 4 ++-- webserv/socket/Socket.cpp | 2 +- webserv/utils/FileUtils.cpp | 7 ++++++- 9 files changed, 25 insertions(+), 18 deletions(-) diff --git a/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp b/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp index 9efbea0..032cdc0 100644 --- a/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp +++ b/webserv/config/validation/structural_rules/RequiredDirectivesRule.cpp @@ -22,13 +22,13 @@ ValidationResult validateUniversal(const AConfig *config, std::string configType for (const auto &directive : DirectiveFactory::supportedDirectives) { - if (directive.context.find(std::toupper(configType[0])) != std::string::npos && + 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(std::toupper(configType[0])) == std::string::npos && - directive.context.find(std::tolower(configType[0])) == std::string::npos) && + 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/handler/ErrorHandler.hpp b/webserv/handler/ErrorHandler.hpp index ffdb9c7..f367f69 100644 --- a/webserv/handler/ErrorHandler.hpp +++ b/webserv/handler/ErrorHandler.hpp @@ -4,14 +4,15 @@ #include #include +#include class ErrorHandler { public: - static std::unique_ptr getErrorResponse(int statusCode, const AConfig *config = nullptr); + static std::unique_ptr getErrorResponse(uint16_t statusCode, const AConfig *config = nullptr); private: - static std::string generateErrorPage(int statusCode, const AConfig *config = nullptr); - static std::string generateDefaultErrorPage(int statusCode); + static std::string generateErrorPage(uint16_t statusCode, const AConfig *config = nullptr); + static std::string generateDefaultErrorPage(uint16_t statusCode); static std::string getErrorPageFile(const std::string &path); }; \ No newline at end of file diff --git a/webserv/handler/Errorhandler.cpp b/webserv/handler/Errorhandler.cpp index 96319bb..78477ec 100644 --- a/webserv/handler/Errorhandler.cpp +++ b/webserv/handler/Errorhandler.cpp @@ -10,8 +10,9 @@ #include #include // for basic_stringstream #include // for basic_string, operator+, allocator, char_traits, string, to_string +#include -std::unique_ptr ErrorHandler::getErrorResponse(int statusCode, const AConfig *config) +std::unique_ptr ErrorHandler::getErrorResponse(uint16_t statusCode, const AConfig *config) { std::string statusMessage = Http::getStatusCodeReason(statusCode); Log::warning("Generating error response: " + std::to_string(statusCode) + " " + statusMessage); @@ -25,7 +26,7 @@ std::unique_ptr ErrorHandler::getErrorResponse(int statusCode, con return response; } -std::string ErrorHandler::generateErrorPage(int statusCode, const AConfig *config) +std::string ErrorHandler::generateErrorPage(uint16_t statusCode, const AConfig *config) { Log::trace(LOCATION); if (config == nullptr) @@ -46,7 +47,7 @@ std::string ErrorHandler::generateErrorPage(int statusCode, const AConfig *confi return generateDefaultErrorPage(statusCode); } -std::string ErrorHandler::generateDefaultErrorPage(int statusCode) +std::string ErrorHandler::generateDefaultErrorPage(uint16_t statusCode) { Log::info("Generating default error page"); std::string statusMessage = Http::getStatusCodeReason(statusCode); diff --git a/webserv/handler/URI.cpp b/webserv/handler/URI.cpp index 4e53de8..3c55320 100644 --- a/webserv/handler/URI.cpp +++ b/webserv/handler/URI.cpp @@ -8,8 +8,7 @@ #include // for optional -#include // for size_t -#include // for stat, S_ISDIR, S_ISREG +#include // for size_t URI::URI(const HttpRequest &request, const ServerConfig &serverConfig) : uriTrimmed_(utils::trim(request.getTarget(), "/")), config_(matchConfig(uriTrimmed_, serverConfig)) diff --git a/webserv/http/HttpResponse.cpp b/webserv/http/HttpResponse.cpp index 1ef35dc..03984c5 100644 --- a/webserv/http/HttpResponse.cpp +++ b/webserv/http/HttpResponse.cpp @@ -4,6 +4,7 @@ #include #include +#include HttpResponse::HttpResponse() : headers_(std::make_unique()) {} @@ -34,7 +35,7 @@ void HttpResponse::setBody(const std::string &body) setComplete(); } -void HttpResponse::setStatus(int statusCode) +void HttpResponse::setStatus(uint16_t statusCode) { statusCode_ = statusCode; } diff --git a/webserv/http/HttpResponse.hpp b/webserv/http/HttpResponse.hpp index ee2930b..35b5130 100644 --- a/webserv/http/HttpResponse.hpp +++ b/webserv/http/HttpResponse.hpp @@ -32,7 +32,7 @@ class HttpResponse void setComplete(); - void setStatus(int statusCode); + void setStatus(uint16_t statusCode); [[nodiscard]] bool isComplete() const; @@ -47,5 +47,5 @@ class HttpResponse std::vector body_; std::unique_ptr headers_; bool complete_ = false; - int statusCode_ = 200; + uint16_t statusCode_ = 200; }; \ No newline at end of file diff --git a/webserv/log/Channel.cpp b/webserv/log/Channel.cpp index 322cf57..54dbb0a 100644 --- a/webserv/log/Channel.cpp +++ b/webserv/log/Channel.cpp @@ -15,7 +15,7 @@ std::string Channel::printContext(const std::map &cont if (value.find('\n') != std::string::npos) { - ss << "\t| " << std::right << std::setfill(' ') << "\e[1m" << key << "\e[0m" << " : \n"; + ss << "\t| " << std::right << std::setfill(' ') << "\033[1m" << key << "\033[0m" << " : \n"; std::istringstream valueStream(value); std::string line; while (std::getline(valueStream, line)) @@ -24,7 +24,7 @@ std::string Channel::printContext(const std::map &cont } continue; } - ss << "\t| " << std::right << std::setfill(' ') << "\e[1m" << key << "\e[0m" << " : " << value << "\n"; + ss << "\t| " << std::right << std::setfill(' ') << "\033[1m" << key << "\033[0m" << " : " << value << "\n"; } ss << "\n"; } diff --git a/webserv/socket/Socket.cpp b/webserv/socket/Socket.cpp index a4d8143..572215e 100644 --- a/webserv/socket/Socket.cpp +++ b/webserv/socket/Socket.cpp @@ -68,7 +68,7 @@ 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_, (struct sockaddr *)&address, sizeof(address)) < 0) // NOLINT(cppcoreguidelines-pro-type-cstyle-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"); diff --git a/webserv/utils/FileUtils.cpp b/webserv/utils/FileUtils.cpp index 8ffb7fa..91cbabf 100644 --- a/webserv/utils/FileUtils.cpp +++ b/webserv/utils/FileUtils.cpp @@ -80,9 +80,14 @@ std::vector readBinaryFile(const std::string &filepath) } std::streamsize size = file.tellg(); + if (size < 0) + { + Log::error("Failed to determine file size: " + filepath); + return {}; + } file.seekg(0, std::ios::beg); - std::vector buffer(size); + std::vector buffer(static_cast(size)); if (!file.read(buffer.data(), size)) { Log::error("Failed to read file: " + filepath);