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 | } |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | inline 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 | } |