feat: delete requests

This commit is contained in:
Quinten 2025-11-05 16:04:26 +01:00
parent 5a1d8a1dcf
commit 0ae18e534f
3 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,77 @@
#include "webserv/handler/ErrorHandler.hpp"
#include "webserv/http/HttpConstants.hpp"
#include <webserv/handler/DeleteHandler.hpp>
#include <webserv/handler/URI.hpp> // for HttpRequest
#include <webserv/log/Log.hpp> // for Log, LOCATION
#include <webserv/utils/FileUtils.hpp> // for isValidPath
#include <unistd.h>
DeleteHandler::DeleteHandler(const HttpRequest &request, HttpResponse &response) : AHandler(request, response) {}
void DeleteHandler::handle()
{
Log::trace(LOCATION);
// delete file:
std::string fullPath = request_.getUri().getFullPath();
if (fullPath.empty() || !FileUtils::isValidPath(fullPath))
{
Log::warning("DeleteHandler: Invalid path for DELETE: " + fullPath);
ErrorHandler::createErrorResponse(Http::StatusCode::BAD_REQUEST, response_, request_.getUri().getConfig());
return;
}
if (FileUtils::isFile(fullPath))
{
deleteFile(fullPath);
}
else if (FileUtils::isDirectory(fullPath))
{
deleteDirectory(fullPath);
}
else
{
Log::warning("DeleteHandler: Path not found for DELETE: " + fullPath);
ErrorHandler::createErrorResponse(Http::StatusCode::NOT_FOUND, response_, request_.getUri().getConfig());
}
}
void DeleteHandler::deleteFile(const std::string &path)
{
Log::trace(LOCATION);
if (remove(path.c_str()) != 0)
{
Log::error("DeleteHandler: Failed to delete file: " + path);
ErrorHandler::createErrorResponse(Http::StatusCode::FORBIDDEN, response_, request_.getUri().getConfig());
}
else
{
Log::info("DeleteHandler: Successfully deleted file: " + path);
response_.setStatus(Http::StatusCode::NO_CONTENT);
response_.setComplete();
}
}
void DeleteHandler::deleteDirectory(const std::string &path)
{
Log::trace(LOCATION);
// Only allow deletion of empty directories
if (rmdir(path.c_str()) != 0)
{
Log::error("DeleteHandler: Failed to delete directory");
ErrorHandler::createErrorResponse(Http::StatusCode::FORBIDDEN, response_, request_.getUri().getConfig());
}
else
{
Log::info("DeleteHandler: Successfully deleted empty directory: " + path);
response_.setStatus(Http::StatusCode::NO_CONTENT);
response_.setComplete();
}
}
void DeleteHandler::handleTimeout()
{
Log::debug("DeleteHandler: Request timed out");
ErrorHandler::createErrorResponse(504, response_, request_.getUri().getConfig());
}

View File

@ -0,0 +1,17 @@
#pragma once
#include <webserv/handler/AHandler.hpp> // for AHandler
#include <string>
class DeleteHandler : public AHandler
{
public:
DeleteHandler(const HttpRequest &request, HttpResponse &response);
void handle() override;
void handleTimeout() override;
private:
void deleteFile(const std::string &path);
void deleteDirectory(const std::string &path);
};

View File

@ -1,3 +1,4 @@
#include "webserv/handler/DeleteHandler.hpp"
#include <webserv/client/Client.hpp> // for Client
#include <webserv/config/AConfig.hpp> // for AConfig
#include <webserv/config/directive/ADirective.hpp> // for ADirective
@ -58,6 +59,10 @@ std::unique_ptr<AHandler> Router::handleRequest()
{
return std::make_unique<RedirectHandler>(request, response);
}
if (request.getMethod() == "DELETE" && !request.getUri().isCgi())
{
return std::make_unique<DeleteHandler>(request, response);
}
if (request.getUri().isCgi())
{
try