xref: /openbmc/bmcweb/http/verb.hpp (revision c0bdf223)
12c9efc3cSEd Tanous #pragma once
22c9efc3cSEd Tanous 
32c9efc3cSEd Tanous #include <boost/beast/http/verb.hpp>
42c9efc3cSEd Tanous 
52c9efc3cSEd Tanous #include <optional>
62c9efc3cSEd Tanous #include <string_view>
72c9efc3cSEd Tanous 
82c9efc3cSEd Tanous enum class HttpVerb
92c9efc3cSEd Tanous {
102c9efc3cSEd Tanous     Delete = 0,
112c9efc3cSEd Tanous     Get,
122c9efc3cSEd Tanous     Head,
132c9efc3cSEd Tanous     Options,
142c9efc3cSEd Tanous     Patch,
152c9efc3cSEd Tanous     Post,
162c9efc3cSEd Tanous     Put,
172c9efc3cSEd Tanous     Max,
182c9efc3cSEd Tanous };
192c9efc3cSEd Tanous 
20*c0bdf223SEdward Lee static constexpr size_t maxVerbIndex = static_cast<size_t>(HttpVerb::Max) - 1U;
21*c0bdf223SEdward Lee 
22*c0bdf223SEdward Lee // MaxVerb + 1 is designated as the "not found" verb.  It is done this way
23*c0bdf223SEdward Lee // to keep the BaseRule as a single bitfield (thus keeping the struct small)
24*c0bdf223SEdward Lee // while still having a way to declare a route a "not found" route.
25*c0bdf223SEdward Lee static constexpr const size_t notFoundIndex = maxVerbIndex + 1;
26*c0bdf223SEdward Lee static constexpr const size_t methodNotAllowedIndex = notFoundIndex + 1;
27*c0bdf223SEdward Lee 
282c9efc3cSEd Tanous inline std::optional<HttpVerb> httpVerbFromBoost(boost::beast::http::verb bv)
292c9efc3cSEd Tanous {
302c9efc3cSEd Tanous     switch (bv)
312c9efc3cSEd Tanous     {
322c9efc3cSEd Tanous         case boost::beast::http::verb::delete_:
332c9efc3cSEd Tanous             return HttpVerb::Delete;
342c9efc3cSEd Tanous         case boost::beast::http::verb::get:
352c9efc3cSEd Tanous             return HttpVerb::Get;
362c9efc3cSEd Tanous         case boost::beast::http::verb::head:
372c9efc3cSEd Tanous             return HttpVerb::Head;
382c9efc3cSEd Tanous         case boost::beast::http::verb::options:
392c9efc3cSEd Tanous             return HttpVerb::Options;
402c9efc3cSEd Tanous         case boost::beast::http::verb::patch:
412c9efc3cSEd Tanous             return HttpVerb::Patch;
422c9efc3cSEd Tanous         case boost::beast::http::verb::post:
432c9efc3cSEd Tanous             return HttpVerb::Post;
442c9efc3cSEd Tanous         case boost::beast::http::verb::put:
452c9efc3cSEd Tanous             return HttpVerb::Put;
462c9efc3cSEd Tanous         default:
472c9efc3cSEd Tanous             return std::nullopt;
482c9efc3cSEd Tanous     }
492c9efc3cSEd Tanous 
502c9efc3cSEd Tanous     return std::nullopt;
512c9efc3cSEd Tanous }
522c9efc3cSEd Tanous 
532c9efc3cSEd Tanous inline std::string_view httpVerbToString(HttpVerb verb)
542c9efc3cSEd Tanous {
552c9efc3cSEd Tanous     switch (verb)
562c9efc3cSEd Tanous     {
572c9efc3cSEd Tanous         case HttpVerb::Delete:
582c9efc3cSEd Tanous             return "DELETE";
592c9efc3cSEd Tanous         case HttpVerb::Get:
602c9efc3cSEd Tanous             return "GET";
612c9efc3cSEd Tanous         case HttpVerb::Head:
622c9efc3cSEd Tanous             return "HEAD";
632c9efc3cSEd Tanous         case HttpVerb::Patch:
642c9efc3cSEd Tanous             return "PATCH";
652c9efc3cSEd Tanous         case HttpVerb::Post:
662c9efc3cSEd Tanous             return "POST";
672c9efc3cSEd Tanous         case HttpVerb::Put:
682c9efc3cSEd Tanous             return "PUT";
692c9efc3cSEd Tanous         case HttpVerb::Options:
702c9efc3cSEd Tanous             return "OPTIONS";
712c9efc3cSEd Tanous         case HttpVerb::Max:
722c9efc3cSEd Tanous             return "";
732c9efc3cSEd Tanous     }
742c9efc3cSEd Tanous 
752c9efc3cSEd Tanous     // Should never reach here
762c9efc3cSEd Tanous     return "";
772c9efc3cSEd Tanous }
78