diff --git a/webserv/handler/DeleteHandler.cpp b/webserv/handler/DeleteHandler.cpp new file mode 100644 index 0000000..eee83c9 --- /dev/null +++ b/webserv/handler/DeleteHandler.cpp @@ -0,0 +1,77 @@ +#include "webserv/handler/ErrorHandler.hpp" +#include "webserv/http/HttpConstants.hpp" + +#include +#include // for HttpRequest +#include // for Log, LOCATION +#include // for isValidPath + +#include + +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()); +} \ No newline at end of file diff --git a/webserv/handler/DeleteHandler.hpp b/webserv/handler/DeleteHandler.hpp new file mode 100644 index 0000000..c5b35af --- /dev/null +++ b/webserv/handler/DeleteHandler.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include // for AHandler +#include + +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); +}; \ No newline at end of file diff --git a/webserv/router/Router.cpp b/webserv/router/Router.cpp index a527cf0..4858f2d 100644 --- a/webserv/router/Router.cpp +++ b/webserv/router/Router.cpp @@ -1,3 +1,4 @@ +#include "webserv/handler/DeleteHandler.hpp" #include // for Client #include // for AConfig #include // for ADirective @@ -58,6 +59,10 @@ std::unique_ptr Router::handleRequest() { return std::make_unique(request, response); } + if (request.getMethod() == "DELETE" && !request.getUri().isCgi()) + { + return std::make_unique(request, response); + } if (request.getUri().isCgi()) { try