blob: 949f3903a3bc1720226ae29b574327125e3c8f74 [file] [log] [blame]
Ed Tanous08bbe112023-04-06 13:10:02 -07001#pragma once
2
3#include "sserule.hpp"
4#include "websocketrule.hpp"
5
6#include <boost/beast/http/verb.hpp>
7
8#include <initializer_list>
9#include <optional>
10
11namespace crow
12{
13template <typename T>
14struct RuleParameterTraits
15{
16 using self_t = T;
17 WebSocketRule& websocket()
18 {
19 self_t* self = static_cast<self_t*>(this);
20 WebSocketRule* p = new WebSocketRule(self->rule);
21 p->privilegesSet = self->privilegesSet;
22 self->ruleToUpgrade.reset(p);
23 return *p;
24 }
25
26 SseSocketRule& serverSentEvent()
27 {
28 self_t* self = static_cast<self_t*>(this);
29 SseSocketRule* p = new SseSocketRule(self->rule);
30 self->ruleToUpgrade.reset(p);
31 return *p;
32 }
33
34 self_t& methods(boost::beast::http::verb method)
35 {
36 self_t* self = static_cast<self_t*>(this);
37 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
38 if (verb)
39 {
40 self->methodsBitfield = 1U << static_cast<size_t>(*verb);
41 }
42 return *self;
43 }
44
45 template <typename... MethodArgs>
46 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
47 {
48 self_t* self = static_cast<self_t*>(this);
49 methods(argsMethod...);
50 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
51 if (verb)
52 {
53 self->methodsBitfield |= 1U << static_cast<size_t>(*verb);
54 }
55 return *self;
56 }
57
58 self_t& notFound()
59 {
60 self_t* self = static_cast<self_t*>(this);
Ed Tanousa3b9eb92024-06-03 08:39:37 -070061 self->isNotFound = true;
62 self->methodsBitfield = 0;
Ed Tanous08bbe112023-04-06 13:10:02 -070063 return *self;
64 }
65
66 self_t& methodNotAllowed()
67 {
68 self_t* self = static_cast<self_t*>(this);
Ed Tanousa3b9eb92024-06-03 08:39:37 -070069 self->isMethodNotAllowed = true;
70 self->methodsBitfield = 0;
Ed Tanous08bbe112023-04-06 13:10:02 -070071 return *self;
72 }
73
74 self_t& privileges(
75 const std::initializer_list<std::initializer_list<const char*>>& p)
76 {
77 self_t* self = static_cast<self_t*>(this);
78 for (const std::initializer_list<const char*>& privilege : p)
79 {
80 self->privilegesSet.emplace_back(privilege);
81 }
82 return *self;
83 }
84
85 template <size_t N, typename... MethodArgs>
86 self_t& privileges(const std::array<redfish::Privileges, N>& p)
87 {
88 self_t* self = static_cast<self_t*>(this);
89 for (const redfish::Privileges& privilege : p)
90 {
91 self->privilegesSet.emplace_back(privilege);
92 }
93 return *self;
94 }
95};
96} // namespace crow