Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 3 | #include <boost/beast/http/verb.hpp> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 4 | #include <iostream> |
| 5 | #include <stdexcept> |
| 6 | #include <string> |
| 7 | #include <vector> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 8 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 9 | #include "utility.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | |
| 11 | namespace crow |
| 12 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | inline std::string methodName(boost::beast::http::verb method) |
| 15 | { |
| 16 | switch (method) |
| 17 | { |
| 18 | case boost::beast::http::verb::delete_: |
| 19 | return "DELETE"; |
| 20 | case boost::beast::http::verb::get: |
| 21 | return "GET"; |
| 22 | case boost::beast::http::verb::head: |
| 23 | return "HEAD"; |
| 24 | case boost::beast::http::verb::post: |
| 25 | return "POST"; |
| 26 | case boost::beast::http::verb::put: |
| 27 | return "PUT"; |
| 28 | case boost::beast::http::verb::connect: |
| 29 | return "CONNECT"; |
| 30 | case boost::beast::http::verb::options: |
| 31 | return "OPTIONS"; |
| 32 | case boost::beast::http::verb::trace: |
| 33 | return "TRACE"; |
| 34 | case boost::beast::http::verb::patch: |
| 35 | return "PATCH"; |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 36 | default: |
| 37 | return "invalid"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | enum class ParamType |
| 42 | { |
| 43 | INT, |
| 44 | UINT, |
| 45 | DOUBLE, |
| 46 | STRING, |
| 47 | PATH, |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | MAX |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | struct RoutingParams |
| 53 | { |
| 54 | std::vector<int64_t> intParams; |
| 55 | std::vector<uint64_t> uintParams; |
| 56 | std::vector<double> doubleParams; |
| 57 | std::vector<std::string> stringParams; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | void debugPrint() const |
| 60 | { |
| 61 | std::cerr << "RoutingParams" << std::endl; |
| 62 | for (auto i : intParams) |
| 63 | { |
| 64 | std::cerr << i << ", "; |
| 65 | } |
| 66 | std::cerr << std::endl; |
| 67 | for (auto i : uintParams) |
| 68 | { |
| 69 | std::cerr << i << ", "; |
| 70 | } |
| 71 | std::cerr << std::endl; |
| 72 | for (auto i : doubleParams) |
| 73 | { |
| 74 | std::cerr << i << ", "; |
| 75 | } |
| 76 | std::cerr << std::endl; |
| 77 | for (auto& i : stringParams) |
| 78 | { |
| 79 | std::cerr << i << ", "; |
| 80 | } |
| 81 | std::cerr << std::endl; |
Borawski.Lukasz | c331344 | 2018-01-19 13:54:19 +0100 | [diff] [blame] | 82 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | template <typename T> T get(unsigned) const; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | template <> inline int64_t RoutingParams::get<int64_t>(unsigned index) const |
| 88 | { |
| 89 | return intParams[index]; |
| 90 | } |
| 91 | |
| 92 | template <> inline uint64_t RoutingParams::get<uint64_t>(unsigned index) const |
| 93 | { |
| 94 | return uintParams[index]; |
| 95 | } |
| 96 | |
| 97 | template <> inline double RoutingParams::get<double>(unsigned index) const |
| 98 | { |
| 99 | return doubleParams[index]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | template <> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | inline std::string RoutingParams::get<std::string>(unsigned index) const |
| 104 | { |
| 105 | return stringParams[index]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | } // namespace crow |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 109 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 110 | constexpr boost::beast::http::verb operator"" _method(const char* str, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | size_t /*len*/) |
| 112 | { |
| 113 | using verb = boost::beast::http::verb; |
| 114 | // clang-format off |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 115 | return |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 116 | crow::black_magic::isEquP(str, "GET", 3) ? verb::get : |
| 117 | crow::black_magic::isEquP(str, "DELETE", 6) ? verb::delete_ : |
| 118 | crow::black_magic::isEquP(str, "HEAD", 4) ? verb::head : |
| 119 | crow::black_magic::isEquP(str, "POST", 4) ? verb::post : |
| 120 | crow::black_magic::isEquP(str, "PUT", 3) ? verb::put : |
| 121 | crow::black_magic::isEquP(str, "OPTIONS", 7) ? verb::options : |
| 122 | crow::black_magic::isEquP(str, "CONNECT", 7) ? verb::connect : |
| 123 | crow::black_magic::isEquP(str, "TRACE", 5) ? verb::trace : |
| 124 | crow::black_magic::isEquP(str, "PATCH", 5) ? verb::patch : |
| 125 | crow::black_magic::isEquP(str, "PURGE", 5) ? verb::purge : |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 126 | throw std::runtime_error("invalid http method"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | // clang-format on |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 128 | } |