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