blob: 47df7e63c793d364be717fba4597ee913e6a2509 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003#include "utility.h"
4
Ed Tanous1abe55e2018-09-05 08:30:59 -07005#include <boost/beast/http/verb.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05006
Ed Tanous7045c8d2017-04-03 10:04:37 -07007#include <iostream>
8#include <stdexcept>
9#include <string>
10#include <vector>
Ed Tanous7045c8d2017-04-03 10:04:37 -070011
Ed Tanous1abe55e2018-09-05 08:30:59 -070012namespace crow
13{
Ed Tanous7045c8d2017-04-03 10:04:37 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015inline std::string methodName(boost::beast::http::verb method)
16{
17 switch (method)
18 {
19 case boost::beast::http::verb::delete_:
20 return "DELETE";
21 case boost::beast::http::verb::get:
22 return "GET";
23 case boost::beast::http::verb::head:
24 return "HEAD";
25 case boost::beast::http::verb::post:
26 return "POST";
27 case boost::beast::http::verb::put:
28 return "PUT";
29 case boost::beast::http::verb::connect:
30 return "CONNECT";
31 case boost::beast::http::verb::options:
32 return "OPTIONS";
33 case boost::beast::http::verb::trace:
34 return "TRACE";
35 case boost::beast::http::verb::patch:
36 return "PATCH";
Ed Tanous43b761d2019-02-13 20:10:56 -080037 default:
38 return "invalid";
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070040}
41
Ed Tanous1abe55e2018-09-05 08:30:59 -070042enum class ParamType
43{
44 INT,
45 UINT,
46 DOUBLE,
47 STRING,
48 PATH,
Ed Tanous7045c8d2017-04-03 10:04:37 -070049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 MAX
Ed Tanous7045c8d2017-04-03 10:04:37 -070051};
52
Ed Tanous1abe55e2018-09-05 08:30:59 -070053struct RoutingParams
54{
55 std::vector<int64_t> intParams;
56 std::vector<uint64_t> uintParams;
57 std::vector<double> doubleParams;
58 std::vector<std::string> stringParams;
Ed Tanous7045c8d2017-04-03 10:04:37 -070059
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 void debugPrint() const
61 {
62 std::cerr << "RoutingParams" << std::endl;
63 for (auto i : intParams)
64 {
65 std::cerr << i << ", ";
66 }
67 std::cerr << std::endl;
68 for (auto i : uintParams)
69 {
70 std::cerr << i << ", ";
71 }
72 std::cerr << std::endl;
73 for (auto i : doubleParams)
74 {
75 std::cerr << i << ", ";
76 }
77 std::cerr << std::endl;
78 for (auto& i : stringParams)
79 {
80 std::cerr << i << ", ";
81 }
82 std::cerr << std::endl;
Borawski.Lukaszc3313442018-01-19 13:54:19 +010083 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070084
Gunnar Mills1214b7e2020-06-04 10:11:30 -050085 template <typename T>
86 T get(unsigned) const;
Ed Tanous7045c8d2017-04-03 10:04:37 -070087};
88
Gunnar Mills1214b7e2020-06-04 10:11:30 -050089template <>
90inline int64_t RoutingParams::get<int64_t>(unsigned index) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070091{
92 return intParams[index];
93}
94
Gunnar Mills1214b7e2020-06-04 10:11:30 -050095template <>
96inline uint64_t RoutingParams::get<uint64_t>(unsigned index) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070097{
98 return uintParams[index];
99}
100
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500101template <>
102inline double RoutingParams::get<double>(unsigned index) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103{
104 return doubleParams[index];
Ed Tanous7045c8d2017-04-03 10:04:37 -0700105}
106
107template <>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108inline std::string RoutingParams::get<std::string>(unsigned index) const
109{
110 return stringParams[index];
Ed Tanous7045c8d2017-04-03 10:04:37 -0700111}
112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113} // namespace crow
Ed Tanous7045c8d2017-04-03 10:04:37 -0700114
Ed Tanouse0d918b2018-03-27 17:41:04 -0700115constexpr boost::beast::http::verb operator"" _method(const char* str,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 size_t /*len*/)
117{
118 using verb = boost::beast::http::verb;
119 // clang-format off
Ed Tanouse0d918b2018-03-27 17:41:04 -0700120 return
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700121 crow::black_magic::isEquP(str, "GET", 3) ? verb::get :
122 crow::black_magic::isEquP(str, "DELETE", 6) ? verb::delete_ :
123 crow::black_magic::isEquP(str, "HEAD", 4) ? verb::head :
124 crow::black_magic::isEquP(str, "POST", 4) ? verb::post :
125 crow::black_magic::isEquP(str, "PUT", 3) ? verb::put :
126 crow::black_magic::isEquP(str, "OPTIONS", 7) ? verb::options :
127 crow::black_magic::isEquP(str, "CONNECT", 7) ? verb::connect :
128 crow::black_magic::isEquP(str, "TRACE", 5) ? verb::trace :
129 crow::black_magic::isEquP(str, "PATCH", 5) ? verb::patch :
130 crow::black_magic::isEquP(str, "PURGE", 5) ? verb::purge :
Ed Tanouse0d918b2018-03-27 17:41:04 -0700131 throw std::runtime_error("invalid http method");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132 // clang-format on
Ed Tanous43b761d2019-02-13 20:10:56 -0800133}