drogon
C++14/17-based HTTP application framework
Toggle main menu visibility
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
>
21
#include <
drogon/HttpMiddleware.h
>
22
#include <memory>
23
24
#ifdef __cpp_impl_coroutine
25
#include <
drogon/utils/coroutine.h
>
26
#endif
27
28
namespace
drogon
29
{
34
class
DROGON_EXPORT
HttpFilterBase
:
public
virtual
DrObjectBase
,
35
public
HttpMiddlewareBase
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));
60
doFilter
(
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
80
template
<
typename
T,
bool
AutoCreation = true>
81
class
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
89
template
<
typename
T,
bool
AutoCreation = true>
90
class
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
DrObject.h
HttpMiddleware.h
HttpRequest.h
HttpResponse.h
drogon::DrObjectBase
The base class for all drogon reflection classes.
Definition
DrObject.h:34
drogon::DrObject
Definition
DrObject.h:86
drogon::HttpFilterBase
The abstract base class for filters For more details on the class, see the wiki site (the 'Filter' se...
Definition
HttpFilter.h:36
drogon::HttpFilterBase::doFilter
virtual void doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb)=0
This virtual function should be overridden in subclasses.
drogon::HttpFilter
The reflection base class template for filters.
Definition
HttpFilter.h:82
drogon::HttpMiddlewareBase
The abstract base class for middleware.
Definition
HttpMiddleware.h:33
coroutine.h
drogon
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition
Attribute.h:23
drogon::async_run
void async_run(Coro &&coro)
Runs a coroutine from a regular function.
Definition
coroutine.h:749
drogon::Task
Definition
coroutine.h:161
lib
inc
drogon
HttpFilter.h
Generated by
1.17.0