blob: e4e85271fdc21f447cfa614906cb3f7e0335cf76 [file] [log] [blame]
#include "tlbmc/redfish/verb.h"
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include "boost/beast/http/verb.hpp" //NOLINT
namespace milotic_tlbmc {
std::optional<HttpVerb> HttpVerbFromBoost(boost::beast::http::verb verb) {
switch (verb) {
case boost::beast::http::verb::delete_:
return HttpVerb::kDelete;
case boost::beast::http::verb::get:
return HttpVerb::kGet;
case boost::beast::http::verb::head:
return HttpVerb::kHead;
case boost::beast::http::verb::options:
return HttpVerb::kOptions;
case boost::beast::http::verb::patch:
return HttpVerb::kPatch;
case boost::beast::http::verb::post:
return HttpVerb::kPost;
case boost::beast::http::verb::put:
return HttpVerb::kPut;
default:
return std::nullopt;
}
return std::nullopt;
}
std::string_view HttpVerbToString(HttpVerb verb) {
switch (verb) {
case HttpVerb::kDelete:
return "DELETE";
case HttpVerb::kGet:
return "GET";
case HttpVerb::kHead:
return "HEAD";
case HttpVerb::kPatch:
return "PATCH";
case HttpVerb::kPost:
return "POST";
case HttpVerb::kPut:
return "PUT";
case HttpVerb::kOptions:
return "OPTIONS";
case HttpVerb::kMax:
return "";
}
// Should never reach here
return "";
}
std::string_view BoostVerbToString(boost::beast::http::verb v) {
std::optional<HttpVerb> verb = HttpVerbFromBoost(v);
if (!verb) {
return "";
}
return HttpVerbToString(*verb);
}
size_t MethodFieldsForSingleBoostVerb(boost::beast::http::verb verb) {
std::optional<HttpVerb> http_verb = HttpVerbFromBoost(verb);
if (!http_verb) {
return 0;
}
return MethodFieldsFromSingleHttpVerb(*http_verb);
}
bool MethodFieldsContainBoostVerb(size_t method_fields,
boost::beast::http::verb verb) {
std::optional<HttpVerb> http_verb = HttpVerbFromBoost(verb);
if (!http_verb) {
return false;
}
return MethodFieldsContainHttpVerb(method_fields, *http_verb);
}
std::string MethodFieldsToString(size_t method_fields) {
std::string result;
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))) {
result += HttpVerbToString(http_verb);
result += ",";
}
}
if (!result.empty() && result.back() == ',') {
result.pop_back();
}
return result;
}
} // namespace milotic_tlbmc