feat: implement method support check in Router for HTTP requests
This commit is contained in:
parent
ec683f1690
commit
e0efb0192c
@ -1,4 +1,4 @@
|
|||||||
#include <webserv/router/Router.hpp>
|
#include "webserv/config/directive/ADirective.hpp"
|
||||||
|
|
||||||
#include <webserv/config/ConfigManager.hpp> // for ConfigManager
|
#include <webserv/config/ConfigManager.hpp> // for ConfigManager
|
||||||
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
|
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
|
||||||
@ -7,15 +7,29 @@
|
|||||||
#include <webserv/handler/URIParser.hpp> // for URIParser
|
#include <webserv/handler/URIParser.hpp> // for URIParser
|
||||||
#include <webserv/http/HttpHeaders.hpp> // for HttpHeaders
|
#include <webserv/http/HttpHeaders.hpp> // for HttpHeaders
|
||||||
#include <webserv/log/Log.hpp> // for LOCATION, Log
|
#include <webserv/log/Log.hpp> // for LOCATION, Log
|
||||||
|
#include <webserv/router/Router.hpp>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <memory> // for unique_ptr
|
#include <memory> // for unique_ptr
|
||||||
#include <optional> // for optional
|
#include <optional> // for optional
|
||||||
#include <string> // for basic_string, string
|
#include <string> // for basic_string, string
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class LocationConfig;
|
class LocationConfig;
|
||||||
|
|
||||||
Router::Router() {}
|
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<std::vector<std::string>>().has_value())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
auto methods = allowedMethods->getValue().get<std::vector<std::string>>();
|
||||||
|
return std::ranges::find(methods, method) != methods.end();
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<HttpResponse> Router::handleRequest(const HttpRequest &request)
|
std::unique_ptr<HttpResponse> Router::handleRequest(const HttpRequest &request)
|
||||||
{
|
{
|
||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
@ -37,7 +51,10 @@ std::unique_ptr<HttpResponse> Router::handleRequest(const HttpRequest &request)
|
|||||||
{
|
{
|
||||||
return ErrorHandler::getErrorResponse(404, config);
|
return ErrorHandler::getErrorResponse(404, config);
|
||||||
}
|
}
|
||||||
|
if (!isMethodSupported(method, *location))
|
||||||
|
{
|
||||||
|
return ErrorHandler::getErrorResponse(405, config);
|
||||||
|
}
|
||||||
FileHandler fileHandler(location, uriParser);
|
FileHandler fileHandler(location, uriParser);
|
||||||
return fileHandler.getResponse();
|
return fileHandler.getResponse();
|
||||||
}
|
}
|
||||||
@ -19,4 +19,5 @@ class Router
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] const LocationConfig *getLocation(const std::string &path, const ServerConfig &serverConfig) const;
|
[[nodiscard]] const LocationConfig *getLocation(const std::string &path, const ServerConfig &serverConfig) const;
|
||||||
|
[[nodiscard]] static bool isMethodSupported(const std::string &method, const LocationConfig &location);
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user