blob: 77c2074013aa8b08ac091c5c97a2766c161d07f5 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous1abe55e2018-09-05 08:30:59 -07003#include <boost/beast/http/verb.hpp>
Ed Tanous7045c8d2017-04-03 10:04:37 -07004#include <iostream>
5#include <stdexcept>
6#include <string>
7#include <vector>
Ed Tanous7045c8d2017-04-03 10:04:37 -07008
Ed Tanousc94ad492019-10-10 15:39:33 -07009#include "utility.h"
Ed Tanous1abe55e2018-09-05 08:30:59 -070010
11namespace crow
12{
Ed Tanous7045c8d2017-04-03 10:04:37 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014inline 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 Tanous43b761d2019-02-13 20:10:56 -080036 default:
37 return "invalid";
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070039}
40
Ed Tanous1abe55e2018-09-05 08:30:59 -070041enum class ParamType
42{
43 INT,
44 UINT,
45 DOUBLE,
46 STRING,
47 PATH,
Ed Tanous7045c8d2017-04-03 10:04:37 -070048
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 MAX
Ed Tanous7045c8d2017-04-03 10:04:37 -070050};
51
Ed Tanous1abe55e2018-09-05 08:30:59 -070052struct 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 Tanous7045c8d2017-04-03 10:04:37 -070058
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 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.Lukaszc3313442018-01-19 13:54:19 +010082 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070083
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 template <typename T> T get(unsigned) const;
Ed Tanous7045c8d2017-04-03 10:04:37 -070085};
86
Ed Tanous1abe55e2018-09-05 08:30:59 -070087template <> inline int64_t RoutingParams::get<int64_t>(unsigned index) const
88{
89 return intParams[index];
90}
91
92template <> inline uint64_t RoutingParams::get<uint64_t>(unsigned index) const
93{
94 return uintParams[index];
95}
96
97template <> inline double RoutingParams::get<double>(unsigned index) const
98{
99 return doubleParams[index];
Ed Tanous7045c8d2017-04-03 10:04:37 -0700100}
101
102template <>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103inline std::string RoutingParams::get<std::string>(unsigned index) const
104{
105 return stringParams[index];
Ed Tanous7045c8d2017-04-03 10:04:37 -0700106}
107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108} // namespace crow
Ed Tanous7045c8d2017-04-03 10:04:37 -0700109
Ed Tanouse0d918b2018-03-27 17:41:04 -0700110constexpr boost::beast::http::verb operator"" _method(const char* str,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 size_t /*len*/)
112{
113 using verb = boost::beast::http::verb;
114 // clang-format off
Ed Tanouse0d918b2018-03-27 17:41:04 -0700115 return
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700116 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 Tanouse0d918b2018-03-27 17:41:04 -0700126 throw std::runtime_error("invalid http method");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 // clang-format on
Ed Tanous43b761d2019-02-13 20:10:56 -0800128}