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