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