1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3 #pragma once 4 5 #include "sserule.hpp" 6 #include "websocketrule.hpp" 7 8 #include <boost/beast/http/verb.hpp> 9 10 #include <initializer_list> 11 #include <optional> 12 13 namespace crow 14 { 15 template <typename T> 16 struct RuleParameterTraits 17 { 18 private: 19 RuleParameterTraits() = default; 20 friend T; 21 22 public: 23 using self_t = T; websocketcrow::RuleParameterTraits24 WebSocketRule& websocket() 25 { 26 self_t* self = static_cast<self_t*>(this); 27 WebSocketRule* p = new WebSocketRule(self->rule); 28 p->privilegesSet = self->privilegesSet; 29 self->ruleToUpgrade.reset(p); 30 return *p; 31 } 32 serverSentEventcrow::RuleParameterTraits33 SseSocketRule& serverSentEvent() 34 { 35 self_t* self = static_cast<self_t*>(this); 36 SseSocketRule* p = new SseSocketRule(self->rule); 37 self->ruleToUpgrade.reset(p); 38 return *p; 39 } 40 methodscrow::RuleParameterTraits41 self_t& methods(boost::beast::http::verb method) 42 { 43 self_t* self = static_cast<self_t*>(this); 44 std::optional<HttpVerb> verb = httpVerbFromBoost(method); 45 if (verb) 46 { 47 self->methodsBitfield = 1U << static_cast<size_t>(*verb); 48 } 49 return *self; 50 } 51 52 template <typename... MethodArgs> methodscrow::RuleParameterTraits53 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod) 54 { 55 self_t* self = static_cast<self_t*>(this); 56 methods(argsMethod...); 57 std::optional<HttpVerb> verb = httpVerbFromBoost(method); 58 if (verb) 59 { 60 self->methodsBitfield |= 1U << static_cast<size_t>(*verb); 61 } 62 return *self; 63 } 64 notFoundcrow::RuleParameterTraits65 self_t& notFound() 66 { 67 self_t* self = static_cast<self_t*>(this); 68 self->isNotFound = true; 69 self->methodsBitfield = 0; 70 return *self; 71 } 72 methodNotAllowedcrow::RuleParameterTraits73 self_t& methodNotAllowed() 74 { 75 self_t* self = static_cast<self_t*>(this); 76 self->isMethodNotAllowed = true; 77 self->methodsBitfield = 0; 78 return *self; 79 } 80 privilegescrow::RuleParameterTraits81 self_t& privileges( 82 const std::initializer_list<std::initializer_list<const char*>>& p) 83 { 84 self_t* self = static_cast<self_t*>(this); 85 for (const std::initializer_list<const char*>& privilege : p) 86 { 87 self->privilegesSet.emplace_back(privilege); 88 } 89 return *self; 90 } 91 92 template <size_t N, typename... MethodArgs> privilegescrow::RuleParameterTraits93 self_t& privileges(const std::array<redfish::Privileges, N>& p) 94 { 95 self_t* self = static_cast<self_t*>(this); 96 for (const redfish::Privileges& privilege : p) 97 { 98 self->privilegesSet.emplace_back(privilege); 99 } 100 return *self; 101 } 102 }; 103 } // namespace crow 104