From e0efb0192c8272b0dbad32df02d8f965ae0bc654 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 7 Oct 2025 17:03:57 +0200 Subject: [PATCH] feat: implement method support check in Router for HTTP requests --- webserv/router/Router.cpp | 21 +++++++++++++++++++-- webserv/router/Router.hpp | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/webserv/router/Router.cpp b/webserv/router/Router.cpp index 3a8d132..0db1be3 100644 --- a/webserv/router/Router.cpp +++ b/webserv/router/Router.cpp @@ -1,4 +1,4 @@ -#include +#include "webserv/config/directive/ADirective.hpp" #include // for ConfigManager #include // for ServerConfig @@ -7,15 +7,29 @@ #include // for URIParser #include // for HttpHeaders #include // for LOCATION, Log +#include +#include #include // for unique_ptr #include // for optional #include // for basic_string, string +#include class LocationConfig; Router::Router() {} +bool Router::isMethodSupported(const std::string &method, const LocationConfig &location) +{ + const ADirective *allowedMethods = location.getDirective("allowed_methods"); + if (allowedMethods == nullptr || !allowedMethods->getValue().try_get>().has_value()) + { + return true; + } + auto methods = allowedMethods->getValue().get>(); + return std::ranges::find(methods, method) != methods.end(); +} + std::unique_ptr Router::handleRequest(const HttpRequest &request) { Log::trace(LOCATION); @@ -37,7 +51,10 @@ std::unique_ptr Router::handleRequest(const HttpRequest &request) { return ErrorHandler::getErrorResponse(404, config); } - + if (!isMethodSupported(method, *location)) + { + return ErrorHandler::getErrorResponse(405, config); + } FileHandler fileHandler(location, uriParser); return fileHandler.getResponse(); } \ No newline at end of file diff --git a/webserv/router/Router.hpp b/webserv/router/Router.hpp index 04bd855..96cf8d8 100644 --- a/webserv/router/Router.hpp +++ b/webserv/router/Router.hpp @@ -19,4 +19,5 @@ class Router private: [[nodiscard]] const LocationConfig *getLocation(const std::string &path, const ServerConfig &serverConfig) const; + [[nodiscard]] static bool isMethodSupported(const std::string &method, const LocationConfig &location); }; \ No newline at end of file