3 #include "crow/http_request.h"
4 #include "crow/http_response.h"
5 #include "crow/utility.h"
18 using call_global = std::false_type;
26 template<
typename T,
void (T::*)(request&, response&,
typename MW::context&) const = &T::before_handle>
34 template<
typename T,
void (T::*)(request&, response&,
typename MW::context&) = &T::before_handle>
42 template<
typename T,
void (T::*)(request&, response&,
typename MW::context&) const = &T::after_handle>
50 template<
typename T,
void (T::*)(request&, response&,
typename MW::context&) = &T::after_handle>
58 template<typename T, typename std::enable_if<T::call_global::value == false, bool>::type =
true>
73 static std::false_type f(...);
76 static const bool value = decltype(f<T>(
nullptr))::value;
89 static std::false_type f(...);
92 static constexpr
bool value = decltype(f<T>(
nullptr))::value;
102 static std::true_type f(...);
104 static const bool value = decltype(f<MW>(
nullptr))::value;
107 template<
typename MW,
typename Context,
typename ParentContext>
108 typename std::enable_if<!is_before_handle_arity_3_impl<MW>::value>::type
109 before_handler_call(MW& mw,
request& req,
response& res, Context& ctx, ParentContext& )
111 mw.before_handle(req, res, ctx.template get<MW>(), ctx);
114 template<
typename MW,
typename Context,
typename ParentContext>
115 typename std::enable_if<is_before_handle_arity_3_impl<MW>::value>::type
116 before_handler_call(MW& mw,
request& req,
response& res, Context& ctx, ParentContext& )
118 mw.before_handle(req, res, ctx.template get<MW>());
121 template<
typename MW,
typename Context,
typename ParentContext>
122 typename std::enable_if<!is_after_handle_arity_3_impl<MW>::value>::type
123 after_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& )
125 mw.after_handle(req, res, ctx.template get<MW>(), ctx);
128 template<
typename MW,
typename Context,
typename ParentContext>
129 typename std::enable_if<is_after_handle_arity_3_impl<MW>::value>::type
130 after_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& )
132 mw.after_handle(req, res, ctx.template get<MW>());
136 template<
typename CallCriteria,
137 int N,
typename Context,
typename Container>
138 typename std::enable_if<(N < std::tuple_size<typename std::remove_reference<Container>::type>::value),
bool>::type
139 middleware_call_helper(
const CallCriteria& cc, Container& middlewares, request& req, response& res, Context& ctx)
142 using CurrentMW =
typename std::tuple_element<N, typename std::remove_reference<Container>::type>::type;
144 if (!cc.template enabled<CurrentMW>(N))
146 return middleware_call_helper<CallCriteria, N + 1, Context, Container>(cc, middlewares, req, res, ctx);
149 using parent_context_t =
typename Context::template partial<N - 1>;
150 before_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx,
static_cast<parent_context_t&
>(ctx));
151 if (res.is_completed())
153 after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx,
static_cast<parent_context_t&
>(ctx));
157 if (middleware_call_helper<CallCriteria, N + 1, Context, Container>(cc, middlewares, req, res, ctx))
159 after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx,
static_cast<parent_context_t&
>(ctx));
166 template<
typename CallCriteria,
int N,
typename Context,
typename Container>
167 typename std::enable_if<(N >= std::tuple_size<typename std::remove_reference<Container>::type>::value),
bool>::type
168 middleware_call_helper(
const CallCriteria& , Container& , request& , response& , Context& )
173 template<
typename CallCriteria,
int N,
typename Context,
typename Container>
174 typename std::enable_if<(N < 0)>::type
175 after_handlers_call_helper(
const CallCriteria& , Container& , Context& , request& , response& )
179 template<
typename CallCriteria,
int N,
typename Context,
typename Container>
180 typename std::enable_if<(N == 0)>::type after_handlers_call_helper(
const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res)
182 using parent_context_t =
typename Context::template partial<N - 1>;
183 using CurrentMW =
typename std::tuple_element<N, typename std::remove_reference<Container>::type>::type;
184 if (cc.template enabled<CurrentMW>(N))
186 after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx,
static_cast<parent_context_t&
>(ctx));
190 template<
typename CallCriteria,
int N,
typename Context,
typename Container>
191 typename std::enable_if<(N > 0)>::type after_handlers_call_helper(
const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res)
193 using parent_context_t =
typename Context::template partial<N - 1>;
194 using CurrentMW =
typename std::tuple_element<N, typename std::remove_reference<Container>::type>::type;
195 if (cc.template enabled<CurrentMW>(N))
197 after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx,
static_cast<parent_context_t&
>(ctx));
199 after_handlers_call_helper<CallCriteria, N - 1, Context, Container>(cc, middlewares, ctx, req, res);
205 template<
typename MW>
206 constexpr
bool enabled(
int)
const
212 template<
typename F,
typename... Args>
213 typename std::enable_if<black_magic::CallHelper<F, black_magic::S<Args...>>::value,
void>::type
216 static_assert(!std::is_same<
void, decltype(f(std::declval<Args>()...))>::value,
217 "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable");
223 template<
typename F,
typename... Args>
224 typename std::enable_if<
225 !black_magic::CallHelper<F, black_magic::S<Args...>>::value &&
226 black_magic::CallHelper<F, black_magic::S<
crow::request&, Args...>>::value,
230 static_assert(!std::is_same<
void, decltype(f(std::declval<crow::request>(), std::declval<Args>()...))>::value,
231 "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable");
237 template<
typename F,
typename... Args>
238 typename std::enable_if<
239 !black_magic::CallHelper<F, black_magic::S<Args...>>::value &&
240 !black_magic::CallHelper<F, black_magic::S<
crow::request&, Args...>>::value &&
241 black_magic::CallHelper<F, black_magic::S<
crow::response&, Args...>>::value,
245 static_assert(std::is_same<
void, decltype(f(std::declval<crow::response&>(), std::declval<Args>()...))>::value,
246 "Handler function with response argument should have void return type");
248 f(res, std::forward<Args>(args)...);
251 template<
typename F,
typename... Args>
252 typename std::enable_if<
253 !black_magic::CallHelper<F, black_magic::S<Args...>>::value &&
254 !black_magic::CallHelper<F, black_magic::S<
crow::request&, Args...>>::value &&
255 !black_magic::CallHelper<F, black_magic::S<
crow::response&, Args...>>::value &&
260 static_assert(std::is_same<
void, decltype(f(std::declval<crow::request&>(), std::declval<crow::response&>(), std::declval<Args>()...))>::value,
261 "Handler function with response argument should have void return type");
263 f(req, res, std::forward<Args>(args)...);
267 template<
typename F,
typename... Args>
268 typename std::enable_if<
269 !black_magic::CallHelper<F, black_magic::S<Args...>>::value &&
270 !black_magic::CallHelper<F, black_magic::S<
crow::request&, Args...>>::value &&
271 !black_magic::CallHelper<F, black_magic::S<
crow::response&, Args...>>::value &&
276 static_assert(std::is_same<
void, decltype(f(std::declval<crow::request&>(), std::declval<crow::response&>(), std::declval<Args>()...))>::value,
277 "Handler function with response argument should have void return type");
279 f(req, res, std::forward<Args>(args)...);
282 template<
bool Reversed>
290 indices(indices_), slider(0) {}
293 bool enabled(
int mw_index)
const
295 if (slider <
int(indices.size()) && indices[slider] == mw_index)
304 const std::vector<int>& indices;
312 indices(indices_), slider(
int(indices_.size()) - 1) {}
315 bool enabled(
int mw_index)
const
317 if (slider >= 0 && indices[slider] == mw_index)
326 const std::vector<int>& indices;
The main namespace of the library. In this namespace is defined the most important classes and functi...
Local middleware should extend ILocalMiddleware.
Definition: middleware.h:17
Definition: middleware.h:52
Definition: middleware.h:44
Definition: middleware.h:41
Definition: middleware.h:49
Definition: middleware.h:36
Definition: middleware.h:28
Definition: middleware.h:25
Definition: middleware.h:33
Definition: middleware.h:60
Definition: middleware.h:57
Definition: middleware.h:81
Definition: middleware.h:65
Definition: middleware.h:97
Definition: middleware.h:284
Definition: middleware.h:204
An HTTP request.
Definition: http_request.h:36
HTTP response.
Definition: http_response.h:34
void end()
Set the response completion flag and call the handler (to send the response).
Definition: http_response.h:237