Skip to content

Commit

Permalink
BUGFIX: POST: wouldn't post body into file; DELETE: finds request_tar…
Browse files Browse the repository at this point in the history
…get; Content-Length: getschecked; other minor refactor
  • Loading branch information
Tentanus committed May 20, 2024
1 parent 9db9a0b commit fee7f25
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
Expand Down
29 changes: 14 additions & 15 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ 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);
_response += status.getStatusLine("HTTP/1.1");
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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions src/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/ServerSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fee7f25

Please sign in to comment.