From fee7f25079f51d559fb3cf3b184baf6721f9ba45 Mon Sep 17 00:00:00 2001 From: Martijn Weverling Date: Mon, 20 May 2024 20:50:16 +0200 Subject: [PATCH] BUGFIX: POST: wouldn't post body into file; DELETE: finds request_target; Content-Length: getschecked; other minor refactor --- include/HTTPRequest.hpp | 2 +- src/Client.cpp | 8 +++++--- src/FileManager.cpp | 29 ++++++++++++++--------------- src/HTTPRequest.cpp | 13 +++++-------- src/ServerSettings.cpp | 1 - 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/include/HTTPRequest.hpp b/include/HTTPRequest.hpp index 5c913ef..c0bdb04 100644 --- a/include/HTTPRequest.hpp +++ b/include/HTTPRequest.hpp @@ -43,7 +43,7 @@ class HTTPRequest const std::string &getRequestTarget(void) const; void setMaxBodySize(std::string inp); - ssize_t getMaxBodySize(void) const; + size_t getMaxBodySize(void) const; void setHTTPVersion(const std::string &http_version); const std::string &getHTTPVersion(void) const; diff --git a/src/Client.cpp b/src/Client.cpp index 970abcc..b43c73f 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -96,9 +96,9 @@ void Client::checkCGI(const std::string &request_target, HTTPMethod method) // TODO: // [x] ServerName isn't working propperly, requires testing // [x] ResolveAlias needs a rework should copy and replace. -// [ ] Contentlenght Isnt working, requires testing -// [ ] Post Doesn't write the body of the request to the file -// [ ] Delete doens't work, maybe resolve Location +// [x] Contentlenght Isnt working, requires testing +// [x] Post Doesn't write the body of the request to the file +// [x] Delete doens't work, maybe resolve Location // [ ] CGI Hangs when the child process gets stuck. // [x] I'm still sometimes getting FATAL ERROR getClientPipe @@ -118,6 +118,8 @@ ClientState Client::handleConnection( if (_request.getHeaderEnd()) { resolveServerSetting(); + if (_request.getBody().size() > _request.getMaxBodySize()) + throw ClientException(StatusCode::RequestBodyTooLarge); checkCGI(_request.getRequestTarget(), _request.getMethodType()); } return (_state); diff --git a/src/FileManager.cpp b/src/FileManager.cpp index 980e44d..523b826 100644 --- a/src/FileManager.cpp +++ b/src/FileManager.cpp @@ -45,9 +45,6 @@ FileManager::applyLocationSettings(const std::string &request_target) void FileManager::openGetFile(const std::string &request_target_path) { - const std::string resolved_target = - applyLocationSettings(request_target_path); - if (_autoindex == true) { HTTPStatus status(StatusCode::OK); @@ -55,9 +52,9 @@ void FileManager::openGetFile(const std::string &request_target_path) return; } - if (!std::filesystem::exists(resolved_target)) + if (!std::filesystem::exists(request_target_path)) throw ClientException(StatusCode::NotFound); - _request_target.open(resolved_target, std::ios::in | std::ios::binary); + _request_target.open(request_target_path, std::ios::in | std::ios::binary); if (!_request_target.is_open()) throw ClientException(StatusCode::NotFound); HTTPStatus status(StatusCode::OK); @@ -66,12 +63,9 @@ void FileManager::openGetFile(const std::string &request_target_path) void FileManager::openPostFile(const std::string &request_target_path) { - const std::string resolved_target = - applyLocationSettings(request_target_path); - - if (!std::filesystem::exists(resolved_target)) + if (!std::filesystem::exists(request_target_path)) { - _request_target.open(resolved_target, std::ios::out); + _request_target.open(request_target_path, std::ios::out); if (!_request_target.is_open()) throw ClientException(StatusCode::InternalServerError); HTTPStatus status(StatusCode::Created); @@ -80,7 +74,7 @@ void FileManager::openPostFile(const std::string &request_target_path) else { _request_target.open(request_target_path, - std::ios::out | std::ios::app); + std::ios::out | std::ios::trunc); if (!_request_target.is_open()) throw ClientException(StatusCode::InternalServerError); HTTPStatus status(StatusCode::OK); @@ -184,19 +178,24 @@ ClientState FileManager::manage(HTTPMethod method, { Logger &logger = Logger::getInstance(); - logger.log(DEBUG, "FileManager::manage: "); + logger.log(DEBUG, "FileManager::manage:"); + logger.log(DEBUG, "request_target:\t" + request_target_path); + const std::string resolved_target = + applyLocationSettings(request_target_path); + logger.log(DEBUG, "resolved_target:\t" + resolved_target); + if (method == HTTPMethod::DELETE) - return (manageDelete(request_target_path)); + return (manageDelete(resolved_target)); if (method == HTTPMethod::GET) { if (!_request_target.is_open()) - openGetFile(request_target_path); + openGetFile(resolved_target); return (manageGet()); } else if (method == HTTPMethod::POST) { if (!_request_target.is_open()) - openPostFile(request_target_path); + openPostFile(resolved_target); return (managePost(body)); } return (ClientState::Unknown); diff --git a/src/HTTPRequest.cpp b/src/HTTPRequest.cpp index 3da71e1..2a59237 100644 --- a/src/HTTPRequest.cpp +++ b/src/HTTPRequest.cpp @@ -55,7 +55,7 @@ void HTTPRequest::setMaxBodySize(std::string inp) _max_body_size = std::stoull(nbr); } -ssize_t HTTPRequest::getMaxBodySize(void) const +size_t HTTPRequest::getMaxBodySize(void) const { return (_max_body_size); } @@ -165,13 +165,13 @@ size_t HTTPRequest::parseHeaders(size_t &i) ClientState HTTPRequest::setRequestVariables(size_t pos) { setHeaderEnd(true); - if (_headers.find("Content-length") != _headers.end()) - _content_length = std::stoi(getHeader("Content-length")); + if (_headers.find("Content-Length") != _headers.end()) + _content_length = std::stoi(getHeader("Content-Length")); if (_content_length == 0) return (ClientState::Loading); _body += _http_request.substr(pos + 2); - if (std::strlen(_body.c_str()) == _content_length) + if (_body.size() == _content_length) return (ClientState::Loading); return (ClientState::Receiving); } @@ -191,13 +191,10 @@ ClientState HTTPRequest::receive(int client_fd) if (_content_length != 0) { _body += std::string(buffer, _bytes_read); - if (_body.size() >= _max_body_size) + if (_body.size() > _max_body_size) throw ClientException(StatusCode::RequestBodyTooLarge); if (_body.size() >= _content_length) - { - logger.log(DEBUG, "Body: " + _body); return (ClientState::Loading); - } return (ClientState::Receiving); } _http_request += std::string(buffer, _bytes_read); diff --git a/src/ServerSettings.cpp b/src/ServerSettings.cpp index ccb6c0b..c553b8b 100644 --- a/src/ServerSettings.cpp +++ b/src/ServerSettings.cpp @@ -235,7 +235,6 @@ ServerSettings::resolveLocation(const std::string &request_target) const logger.log(DEBUG, "resolveLocation: search:\t" + request_target); for (const auto &instance : _location_settings) { - instance.printLocationSettings(); const size_t pos = request_target.find(instance.getPath()); if (pos != 0)