Add local middleware after handlers to request handler

This commit is contained in:
Vladislav Oleshko 2022-02-02 17:37:19 +03:00
parent 69e9ad9c1e
commit 5d6db06706
2 changed files with 19 additions and 6 deletions

View File

@ -19,12 +19,20 @@ namespace crow
template<typename Adaptor, typename Handler, typename... Middlewares>
class Connection;
namespace detail {
template<typename F, typename App, typename... Middlewares>
struct handler_middleware_wrapper;
} // namespace detail
/// HTTP response
struct response
{
template<typename Adaptor, typename Handler, typename... Middlewares>
friend class crow::Connection;
template<typename F, typename App, typename... Middlewares>
friend struct crow::detail::handler_middleware_wrapper;
int code{200}; ///< The Status code for the response.
std::string body; ///< The actual payload containing the response data.
ci_map headers; ///< HTTP headers.

View File

@ -274,13 +274,18 @@ namespace crow
if (completed) return;
wrapped_handler_call(req, res, f, std::forward<Args>(args)...);
auto glob_completion_handler = std::move(res.complete_request_handler_);
auto completion_handler = [&ctx, &container, &req, &res, &glob_completion_handler] {
after_handlers_call_helper<
middleware_call_criteria,
std::tuple_size<typename App::mw_container_t>::value - 1,
typename App::context_t,
typename App::mw_container_t>(container, ctx, req, res);
glob_completion_handler();
};
res.complete_request_handler_ = std::move(completion_handler);
after_handlers_call_helper<
middleware_call_criteria,
std::tuple_size<typename App::mw_container_t>::value - 1,
typename App::context_t,
typename App::mw_container_t>(container, ctx, req, res);
wrapped_handler_call(req, res, f, std::forward<Args>(args)...);
}
F f;