| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_VERB_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_VERB_H_ |
| |
| #include <cstddef> |
| #include <cstdint> |
| #include <iostream> //NOLINT, include so that boost::beast can build. |
| #include <optional> |
| #include <string> |
| #include <string_view> |
| |
| #include "boost/beast/http/verb.hpp" //NOLINT |
| |
| namespace milotic_tlbmc { |
| |
| enum class HttpVerb : uint8_t { |
| kDelete = 0, |
| kGet, |
| kHead, |
| kOptions, |
| kPatch, |
| kPost, |
| kPut, |
| kMax, |
| }; |
| |
| constexpr size_t MethodFieldsFromSingleHttpVerb(HttpVerb verb) { |
| return 1 << static_cast<size_t>(verb); |
| } |
| |
| constexpr bool MethodFieldsContainHttpVerb(size_t method_fields, |
| HttpVerb verb) { |
| return (method_fields & MethodFieldsFromSingleHttpVerb(verb)) > 0U; |
| } |
| |
| constexpr HttpVerb SingleHttpVerbFromMethodFields(size_t method_fields) { |
| for (int verb_index = 0; verb_index < static_cast<int>(HttpVerb::kMax); |
| ++verb_index) { |
| HttpVerb http_verb = static_cast<HttpVerb>(verb_index); |
| if (MethodFieldsContainHttpVerb(method_fields, (http_verb))) { |
| return http_verb; |
| } |
| } |
| return HttpVerb::kMax; |
| } |
| |
| std::string MethodFieldsToString(size_t method_fields); |
| |
| constexpr boost::beast::http::verb BoostVerbFromHttpVerb(HttpVerb verb) { |
| switch (verb) { |
| case HttpVerb::kDelete: |
| return boost::beast::http::verb::delete_; |
| case HttpVerb::kGet: |
| return boost::beast::http::verb::get; |
| case HttpVerb::kHead: |
| return boost::beast::http::verb::head; |
| case HttpVerb::kOptions: |
| return boost::beast::http::verb::options; |
| case HttpVerb::kPatch: |
| return boost::beast::http::verb::patch; |
| case HttpVerb::kPost: |
| return boost::beast::http::verb::post; |
| case HttpVerb::kPut: |
| return boost::beast::http::verb::put; |
| default: |
| return boost::beast::http::verb::unknown; |
| } |
| } |
| |
| constexpr size_t MergeMethodFields(size_t method_fields_1) { |
| return method_fields_1; |
| } |
| |
| // Returns the bitwise OR of the two method fields. |
| template <typename... Args> |
| constexpr size_t MergeMethodFields(size_t first, Args... args) { |
| return first | MergeMethodFields(args...); |
| } |
| |
| constexpr boost::beast::http::verb SingleBoostVerbFromMethodFields( |
| size_t method_fields) { |
| return BoostVerbFromHttpVerb(SingleHttpVerbFromMethodFields(method_fields)); |
| } |
| |
| static constexpr size_t kMaxVerbIndex = |
| static_cast<size_t>(HttpVerb::kMax) - 1U; |
| |
| // `kMaxVerbIndex` + 1 is designated as the "not found" verb. It is done this |
| // way to keep the BaseRule as a single bitfield (thus keeping the struct small) |
| // while still having a way to declare a route a "not found" route. |
| static constexpr const size_t kNotFoundIndex = kMaxVerbIndex + 1; |
| |
| static constexpr const size_t kMethodNotAllowedIndex = kNotFoundIndex + 1; |
| |
| size_t MethodFieldsForSingleBoostVerb(boost::beast::http::verb verb); |
| |
| bool MethodFieldsContainBoostVerb(size_t method_fields, |
| boost::beast::http::verb verb); |
| |
| std::optional<HttpVerb> HttpVerbFromBoost(boost::beast::http::verb verb); |
| |
| std::string_view HttpVerbToString(HttpVerb verb); |
| |
| std::string_view BoostVerbToString(boost::beast::http::verb verb); |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_VERB_H_ |