xref: /openbmc/bmcweb/http/verb.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
32c9efc3cSEd Tanous #pragma once
42c9efc3cSEd Tanous 
52c9efc3cSEd Tanous #include <boost/beast/http/verb.hpp>
62c9efc3cSEd Tanous 
7*d7857201SEd Tanous #include <cstddef>
82c9efc3cSEd Tanous #include <optional>
9*d7857201SEd Tanous // boost/beast/http/verb for whatever reason requires this?
10*d7857201SEd Tanous // NOLINTNEXTLINE(misc-include-cleaner)
11*d7857201SEd Tanous #include <ostream>
122c9efc3cSEd Tanous #include <string_view>
132c9efc3cSEd Tanous 
142c9efc3cSEd Tanous enum class HttpVerb
152c9efc3cSEd Tanous {
162c9efc3cSEd Tanous     Delete = 0,
172c9efc3cSEd Tanous     Get,
182c9efc3cSEd Tanous     Head,
192c9efc3cSEd Tanous     Options,
202c9efc3cSEd Tanous     Patch,
212c9efc3cSEd Tanous     Post,
222c9efc3cSEd Tanous     Put,
232c9efc3cSEd Tanous     Max,
242c9efc3cSEd Tanous };
252c9efc3cSEd Tanous 
26c0bdf223SEdward Lee static constexpr size_t maxVerbIndex = static_cast<size_t>(HttpVerb::Max) - 1U;
27c0bdf223SEdward Lee 
httpVerbFromBoost(boost::beast::http::verb bv)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 
httpVerbToString(HttpVerb verb)512c9efc3cSEd Tanous inline std::string_view httpVerbToString(HttpVerb verb)
522c9efc3cSEd Tanous {
532c9efc3cSEd Tanous     switch (verb)
542c9efc3cSEd Tanous     {
552c9efc3cSEd Tanous         case HttpVerb::Delete:
562c9efc3cSEd Tanous             return "DELETE";
572c9efc3cSEd Tanous         case HttpVerb::Get:
582c9efc3cSEd Tanous             return "GET";
592c9efc3cSEd Tanous         case HttpVerb::Head:
602c9efc3cSEd Tanous             return "HEAD";
612c9efc3cSEd Tanous         case HttpVerb::Patch:
622c9efc3cSEd Tanous             return "PATCH";
632c9efc3cSEd Tanous         case HttpVerb::Post:
642c9efc3cSEd Tanous             return "POST";
652c9efc3cSEd Tanous         case HttpVerb::Put:
662c9efc3cSEd Tanous             return "PUT";
672c9efc3cSEd Tanous         case HttpVerb::Options:
682c9efc3cSEd Tanous             return "OPTIONS";
694da0490bSEd Tanous         default:
702c9efc3cSEd Tanous             return "";
712c9efc3cSEd Tanous     }
722c9efc3cSEd Tanous 
732c9efc3cSEd Tanous     // Should never reach here
742c9efc3cSEd Tanous     return "";
752c9efc3cSEd Tanous }
76