From 4ca633d50eef923e6a8ffa7d58fb318edd766b80 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 12 Jul 2025 13:48:32 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=80=E3=80=8D=20merge:=20a=20v?= =?UTF-8?q?ery=20cool=20feature=20has=20been=20merged=20!=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Route.hpp | 8 ++--- src/config/Route.cpp | 7 +++- src/requests_handling/Cgi.cpp | 9 ++--- .../requestImplementation/Post.cpp | 33 +++++++++++-------- src/server/Client.cpp | 6 ++-- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 5f9eebe..99eef07 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/06/23 21:03:07 by adjoly ### ########.fr */ +/* Updated: 2025/07/12 13:42:03 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,11 +107,11 @@ class Route { * @return The number in bytes */ int32_t _parseSize(std::string size) { - if (size[size.size()] == 'M') + if (size[size.length() - 1] == 'M') return std::atoi(size.c_str()) * 1024 * 1024; - if (size[size.size()] == 'K') + if (size[size.length() - 1] == 'K') return std::atoi(size.c_str()) * 1024; - if (isalpha(size[size.size()])) + if (!isalpha(size[size.length() - 1])) return std::atoi(size.c_str()); return -1; } diff --git a/src/config/Route.cpp b/src/config/Route.cpp index 74067c2..cb222f7 100644 --- a/src/config/Route.cpp +++ b/src/config/Route.cpp @@ -125,8 +125,13 @@ Route::Route(toml::ANode *table) : _max_body(10485760) { _root = "./html"; #endif val = accessValue("client_max_body_size", toml::STRING, _table, _log); - if (val != not_nullptr) + if (val != not_nullptr) { _max_body = _parseSize(*static_cast(val)); + if (_max_body == -1) + _max_body = 10485760; + } else { + _max_body = 10485760; + } std::map::iterator it = _table->accessIt("cgi", toml::ARRAY, found); if (found == true && it != _table->getTable()->end()) diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index 062cb04..b15ebcc 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,7 +6,7 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/07/08 15:45:20 by adjoly ### ########.fr */ +/* Updated: 2025/07/12 13:22:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -180,15 +180,16 @@ void Cgi::process(void) { } std::string Cgi::str(void) { - int max = _conf->getMaxBody(); + // int max = _conf->getMaxBody(); char buffer[1024]; std::ostringstream str; - while (max) { + // while (max) { + while (727) { ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer)); if (count > 0) { str.write(buffer, count); - max -= count; + // max -= count; } else if (count == 0) { break; } else diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 3706ac6..2735e2c 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ -/* Updated: 2025/07/12 08:54:08 by mmoussou ### ########.fr */ +/* Updated: 2025/07/12 13:48:25 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,12 @@ void Post::parse(std::string const &data) { } _route = _srv->whatRoute(URL(_target)); + if (_route->getMaxBody() != -1 && + (int32_t)_body.length() > _route->getMaxBody()) { + _method = "413"; + _log->warn("post body too large"); + return; + } _url = new URL(_target); @@ -108,8 +114,10 @@ void Post::handleMultipartData(const std::string &body, std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4); - std::ofstream outfile((this->_route->getUpRoot() + extractFilename(part_header)).c_str(), - std::ios::binary); + std::ofstream outfile( + (this->_route->getUpRoot() + extractFilename(part_header)) + .c_str(), + std::ios::binary); if (outfile.is_open()) { outfile.write(part_content.c_str(), part_content.length()); outfile.close(); @@ -123,18 +131,16 @@ void Post::handleMultipartData(const std::string &body, } } -void Post::handleBinaryUpload() -{ +void Post::handleBinaryUpload() { _log->info("handling binary upload..."); std::cout << (this->_route->getUpRoot() + this->_target) << std::endl; - std::ofstream outfile((this->_route->getUpRoot() + this->_target).c_str(), std::ios::binary); + std::ofstream outfile((this->_route->getUpRoot() + this->_target).c_str(), + std::ios::binary); if (outfile.is_open()) { outfile.write(this->_body.data(), this->_body.length()); outfile.close(); - } - else - { + } else { _log->error("open failed D:"); throw std::runtime_error("open failed"); } @@ -166,18 +172,19 @@ Response Post::execute(void) { } try { - if (this->_route->getUpRoot().empty()) - { + if (this->_route->getUpRoot().empty()) { _log->error("invalid upload path"); throw std::runtime_error("invalid upload path"); } - if (this->getHeaders()["Content-Type"].substr(0, 19) == "multipart/form-data") + if (this->getHeaders()["Content-Type"].substr(0, 19) == + "multipart/form-data") handleMultipartData( this->_body, this->getHeaders()["Content-Type"].substr( this->getHeaders()["Content-Type"].find( - "=", this->getHeaders()["Content-Type"].find(";")) + 1)); + "=", this->getHeaders()["Content-Type"].find(";")) + + 1)); else handleBinaryUpload(); diff --git a/src/server/Client.cpp b/src/server/Client.cpp index ffa8d61..78c8b42 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou getMethod() == "DELETE" && !_route->getMethods()[2])) this->_request->setMethod("405"); - if (received_data.length() > (unsigned long)(_route->getMaxBody())) - this->_request->setMethod("413"); + // if (received_data.length() > (unsigned long)(_route->getMaxBody())) + // this->_request->setMethod("413"); } bool Client::requestParsed(void) {