From 57e71094e059b847983bf8fff2fa9bcf0e1ca54b Mon Sep 17 00:00:00 2001 From: yosyo Date: Thu, 10 Jul 2025 19:32:42 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20POST=20IS?= =?UTF-8?q?=20WORKING=20git=20pull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/RequestImplement.hpp | 10 ++-- sample.toml | 1 + .../requestImplementation/Post.cpp | 48 +++++++++++++------ src/server/Client.cpp | 25 +++++----- 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/includes/requests/RequestImplement.hpp b/includes/requests/RequestImplement.hpp index ca8677f..fb19621 100644 --- a/includes/requests/RequestImplement.hpp +++ b/includes/requests/RequestImplement.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */ -/* Updated: 2025/07/08 11:42:46 by adjoly ### ########.fr */ +/* Updated: 2025/07/10 16:51:32 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,15 +43,15 @@ class Post : public ARequest { void parse(std::string const &data); - std::string extractFilename(const std::string &header); - void handleMultipartData(const std::string &body, - const std::string &boundary); - Response execute(void); server::Cgi *getCgi() const { return _cgi; } private: + std::string extractFilename(const std::string &header); + void handleMultipartData(const std::string &body, + const std::string &boundary); + void handleBinaryUpload(); server::Cgi *_cgi; }; diff --git a/sample.toml b/sample.toml index 48edece..54c5ec7 100644 --- a/sample.toml +++ b/sample.toml @@ -4,5 +4,6 @@ port = 2727 [server.location./] root = "./exemples/webpage" +upload_path = "/home/yosyo/test" methods = { "GET", "POST", "DELETE" } cgi = { ".py"} diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 47226e2..f052359 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/02 13:01:28 by adjoly ### ########.fr */ +/* Updated: 2025/07/10 18:09:03 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,12 @@ Post::Post(std::string &data, config::Server *srv) { } void Post::parse(std::string const &data) { - std::istringstream stream(data); + size_t header_end = data.find("\r\n\r\n"); + if (header_end == std::string::npos) + throw std::runtime_error("Invalid HTTP request"); + this->_body = data.substr(header_end + 4); + + std::istringstream stream(data.substr(0, header_end)); std::string line; if (std::getline(stream, line)) { @@ -42,7 +47,6 @@ void Post::parse(std::string const &data) { _method = _sanitizeStr(_method); _target = _sanitizeStr(_target); _protocol = _sanitizeStr(_protocol); - // this->_target.insert(this->_target.begin(), '.'); } while (std::getline(stream, line) && line != "\r") { @@ -56,11 +60,6 @@ void Post::parse(std::string const &data) { _route = _srv->whatRoute(URL(_target)); - std::ostringstream body_stream; - while (std::getline(stream, line)) - body_stream << line << "\n"; - this->_body = body_stream.str(); - _url = new URL(_target); std::string targ = _target; @@ -95,6 +94,7 @@ std::string Post::extractFilename(const std::string &header) { void Post::handleMultipartData(const std::string &body, const std::string &boundary) { + _log->info("handling MultipartData upload..."); size_t i = 0; std::string delim = "--" + boundary; delim.erase(delim.size() - 1); @@ -108,7 +108,7 @@ void Post::handleMultipartData(const std::string &body, std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4); - std::ofstream outfile(extractFilename(part_header).c_str(), + 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()); @@ -122,6 +122,22 @@ void Post::handleMultipartData(const std::string &body, } } +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); + + if (outfile.is_open()) { + outfile.write(this->_body.data(), this->_body.length()); + outfile.close(); + } + else + { + _log->error("open failed D:"); + } +} + Response Post::execute(void) { http::Response response; @@ -148,12 +164,14 @@ Response Post::execute(void) { } try { - handleMultipartData( - this->_body, - this->getHeaders()["Content-Type"].substr( - this->getHeaders()["Content-Type"].find( - "=", this->getHeaders()["Content-Type"].find(";")) + - 1)); + 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)); + else + handleBinaryUpload(); response.setProtocol(this->_protocol); response.setStatusCode(200); diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 495f786..ffa8d61 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou error("failed to receive request"); throw std::runtime_error("failed to receive request"); } - std::cout << "reading passed :thumbsupcat:" << std::endl; - _getRequest(received_data); - std::cout << "request get passed :thumbsupcat:" << std::endl; - if (_request == not_nullptr) return;