blob: 8eec68a352b3b5a75b408b29bbc80ffbd7c5f58c [file] [log] [blame]
Ed Tanous2c9efc32022-07-31 22:08:26 -07001#pragma once
2
3#include <boost/beast/http/verb.hpp>
4
5#include <optional>
6#include <string_view>
7
8enum class HttpVerb
9{
10 Delete = 0,
11 Get,
12 Head,
13 Options,
14 Patch,
15 Post,
16 Put,
17 Max,
18};
19
Edward Leec0bdf222022-11-11 23:38:17 +000020static constexpr size_t maxVerbIndex = static_cast<size_t>(HttpVerb::Max) - 1U;
21
22// MaxVerb + 1 is designated as the "not found" verb. It is done this way
23// to keep the BaseRule as a single bitfield (thus keeping the struct small)
24// while still having a way to declare a route a "not found" route.
25static constexpr const size_t notFoundIndex = maxVerbIndex + 1;
26static constexpr const size_t methodNotAllowedIndex = notFoundIndex + 1;
27
Ed Tanous2c9efc32022-07-31 22:08:26 -070028inline std::optional<HttpVerb> httpVerbFromBoost(boost::beast::http::verb bv)
29{
30 switch (bv)
31 {
32 case boost::beast::http::verb::delete_:
33 return HttpVerb::Delete;
34 case boost::beast::http::verb::get:
35 return HttpVerb::Get;
36 case boost::beast::http::verb::head:
37 return HttpVerb::Head;
38 case boost::beast::http::verb::options:
39 return HttpVerb::Options;
40 case boost::beast::http::verb::patch:
41 return HttpVerb::Patch;
42 case boost::beast::http::verb::post:
43 return HttpVerb::Post;
44 case boost::beast::http::verb::put:
45 return HttpVerb::Put;
46 default:
47 return std::nullopt;
48 }
49
50 return std::nullopt;
51}
52
53inline std::string_view httpVerbToString(HttpVerb verb)
54{
55 switch (verb)
56 {
57 case HttpVerb::Delete:
58 return "DELETE";
59 case HttpVerb::Get:
60 return "GET";
61 case HttpVerb::Head:
62 return "HEAD";
63 case HttpVerb::Patch:
64 return "PATCH";
65 case HttpVerb::Post:
66 return "POST";
67 case HttpVerb::Put:
68 return "PUT";
69 case HttpVerb::Options:
70 return "OPTIONS";
71 case HttpVerb::Max:
72 return "";
73 }
74
75 // Should never reach here
76 return "";
77}