xref: /openbmc/bmcweb/http/routing/ruleparametertraits.hpp (revision 56431b29998d58c43b101b5f55401e505c85be5e)
1 #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 
11 namespace crow
12 {
13 template <typename T>
14 struct RuleParameterTraits
15 {
16   private:
17     RuleParameterTraits() = default;
18     friend T;
19 
20   public:
21     using self_t = T;
22     WebSocketRule& websocket()
23     {
24         self_t* self = static_cast<self_t*>(this);
25         WebSocketRule* p = new WebSocketRule(self->rule);
26         p->privilegesSet = self->privilegesSet;
27         self->ruleToUpgrade.reset(p);
28         return *p;
29     }
30 
31     SseSocketRule& serverSentEvent()
32     {
33         self_t* self = static_cast<self_t*>(this);
34         SseSocketRule* p = new SseSocketRule(self->rule);
35         self->ruleToUpgrade.reset(p);
36         return *p;
37     }
38 
39     self_t& methods(boost::beast::http::verb method)
40     {
41         self_t* self = static_cast<self_t*>(this);
42         std::optional<HttpVerb> verb = httpVerbFromBoost(method);
43         if (verb)
44         {
45             self->methodsBitfield = 1U << static_cast<size_t>(*verb);
46         }
47         return *self;
48     }
49 
50     template <typename... MethodArgs>
51     self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
52     {
53         self_t* self = static_cast<self_t*>(this);
54         methods(argsMethod...);
55         std::optional<HttpVerb> verb = httpVerbFromBoost(method);
56         if (verb)
57         {
58             self->methodsBitfield |= 1U << static_cast<size_t>(*verb);
59         }
60         return *self;
61     }
62 
63     self_t& notFound()
64     {
65         self_t* self = static_cast<self_t*>(this);
66         self->isNotFound = true;
67         self->methodsBitfield = 0;
68         return *self;
69     }
70 
71     self_t& methodNotAllowed()
72     {
73         self_t* self = static_cast<self_t*>(this);
74         self->isMethodNotAllowed = true;
75         self->methodsBitfield = 0;
76         return *self;
77     }
78 
79     self_t& privileges(
80         const std::initializer_list<std::initializer_list<const char*>>& p)
81     {
82         self_t* self = static_cast<self_t*>(this);
83         for (const std::initializer_list<const char*>& privilege : p)
84         {
85             self->privilegesSet.emplace_back(privilege);
86         }
87         return *self;
88     }
89 
90     template <size_t N, typename... MethodArgs>
91     self_t& privileges(const std::array<redfish::Privileges, N>& p)
92     {
93         self_t* self = static_cast<self_t*>(this);
94         for (const redfish::Privileges& privilege : p)
95         {
96             self->privilegesSet.emplace_back(privilege);
97         }
98         return *self;
99     }
100 };
101 } // namespace crow
102