diff --git a/webserv/config/directive/SizeDirective.cpp b/webserv/config/directive/SizeDirective.cpp index 53949a8..aad1984 100644 --- a/webserv/config/directive/SizeDirective.cpp +++ b/webserv/config/directive/SizeDirective.cpp @@ -25,7 +25,14 @@ void SizeDirective::parse(const std::string &value) { throw std::invalid_argument("Invalid size directive: " + value); } - value_ = std::stoul(number, &idx); + try + { + value_ = std::stoul(number, &idx); + } + catch (const std::exception &e) + { + throw std::invalid_argument("Invalid size directive: " + value + " - " + e.what()); + } if (idx == number.size()) { return; @@ -33,15 +40,15 @@ void SizeDirective::parse(const std::string &value) std::string suffix = number.substr(idx); if (suffix == "k") { - multiplier = 1024; + multiplier = 1024UL; } else if (suffix == "m") { - multiplier = 1024 * 1024; + multiplier = 1024UL * 1024UL; } else if (suffix == "g") { - multiplier = 1024 * 1024 * 1024; + multiplier = 1024UL * 1024UL * 1024UL; } else { diff --git a/webserv/http/HttpHeaders.cpp b/webserv/http/HttpHeaders.cpp index 0c2fb10..1c3befa 100644 --- a/webserv/http/HttpHeaders.cpp +++ b/webserv/http/HttpHeaders.cpp @@ -7,7 +7,7 @@ #include // for tolower #include // for pair -std::optional HttpHeaders::getContentLength() const noexcept +std::optional HttpHeaders::getContentLength() const { const auto &value = this->get("Content-Length"); if (value.empty()) diff --git a/webserv/http/HttpHeaders.hpp b/webserv/http/HttpHeaders.hpp index e92e803..63e8bd8 100644 --- a/webserv/http/HttpHeaders.hpp +++ b/webserv/http/HttpHeaders.hpp @@ -26,7 +26,7 @@ class HttpHeaders void remove(const std::string &name) noexcept; [[nodiscard]] std::string toString() const noexcept; - [[nodiscard]] std::optional getContentLength() const noexcept; + [[nodiscard]] std::optional getContentLength() const; [[nodiscard]] std::optional getContentType() const noexcept; [[nodiscard]] std::optional getHost() const noexcept; diff --git a/webserv/http/HttpRequest.cpp b/webserv/http/HttpRequest.cpp index 655f22b..7cec63d 100644 --- a/webserv/http/HttpRequest.cpp +++ b/webserv/http/HttpRequest.cpp @@ -182,8 +182,17 @@ bool HttpRequest::parseBufferforChunkedBody() } std::string chunkSizeStr = buffer_.substr(0, pos); Log::debug("Chunk size string: " + chunkSizeStr); - size_t chunkSize = utils::stoul(chunkSizeStr, 16); - Log::warning("Invalid chunk size: " + chunkSizeStr); + size_t chunkSize = 0; + try + { + chunkSize = utils::stoul(chunkSizeStr, 16); + } + catch (const std::exception &e) + { + Log::warning("Invalid chunk size: " + chunkSizeStr + " - " + e.what()); + setState(State::ParseError); + return false; + } if (chunkSize == 0) { setState(State::Complete); // Last chunk