Crow  1.1
A C++ microframework for the web
 
Loading...
Searching...
No Matches
common.h
1#pragma once
2
3#include <vector>
4#include <string>
5#include <stdexcept>
6#include <iostream>
7#include "crow/utility.h"
8
9namespace crow
10{
11 const char cr = '\r';
12 const char lf = '\n';
13 const std::string crlf("\r\n");
14
15 enum class HTTPMethod : char
16 {
17#ifndef DELETE
18 DELETE = 0,
19 GET,
20 HEAD,
21 POST,
22 PUT,
23
24 CONNECT,
25 OPTIONS,
26 TRACE,
27
28 PATCH,
29 PURGE,
30
31 COPY,
32 LOCK,
33 MKCOL,
34 MOVE,
35 PROPFIND,
36 PROPPATCH,
37 SEARCH,
38 UNLOCK,
39 BIND,
40 REBIND,
41 UNBIND,
42 ACL,
43
44 REPORT,
45 MKACTIVITY,
46 CHECKOUT,
47 MERGE,
48
49 MSEARCH,
50 NOTIFY,
51 SUBSCRIBE,
52 UNSUBSCRIBE,
53
54 MKCALENDAR,
55
56 LINK,
57 UNLINK,
58
59 SOURCE,
60#endif
61
62 Delete = 0,
63 Get,
64 Head,
65 Post,
66 Put,
67
68 Connect,
69 Options,
70 Trace,
71
72 Patch,
73 Purge,
74
75 Copy,
76 Lock,
77 MkCol,
78 Move,
79 Propfind,
80 Proppatch,
81 Search,
82 Unlock,
83 Bind,
84 Rebind,
85 Unbind,
86 Acl,
87
88 Report,
89 MkActivity,
90 Checkout,
91 Merge,
92
93 MSearch,
94 Notify,
95 Subscribe,
96 Unsubscribe,
97
98 MkCalendar,
99
100 Link,
101 Unlink,
102
103 Source,
104
105
106 InternalMethodCount,
107 // should not add an item below this line: used for array count
108 };
109
110 constexpr const char* method_strings[] =
111 {
112 "DELETE",
113 "GET",
114 "HEAD",
115 "POST",
116 "PUT",
117
118 "CONNECT",
119 "OPTIONS",
120 "TRACE",
121
122 "PATCH",
123 "PURGE",
124
125 "COPY",
126 "LOCK",
127 "MKCOL",
128 "MOVE",
129 "PROPFIND",
130 "PROPPATCH",
131 "SEARCH",
132 "UNLOCK",
133 "BIND",
134 "REBIND",
135 "UNBIND",
136 "ACL",
137
138 "REPORT",
139 "MKACTIVITY",
140 "CHECKOUT",
141 "MERGE",
142
143 "M-SEARCH",
144 "NOTIFY",
145 "SUBSCRIBE",
146 "UNSUBSCRIBE",
147
148 "MKCALENDAR",
149
150 "LINK",
151 "UNLINK",
152
153 "SOURCE"};
154
155
156 inline std::string method_name(HTTPMethod method)
157 {
158 if (CROW_LIKELY(method < HTTPMethod::InternalMethodCount))
159 {
160 return method_strings[static_cast<unsigned int>(method)];
161 }
162 return "invalid";
163 }
164
165 // clang-format off
166
167 enum status
168 {
169 CONTINUE = 100,
170 SWITCHING_PROTOCOLS = 101,
171
172 OK = 200,
173 CREATED = 201,
174 ACCEPTED = 202,
175 NON_AUTHORITATIVE_INFORMATION = 203,
176 NO_CONTENT = 204,
177 RESET_CONTENT = 205,
178 PARTIAL_CONTENT = 206,
179
180 MULTIPLE_CHOICES = 300,
181 MOVED_PERMANENTLY = 301,
182 FOUND = 302,
183 SEE_OTHER = 303,
184 NOT_MODIFIED = 304,
185 TEMPORARY_REDIRECT = 307,
186 PERMANENT_REDIRECT = 308,
187
188 BAD_REQUEST = 400,
189 UNAUTHORIZED = 401,
190 FORBIDDEN = 403,
191 NOT_FOUND = 404,
192 METHOD_NOT_ALLOWED = 405,
193 NOT_ACCEPTABLE = 406,
194 PROXY_AUTHENTICATION_REQUIRED = 407,
195 CONFLICT = 409,
196 GONE = 410,
197 PAYLOAD_TOO_LARGE = 413,
198 UNSUPPORTED_MEDIA_TYPE = 415,
199 RANGE_NOT_SATISFIABLE = 416,
200 EXPECTATION_FAILED = 417,
201 PRECONDITION_REQUIRED = 428,
202 TOO_MANY_REQUESTS = 429,
203 UNAVAILABLE_FOR_LEGAL_REASONS = 451,
204
205 INTERNAL_SERVER_ERROR = 500,
206 NOT_IMPLEMENTED = 501,
207 BAD_GATEWAY = 502,
208 SERVICE_UNAVAILABLE = 503,
209 GATEWAY_TIMEOUT = 504,
210 VARIANT_ALSO_NEGOTIATES = 506
211 };
212
213 // clang-format on
214
215 enum class ParamType : char
216 {
217 INT,
218 UINT,
219 DOUBLE,
220 STRING,
221 PATH,
222
223 MAX
224 };
225
226 /// @cond SKIP
227 struct routing_params
228 {
229 std::vector<int64_t> int_params;
230 std::vector<uint64_t> uint_params;
231 std::vector<double> double_params;
232 std::vector<std::string> string_params;
233
234 void debug_print() const
235 {
236 std::cerr << "routing_params" << std::endl;
237 for (auto i : int_params)
238 std::cerr << i << ", ";
239 std::cerr << std::endl;
240 for (auto i : uint_params)
241 std::cerr << i << ", ";
242 std::cerr << std::endl;
243 for (auto i : double_params)
244 std::cerr << i << ", ";
245 std::cerr << std::endl;
246 for (auto& i : string_params)
247 std::cerr << i << ", ";
248 std::cerr << std::endl;
249 }
250
251 template<typename T>
252 T get(unsigned) const;
253 };
254
255 template<>
256 inline int64_t routing_params::get<int64_t>(unsigned index) const
257 {
258 return int_params[index];
259 }
260
261 template<>
262 inline uint64_t routing_params::get<uint64_t>(unsigned index) const
263 {
264 return uint_params[index];
265 }
266
267 template<>
268 inline double routing_params::get<double>(unsigned index) const
269 {
270 return double_params[index];
271 }
272
273 template<>
274 inline std::string routing_params::get<std::string>(unsigned index) const
275 {
276 return string_params[index];
277 }
278 /// @endcond
279
281 {
282 bool catch_all{false};
283 uint16_t rule_index;
284 std::vector<uint16_t> blueprint_indices;
285 routing_params r_params;
286 HTTPMethod method;
287
289
290 routing_handle_result(uint16_t rule_index_, std::vector<uint16_t> blueprint_indices_, routing_params r_params_):
291 rule_index(rule_index_),
292 blueprint_indices(blueprint_indices_),
293 r_params(r_params_) {}
294
295 routing_handle_result(uint16_t rule_index_, std::vector<uint16_t> blueprint_indices_, routing_params r_params_, HTTPMethod method_):
296 rule_index(rule_index_),
297 blueprint_indices(blueprint_indices_),
298 r_params(r_params_),
299 method(method_) {}
300 };
301} // namespace crow
302
303// clang-format off
304#ifndef CROW_MSVC_WORKAROUND
305constexpr crow::HTTPMethod method_from_string(const char* str)
306{
307 return crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get :
308 crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete :
309 crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head :
310 crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post :
311 crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put :
312
313 crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options :
314 crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect :
315 crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace :
316
317 crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
318 crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge :
319 crow::black_magic::is_equ_p(str, "COPY", 4) ? crow::HTTPMethod::Copy :
320 crow::black_magic::is_equ_p(str, "LOCK", 4) ? crow::HTTPMethod::Lock :
321 crow::black_magic::is_equ_p(str, "MKCOL", 5) ? crow::HTTPMethod::MkCol :
322 crow::black_magic::is_equ_p(str, "MOVE", 4) ? crow::HTTPMethod::Move :
323 crow::black_magic::is_equ_p(str, "PROPFIND", 8) ? crow::HTTPMethod::Propfind :
324 crow::black_magic::is_equ_p(str, "PROPPATCH", 9) ? crow::HTTPMethod::Proppatch :
325 crow::black_magic::is_equ_p(str, "SEARCH", 6) ? crow::HTTPMethod::Search :
326 crow::black_magic::is_equ_p(str, "UNLOCK", 6) ? crow::HTTPMethod::Unlock :
327 crow::black_magic::is_equ_p(str, "BIND", 4) ? crow::HTTPMethod::Bind :
328 crow::black_magic::is_equ_p(str, "REBIND", 6) ? crow::HTTPMethod::Rebind :
329 crow::black_magic::is_equ_p(str, "UNBIND", 6) ? crow::HTTPMethod::Unbind :
330 crow::black_magic::is_equ_p(str, "ACL", 3) ? crow::HTTPMethod::Acl :
331
332 crow::black_magic::is_equ_p(str, "REPORT", 6) ? crow::HTTPMethod::Report :
333 crow::black_magic::is_equ_p(str, "MKACTIVITY", 10) ? crow::HTTPMethod::MkActivity :
334 crow::black_magic::is_equ_p(str, "CHECKOUT", 8) ? crow::HTTPMethod::Checkout :
335 crow::black_magic::is_equ_p(str, "MERGE", 5) ? crow::HTTPMethod::Merge :
336
337 crow::black_magic::is_equ_p(str, "MSEARCH", 7) ? crow::HTTPMethod::MSearch :
338 crow::black_magic::is_equ_p(str, "NOTIFY", 6) ? crow::HTTPMethod::Notify :
339 crow::black_magic::is_equ_p(str, "SUBSCRIBE", 9) ? crow::HTTPMethod::Subscribe :
340 crow::black_magic::is_equ_p(str, "UNSUBSCRIBE", 11) ? crow::HTTPMethod::Unsubscribe :
341
342 crow::black_magic::is_equ_p(str, "MKCALENDAR", 10) ? crow::HTTPMethod::MkCalendar :
343
344 crow::black_magic::is_equ_p(str, "LINK", 4) ? crow::HTTPMethod::Link :
345 crow::black_magic::is_equ_p(str, "UNLINK", 6) ? crow::HTTPMethod::Unlink :
346
347 crow::black_magic::is_equ_p(str, "SOURCE", 6) ? crow::HTTPMethod::Source :
348 throw std::runtime_error("invalid http method");
349}
350
351constexpr crow::HTTPMethod operator""_method(const char* str, size_t /*len*/)
352{
353 return method_from_string( str );
354}
355#endif
356// clang-format on
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition common.h:281