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 WEBDAV_MULTI_STATUS = 207,
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
202 WEBDAV_PRECONDITION_FAILED = 412,
203 WEBDAV_REQUEST_URI_TOO_LONG = 414,
204 WEBDAV_UNPROCESSABLE_ENTITY = 422,
205 WEBDAV_LOCKED = 423,
206 WEBDAV_FAILED_DEPENDENCY = 424,
207
208 PRECONDITION_REQUIRED = 428,
209 TOO_MANY_REQUESTS = 429,
210 UNAVAILABLE_FOR_LEGAL_REASONS = 451,
211
212 INTERNAL_SERVER_ERROR = 500,
213 NOT_IMPLEMENTED = 501,
214 BAD_GATEWAY = 502,
215 SERVICE_UNAVAILABLE = 503,
216 GATEWAY_TIMEOUT = 504,
217 VARIANT_ALSO_NEGOTIATES = 506,
218 WEBDAV_INSUFFICIENT_STORAGE = 507
219 };
220
221 // clang-format on
222
223 enum class ParamType : char
224 {
225 INT,
226 UINT,
227 DOUBLE,
228 STRING,
229 PATH,
230
231 MAX
232 };
233
234 /// @cond SKIP
235 struct routing_params
236 {
237 std::vector<int64_t> int_params;
238 std::vector<uint64_t> uint_params;
239 std::vector<double> double_params;
240 std::vector<std::string> string_params;
241
242 void debug_print() const
243 {
244 std::cerr << "routing_params" << std::endl;
245 for (auto i : int_params)
246 std::cerr << i << ", ";
247 std::cerr << std::endl;
248 for (auto i : uint_params)
249 std::cerr << i << ", ";
250 std::cerr << std::endl;
251 for (auto i : double_params)
252 std::cerr << i << ", ";
253 std::cerr << std::endl;
254 for (auto& i : string_params)
255 std::cerr << i << ", ";
256 std::cerr << std::endl;
257 }
258
259 template<typename T>
260 T get(unsigned) const;
261 };
262
263 template<>
264 inline int64_t routing_params::get<int64_t>(unsigned index) const
265 {
266 return int_params[index];
267 }
268
269 template<>
270 inline uint64_t routing_params::get<uint64_t>(unsigned index) const
271 {
272 return uint_params[index];
273 }
274
275 template<>
276 inline double routing_params::get<double>(unsigned index) const
277 {
278 return double_params[index];
279 }
280
281 template<>
282 inline std::string routing_params::get<std::string>(unsigned index) const
283 {
284 return string_params[index];
285 }
286 /// @endcond
287
289 {
290 bool catch_all{false};
291 size_t rule_index;
292 std::vector<size_t> blueprint_indices;
293 routing_params r_params;
294 HTTPMethod method;
295
297
298 routing_handle_result(size_t rule_index_, std::vector<size_t> blueprint_indices_, routing_params r_params_):
299 rule_index(rule_index_),
300 blueprint_indices(blueprint_indices_),
301 r_params(r_params_) {}
302
303 routing_handle_result(size_t rule_index_, std::vector<size_t> blueprint_indices_, routing_params r_params_, HTTPMethod method_):
304 rule_index(rule_index_),
305 blueprint_indices(blueprint_indices_),
306 r_params(r_params_),
307 method(method_) {}
308 };
309} // namespace crow
310
311// clang-format off
312#ifndef CROW_MSVC_WORKAROUND
313constexpr crow::HTTPMethod method_from_string(const char* str)
314{
315 return crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get :
316 crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete :
317 crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head :
318 crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post :
319 crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put :
320
321 crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options :
322 crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect :
323 crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace :
324
325 crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
326 crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge :
327 crow::black_magic::is_equ_p(str, "COPY", 4) ? crow::HTTPMethod::Copy :
328 crow::black_magic::is_equ_p(str, "LOCK", 4) ? crow::HTTPMethod::Lock :
329 crow::black_magic::is_equ_p(str, "MKCOL", 5) ? crow::HTTPMethod::MkCol :
330 crow::black_magic::is_equ_p(str, "MOVE", 4) ? crow::HTTPMethod::Move :
331 crow::black_magic::is_equ_p(str, "PROPFIND", 8) ? crow::HTTPMethod::Propfind :
332 crow::black_magic::is_equ_p(str, "PROPPATCH", 9) ? crow::HTTPMethod::Proppatch :
333 crow::black_magic::is_equ_p(str, "SEARCH", 6) ? crow::HTTPMethod::Search :
334 crow::black_magic::is_equ_p(str, "UNLOCK", 6) ? crow::HTTPMethod::Unlock :
335 crow::black_magic::is_equ_p(str, "BIND", 4) ? crow::HTTPMethod::Bind :
336 crow::black_magic::is_equ_p(str, "REBIND", 6) ? crow::HTTPMethod::Rebind :
337 crow::black_magic::is_equ_p(str, "UNBIND", 6) ? crow::HTTPMethod::Unbind :
338 crow::black_magic::is_equ_p(str, "ACL", 3) ? crow::HTTPMethod::Acl :
339
340 crow::black_magic::is_equ_p(str, "REPORT", 6) ? crow::HTTPMethod::Report :
341 crow::black_magic::is_equ_p(str, "MKACTIVITY", 10) ? crow::HTTPMethod::MkActivity :
342 crow::black_magic::is_equ_p(str, "CHECKOUT", 8) ? crow::HTTPMethod::Checkout :
343 crow::black_magic::is_equ_p(str, "MERGE", 5) ? crow::HTTPMethod::Merge :
344
345 crow::black_magic::is_equ_p(str, "MSEARCH", 7) ? crow::HTTPMethod::MSearch :
346 crow::black_magic::is_equ_p(str, "NOTIFY", 6) ? crow::HTTPMethod::Notify :
347 crow::black_magic::is_equ_p(str, "SUBSCRIBE", 9) ? crow::HTTPMethod::Subscribe :
348 crow::black_magic::is_equ_p(str, "UNSUBSCRIBE", 11) ? crow::HTTPMethod::Unsubscribe :
349
350 crow::black_magic::is_equ_p(str, "MKCALENDAR", 10) ? crow::HTTPMethod::MkCalendar :
351
352 crow::black_magic::is_equ_p(str, "LINK", 4) ? crow::HTTPMethod::Link :
353 crow::black_magic::is_equ_p(str, "UNLINK", 6) ? crow::HTTPMethod::Unlink :
354
355 crow::black_magic::is_equ_p(str, "SOURCE", 6) ? crow::HTTPMethod::Source :
356 throw std::runtime_error("invalid http method");
357}
358
359constexpr crow::HTTPMethod operator""_method(const char* str, size_t /*len*/)
360{
361 return method_from_string( str );
362}
363#endif
364// clang-format on
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition common.h:289