drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
HttpFilter.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <drogon/DrObject.h>
18#include <drogon/drogon_callbacks.h>
19#include <drogon/HttpRequest.h>
20#include <drogon/HttpResponse.h>
22#include <memory>
23
24#ifdef __cpp_impl_coroutine
26#endif
27
28namespace drogon
29{
34class DROGON_EXPORT HttpFilterBase : public virtual DrObjectBase,
36{
37 public:
39
49 virtual void doFilter(const HttpRequestPtr &req,
50 FilterCallback &&fcb,
51 FilterChainCallback &&fccb) = 0;
52 ~HttpFilterBase() override = default;
53
54 private:
55 void invoke(const HttpRequestPtr &req,
56 MiddlewareNextCallback &&nextCb,
57 MiddlewareCallback &&mcb) final
58 {
59 auto mcbPtr = std::make_shared<MiddlewareCallback>(std::move(mcb));
61 req,
62 [mcbPtr](const HttpResponsePtr &resp) {
63 (*mcbPtr)(resp);
64 }, // fcb, intercept the response
65 [nextCb = std::move(nextCb), mcbPtr]() mutable {
66 nextCb([mcbPtr = std::move(mcbPtr)](
67 const HttpResponsePtr &resp) { (*mcbPtr)(resp); });
68 } // fccb, call the next middleware
69 );
70 }
71};
72
80template <typename T, bool AutoCreation = true>
81class HttpFilter : public DrObject<T>, public HttpFilterBase
82{
83 public:
84 static constexpr bool isAutoCreation{AutoCreation};
85 ~HttpFilter() override = default;
86};
87
88#ifdef __cpp_impl_coroutine
89template <typename T, bool AutoCreation = true>
90class HttpCoroFilter : public DrObject<T>, public HttpFilterBase
91{
92 public:
93 static constexpr bool isAutoCreation{AutoCreation};
94 ~HttpCoroFilter() override = default;
95
96 void doFilter(const HttpRequestPtr &req,
97 FilterCallback &&fcb,
98 FilterChainCallback &&fccb) final
99 {
100 drogon::async_run([this,
101 req,
102 fcb = std::move(fcb),
103 fccb = std::move(fccb)]() mutable -> drogon::Task<> {
104 HttpResponsePtr resp;
105 try
106 {
107 resp = co_await doFilter(req);
108 }
109 catch (const std::exception &ex)
110 {
111 internal::handleException(ex, req, std::move(fcb));
112 co_return;
113 }
114 catch (...)
115 {
116 LOG_ERROR << "Exception not derived from std::exception";
117 co_return;
118 }
119
120 if (resp)
121 {
122 fcb(resp);
123 }
124 else
125 {
126 fccb();
127 }
128 });
129 }
130
131 virtual Task<HttpResponsePtr> doFilter(const HttpRequestPtr &req) = 0;
132};
133#endif
134
135} // namespace drogon
The base class for all drogon reflection classes.
Definition DrObject.h:34
Definition DrObject.h:86
The abstract base class for filters For more details on the class, see the wiki site (the 'Filter' se...
Definition HttpFilter.h:36
virtual void doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb)=0
This virtual function should be overridden in subclasses.
The reflection base class template for filters.
Definition HttpFilter.h:82
The abstract base class for middleware.
Definition HttpMiddleware.h:33
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
Definition coroutine.h:161