VWeb/Source/Route.cpp

87 lines
2.6 KiB
C++
Raw Normal View History

2022-08-23 14:13:21 +02:00
#include <VWeb.h>
#include <algorithm>
namespace VWeb {
#define stringify(name) {name, std::string(#name).replace(0,12,"")}
static std::unordered_map<HttpMethod, std::string> s_HttpMethodToString = {
stringify(HttpMethod::HEAD),
stringify(HttpMethod::GET),
stringify(HttpMethod::OPTIONS),
stringify(HttpMethod::POST),
stringify(HttpMethod::PUT),
stringify(HttpMethod::PATCH),
stringify(HttpMethod::DELETE),
stringify(HttpMethod::FALLBACK)
};
#undef stringify
Route::Route(std::initializer_list<HttpMethod> methods) {
m_AllowedMethods = methods;
m_AllowAll = false;
m_AllowedMethods.push_back(HttpMethod::HEAD);
m_AllowedMethods.push_back(HttpMethod::OPTIONS);
}
2022-09-10 15:09:18 +02:00
bool Route::Execute(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
switch (request.Method) {
case HttpMethod::GET:
case HttpMethod::HEAD: return Get(request, response);
case HttpMethod::POST: return Post(request, response);
case HttpMethod::PUT: return Put(request, response);
case HttpMethod::OPTIONS: return Options(request, response);
case HttpMethod::PATCH: return Patch(request, response);
case HttpMethod::DELETE: return Delete(request, response);
default: return Fallback(request, response);
}
}
2022-09-10 15:09:18 +02:00
bool Route::Get(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Post(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Put(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Patch(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Delete(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Options(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
std::stringstream str{};
bool isFirst = true;
if (m_AllowAll) {
for (auto &[key, value] : s_HttpMethodToString) {
if (!isFirst)
str << ", ";
str << value;
isFirst = false;
}
} else {
for (auto &method : m_AllowedMethods) {
if (!isFirst)
str << ", ";
str << s_HttpMethodToString[method];
isFirst = false;
}
}
response.SetHeader("Allow", str.str());
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::Fallback(Request &request, Response &response) {
2022-08-23 14:13:21 +02:00
return true;
}
2022-09-10 15:09:18 +02:00
bool Route::IsAllowed(Request &request) { return true; }
bool Route::SupportsMethod(Request &request) {
2022-08-23 14:13:21 +02:00
return m_AllowAll ||
std::find(m_AllowedMethods.begin(), m_AllowedMethods.end(),
request.Method) != m_AllowedMethods.end();
}
2022-08-23 16:40:57 +02:00
void Route::AllowMethod(HttpMethod method) {
if (std::find(m_AllowedMethods.begin(), m_AllowedMethods.end(), method) == m_AllowedMethods.end())
m_AllowedMethods.push_back(method);
}
2022-08-23 14:13:21 +02:00
} // namespace VWeb