18#include <drogon/drogon_callbacks.h>
23#ifdef __cpp_impl_coroutine
58 virtual void invoke(
const HttpRequestPtr &req,
59 MiddlewareNextCallback &&nextCb,
60 MiddlewareCallback &&mcb) = 0;
71template <
typename T,
bool AutoCreation = true>
75 static constexpr bool isAutoCreation{AutoCreation};
81DROGON_EXPORT
void handleException(
82 const std::exception &,
83 const HttpRequestPtr &,
84 std::function<
void(
const HttpResponsePtr &)> &&);
87#ifdef __cpp_impl_coroutine
89struct [[nodiscard]] MiddlewareNextAwaiter
90 :
public CallbackAwaiter<HttpResponsePtr>
93 MiddlewareNextAwaiter(MiddlewareNextCallback &&nextCb)
94 : nextCb_(
std::move(nextCb))
98 void await_suspend(std::coroutine_handle<> handle)
noexcept
100 nextCb_([
this, handle](
const HttpResponsePtr &resp) {
107 MiddlewareNextCallback nextCb_;
110template <
typename T,
bool AutoCreation = true>
114 static constexpr bool isAutoCreation{AutoCreation};
115 ~HttpCoroMiddleware()
override =
default;
117 void invoke(
const HttpRequestPtr &req,
118 MiddlewareNextCallback &&nextCb,
119 MiddlewareCallback &&mcb)
final
123 nextCb = std::move(nextCb),
124 mcb = std::move(mcb)]()
mutable -> drogon::Task<> {
125 HttpResponsePtr resp;
128 resp =
co_await invoke(req, {std::move(nextCb)});
130 catch (
const std::exception &ex)
132 internal::handleException(ex, req, std::move(mcb));
137 LOG_ERROR <<
"Exception not derived from std::exception";
145 virtual Task<HttpResponsePtr> invoke(
const HttpRequestPtr &req,
146 MiddlewareNextAwaiter &&next) = 0;
166template <
class Derived,
bool AutoCreation = true>
172 MiddlewareNextCallback &&nextCb,
173 MiddlewareCallback &&mcb)
override
176 if (req->method() == drogon::HttpMethod::Options)
177 req->attributes()->insert(
"drogon.customCORShandling",
true);
179 nextCb(std::move(mcb));
The base class for all drogon reflection classes.
Definition DrObject.h:34
The abstract base class for middleware.
Definition HttpMiddleware.h:33
virtual void invoke(const HttpRequestPtr &req, MiddlewareNextCallback &&nextCb, MiddlewareCallback &&mcb)=0
The reflection base class template for middlewares.
Definition HttpMiddleware.h:73
Definition HttpMiddleware.h:185
Simple middleware that tags OPTIONS requests.
Definition HttpMiddleware.h:169
void invoke(const HttpRequestPtr &req, MiddlewareNextCallback &&nextCb, MiddlewareCallback &&mcb) override
Definition HttpMiddleware.h:171
Definition HttpMiddleware.h:190
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
void async_run(Coro &&coro)
Runs a coroutine from a regular function.
Definition coroutine.h:749