」 feat: POST IS WORKING git pull

This commit is contained in:
yosyo
2025-07-10 19:32:42 +02:00
parent 752b5a8010
commit 57e71094e0
4 changed files with 52 additions and 32 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:30:15 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); 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); Response execute(void);
server::Cgi *getCgi() const { return _cgi; } server::Cgi *getCgi() const { return _cgi; }
private: private:
std::string extractFilename(const std::string &header);
void handleMultipartData(const std::string &body,
const std::string &boundary);
void handleBinaryUpload();
server::Cgi *_cgi; server::Cgi *_cgi;
}; };

View File

@ -4,5 +4,6 @@ port = 2727
[server.location./] [server.location./]
root = "./exemples/webpage" root = "./exemples/webpage"
upload_path = "/home/yosyo/test"
methods = { "GET", "POST", "DELETE" } methods = { "GET", "POST", "DELETE" }
cgi = { ".py"} cgi = { ".py"}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 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) { 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; std::string line;
if (std::getline(stream, line)) { if (std::getline(stream, line)) {
@ -42,7 +47,6 @@ void Post::parse(std::string const &data) {
_method = _sanitizeStr(_method); _method = _sanitizeStr(_method);
_target = _sanitizeStr(_target); _target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol); _protocol = _sanitizeStr(_protocol);
// this->_target.insert(this->_target.begin(), '.');
} }
while (std::getline(stream, line) && line != "\r") { while (std::getline(stream, line) && line != "\r") {
@ -56,11 +60,6 @@ void Post::parse(std::string const &data) {
_route = _srv->whatRoute(URL(_target)); _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); _url = new URL(_target);
std::string targ = _target; std::string targ = _target;
@ -95,6 +94,7 @@ std::string Post::extractFilename(const std::string &header) {
void Post::handleMultipartData(const std::string &body, void Post::handleMultipartData(const std::string &body,
const std::string &boundary) { const std::string &boundary) {
_log->info("handling MultipartData upload...");
size_t i = 0; size_t i = 0;
std::string delim = "--" + boundary; std::string delim = "--" + boundary;
delim.erase(delim.size() - 1); delim.erase(delim.size() - 1);
@ -108,7 +108,7 @@ void Post::handleMultipartData(const std::string &body,
std::string part_content = std::string part_content =
body.substr(end + 4, body.find(delim, end) - end - 4); 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); std::ios::binary);
if (outfile.is_open()) { if (outfile.is_open()) {
outfile.write(part_content.c_str(), part_content.length()); 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) { Response Post::execute(void) {
http::Response response; http::Response response;
@ -148,12 +164,14 @@ Response Post::execute(void) {
} }
try { try {
handleMultipartData( if (this->getHeaders()["Content-Type"].substr(0, 19) == "multipart/form-data")
this->_body, handleMultipartData(
this->getHeaders()["Content-Type"].substr( this->_body,
this->getHeaders()["Content-Type"].find( this->getHeaders()["Content-Type"].substr(
"=", this->getHeaders()["Content-Type"].find(";")) + this->getHeaders()["Content-Type"].find(
1)); "=", this->getHeaders()["Content-Type"].find(";")) + 1));
else
handleBinaryUpload();
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(200); response.setStatusCode(200);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/07/08 18:29:20 by mmoussou ### ########.fr */ /* Updated: 2025/07/10 19:08:02 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,17 +43,20 @@ std::string readFd(int fd)
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
ssize_t bytes_received; ssize_t bytes_received;
while (received_data.find("\r\n\r\n") == std::string::npos) while (true)
{ {
bytes_received = recv(fd, buffer, BUFFER_SIZE, 0); memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(fd, buffer, BUFFER_SIZE, MSG_DONTWAIT);
if (bytes_received <= 0) if (bytes_received <= 0)
break; break;
received_data.append(buffer, bytes_received); received_data.append(buffer, bytes_received);
if (received_data.find("\r\n\r\n") != std::string::npos)
break;
} }
std::string headers = received_data.substr(0, received_data.find("\r\n\r\n")); size_t body_start = received_data.find("\r\n\r\n");
size_t body_start = received_data.find("\r\n\r\n") + 4; std::string headers = received_data.substr(0, body_start);
std::string body = received_data.substr(body_start); std::string body = received_data.substr(body_start + 4);
std::istringstream stream(headers); std::istringstream stream(headers);
int64_t content_length = -1; int64_t content_length = -1;
@ -65,8 +68,10 @@ std::string readFd(int fd)
content_length = std::atoi(line.c_str() + 15); content_length = std::atoi(line.c_str() + 15);
} }
while (int64_t(body.size()) < content_length) { //while (int64_t(body.size()) < content_length) {
bytes_received = recv(fd, buffer, BUFFER_SIZE, 0); while (content_length == -1 || (int64_t)body.size() < content_length) {
std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(fd, buffer, BUFFER_SIZE, MSG_DONTWAIT);
if (bytes_received <= 0) break; if (bytes_received <= 0) break;
body.append(buffer, bytes_received); body.append(buffer, bytes_received);
} }
@ -85,12 +90,8 @@ void Client::parse(void)
_log->error("failed to receive request"); _log->error("failed to receive request");
throw std::runtime_error("failed to receive request"); throw std::runtime_error("failed to receive request");
} }
std::cout << "reading passed :thumbsupcat:" << std::endl;
_getRequest(received_data); _getRequest(received_data);
std::cout << "request get passed :thumbsupcat:" << std::endl;
if (_request == not_nullptr) if (_request == not_nullptr)
return; return;