Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <boost/beast/http/verb.hpp> |
| 4 | |
Ed Tanous | 6a04e0d | 2023-02-16 16:54:03 -0800 | [diff] [blame] | 5 | #include <iostream> |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 6 | #include <optional> |
| 7 | #include <string_view> |
| 8 | |
| 9 | enum class HttpVerb |
| 10 | { |
| 11 | Delete = 0, |
| 12 | Get, |
| 13 | Head, |
| 14 | Options, |
| 15 | Patch, |
| 16 | Post, |
| 17 | Put, |
| 18 | Max, |
| 19 | }; |
| 20 | |
Edward Lee | c0bdf22 | 2022-11-11 23:38:17 +0000 | [diff] [blame] | 21 | static 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. |
| 26 | static constexpr const size_t notFoundIndex = maxVerbIndex + 1; |
| 27 | static constexpr const size_t methodNotAllowedIndex = notFoundIndex + 1; |
| 28 | |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 29 | inline 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 | |
| 54 | inline 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 | } |