From 36961fc468cdfe9deaf9e312c51019214a303c48 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Wed, 24 Sep 2025 13:01:27 +0200 Subject: [PATCH] fix: add utils namespace to functions --- webserv/config/ConfigManager.cpp | 6 +++--- webserv/config/LocationConfig.cpp | 2 +- webserv/config/ServerConfig.cpp | 6 +++--- webserv/http/HttpHeaders.cpp | 14 +++++++++++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/webserv/config/ConfigManager.cpp b/webserv/config/ConfigManager.cpp index 3057e66..128cd5e 100644 --- a/webserv/config/ConfigManager.cpp +++ b/webserv/config/ConfigManager.cpp @@ -39,9 +39,9 @@ void removeEmptyLines(std::string &str) while (std::getline(stream, line)) { - if (!trim(line).empty()) + if (!utils::trim(line).empty()) { - result += trimSemi(trim(line)) + '\n'; + result += utils::trimSemi(utils::trim(line)) + '\n'; } } str = result; @@ -97,7 +97,7 @@ void ConfigManager::parseConfigFile(const std::string &filePath) } // Add global declarations before this server block globalDeclarations += content.substr(pos, serverPos - pos); - size_t closeBrace = findCorrespondingClosingBrace(content, bracePos); + size_t closeBrace = utils::findCorrespondingClosingBrace(content, bracePos); if (closeBrace == std::string::npos) { throw std::runtime_error("Malformed block in config file."); diff --git a/webserv/config/LocationConfig.cpp b/webserv/config/LocationConfig.cpp index 3ad2e59..f9ef194 100644 --- a/webserv/config/LocationConfig.cpp +++ b/webserv/config/LocationConfig.cpp @@ -22,7 +22,7 @@ void LocationConfig::parseDirectives(const std::string &declarations) while (std::getline(stream, line)) { std::string directive; - std::istringstream lineStream{trim(line)}; + std::istringstream lineStream{utils::trim(line)}; lineStream >> directive; if (!directive.empty()) { diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index fa3e60f..3cb62ba 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -36,10 +36,10 @@ void ServerConfig::parseServerBlock(const std::string &block) serverDeclarations += block.substr(pos); break; } - std::string locationPath = trim(block.substr(locationPos, bracePos - (locationPos))); + std::string locationPath = utils::trim(block.substr(locationPos, bracePos - (locationPos))); // Add global declarations before this server block serverDeclarations += block.substr(pos, locationPos - pos); - size_t closeBrace = findCorrespondingClosingBrace(block, bracePos); + size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos); if (closeBrace == std::string::npos) { throw std::runtime_error("Malformed block in config file."); @@ -111,7 +111,7 @@ void ServerConfig::parseDirectives(const std::string &declarations) } else if (directive == "error_page") { - int statusCode = std::stoi(value); + int statusCode = std::stoi("-1"); std::string errorPagePath; lineStream >> errorPagePath; Log::debug("Set error_page for status " + std::to_string(statusCode) + " to " + errorPagePath); diff --git a/webserv/http/HttpHeaders.cpp b/webserv/http/HttpHeaders.cpp index 55f561a..a85e070 100644 --- a/webserv/http/HttpHeaders.cpp +++ b/webserv/http/HttpHeaders.cpp @@ -1,3 +1,5 @@ +#include "webserv/log/Log.hpp" + #include // for trim #include // for CRLF #include // for HttpHeaders @@ -14,12 +16,18 @@ std::optional HttpHeaders::getContentLength() const { return std::nullopt; } + if (value.find_first_not_of("0123456789") != std::string::npos) + { + Log::warning("Non-numeric Content-Length header value: " + value); + return std::nullopt; + } try { - return std::stoul(value); + return utils::stoul(value); } catch (...) { + Log::warning("Invalid Content-Length header value: " + value); return std::nullopt; } } @@ -90,8 +98,8 @@ void HttpHeaders::parse(const std::string &rawHeaders) { std::string name = line.substr(0, col); std::string value = line.substr(col + 1); - name = trim(name); - value = trim(value); + name = utils::trim(name); + value = utils::trim(value); this->add(name, value); } start = end + Http::Protocol::CRLF.size();