blob: 5eacfbe56780a14bbcad361a716d47a3491f1edf [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 }
Ed Tanous2c9efc32022-07-31 22:08:26 -070050}
51
52inline std::string_view httpVerbToString(HttpVerb verb)
53{
54 switch (verb)
55 {
56 case HttpVerb::Delete:
57 return "DELETE";
58 case HttpVerb::Get:
59 return "GET";
60 case HttpVerb::Head:
61 return "HEAD";
62 case HttpVerb::Patch:
63 return "PATCH";
64 case HttpVerb::Post:
65 return "POST";
66 case HttpVerb::Put:
67 return "PUT";
68 case HttpVerb::Options:
69 return "OPTIONS";
70 case HttpVerb::Max:
71 return "";
72 }
73
74 // Should never reach here
75 return "";
76}