blob: 0d5417de98c1deea7afb9b248b43c0a190851808 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous08bbe112023-04-06 13:10:02 -07003#pragma once
4
Ed Tanousd7857202025-01-28 15:32:26 -08005#include "privileges.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -07006#include "sserule.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "verb.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -07008#include "websocketrule.hpp"
9
10#include <boost/beast/http/verb.hpp>
11
Ed Tanousd7857202025-01-28 15:32:26 -080012#include <array>
13#include <cstddef>
Ed Tanous08bbe112023-04-06 13:10:02 -070014#include <initializer_list>
15#include <optional>
16
17namespace crow
18{
19template <typename T>
20struct RuleParameterTraits
21{
Ed Tanous7da633f2024-12-02 08:25:38 -080022 private:
23 RuleParameterTraits() = default;
24 friend T;
25
26 public:
Ed Tanous08bbe112023-04-06 13:10:02 -070027 using self_t = T;
28 WebSocketRule& websocket()
29 {
30 self_t* self = static_cast<self_t*>(this);
31 WebSocketRule* p = new WebSocketRule(self->rule);
32 p->privilegesSet = self->privilegesSet;
33 self->ruleToUpgrade.reset(p);
34 return *p;
35 }
36
37 SseSocketRule& serverSentEvent()
38 {
39 self_t* self = static_cast<self_t*>(this);
40 SseSocketRule* p = new SseSocketRule(self->rule);
41 self->ruleToUpgrade.reset(p);
42 return *p;
43 }
44
45 self_t& methods(boost::beast::http::verb method)
46 {
47 self_t* self = static_cast<self_t*>(this);
48 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
49 if (verb)
50 {
51 self->methodsBitfield = 1U << static_cast<size_t>(*verb);
52 }
53 return *self;
54 }
55
56 template <typename... MethodArgs>
57 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
58 {
59 self_t* self = static_cast<self_t*>(this);
60 methods(argsMethod...);
61 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
62 if (verb)
63 {
64 self->methodsBitfield |= 1U << static_cast<size_t>(*verb);
65 }
66 return *self;
67 }
68
69 self_t& notFound()
70 {
71 self_t* self = static_cast<self_t*>(this);
Ed Tanousa3b9eb92024-06-03 08:39:37 -070072 self->isNotFound = true;
73 self->methodsBitfield = 0;
Ed Tanous08bbe112023-04-06 13:10:02 -070074 return *self;
75 }
76
77 self_t& methodNotAllowed()
78 {
79 self_t* self = static_cast<self_t*>(this);
Ed Tanousa3b9eb92024-06-03 08:39:37 -070080 self->isMethodNotAllowed = true;
81 self->methodsBitfield = 0;
Ed Tanous08bbe112023-04-06 13:10:02 -070082 return *self;
83 }
84
85 self_t& privileges(
86 const std::initializer_list<std::initializer_list<const char*>>& p)
87 {
88 self_t* self = static_cast<self_t*>(this);
89 for (const std::initializer_list<const char*>& privilege : p)
90 {
91 self->privilegesSet.emplace_back(privilege);
92 }
93 return *self;
94 }
95
96 template <size_t N, typename... MethodArgs>
97 self_t& privileges(const std::array<redfish::Privileges, N>& p)
98 {
99 self_t* self = static_cast<self_t*>(this);
100 for (const redfish::Privileges& privilege : p)
101 {
102 self->privilegesSet.emplace_back(privilege);
103 }
104 return *self;
105 }
106};
107} // namespace crow