1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <drogon/HttpController.h>
#include <drogon/HttpMiddleware.h>
using namespace drogon;
class Middleware1 : public drogon::HttpMiddleware<Middleware1>
{
public:
Middleware1()
{
// do not omit constructor
void(0);
};
void invoke(const HttpRequestPtr &req,
MiddlewareNextCallback &&nextCb,
MiddlewareCallback &&mcb) override
{
auto ptr = std::make_shared<std::string>("1");
req->attributes()->insert("test-middleware", ptr);
nextCb([req, ptr, mcb = std::move(mcb)](const HttpResponsePtr &resp) {
ptr->append("1");
resp->setBody(*ptr);
mcb(resp);
});
}
};
class Middleware2 : public drogon::HttpMiddleware<Middleware2>
{
public:
Middleware2()
{
// do not omit constructor
void(0);
};
void invoke(const HttpRequestPtr &req,
MiddlewareNextCallback &&nextCb,
MiddlewareCallback &&mcb) override
{
auto ptr = req->attributes()->get<std::shared_ptr<std::string>>(
"test-middleware");
ptr->append("2");
nextCb([req, ptr, mcb = std::move(mcb)](const HttpResponsePtr &resp) {
ptr->append("2");
resp->setBody(*ptr);
mcb(resp);
});
}
};
class Middleware3 : public drogon::HttpMiddleware<Middleware3>
{
public:
Middleware3()
{
// do not omit constructor
void(0);
};
void invoke(const HttpRequestPtr &req,
MiddlewareNextCallback &&nextCb,
MiddlewareCallback &&mcb) override
{
auto ptr = req->attributes()->get<std::shared_ptr<std::string>>(
"test-middleware");
ptr->append("3");
nextCb([req, ptr, mcb = std::move(mcb)](const HttpResponsePtr &resp) {
ptr->append("3");
resp->setBody(*ptr);
mcb(resp);
});
}
};
class MiddlewareBlock : public drogon::HttpMiddleware<MiddlewareBlock>
{
public:
MiddlewareBlock()
{
// do not omit constructor
void(0);
};
void invoke(const HttpRequestPtr &req,
MiddlewareNextCallback &&nextCb,
MiddlewareCallback &&mcb) override
{
auto ptr = req->attributes()->get<std::shared_ptr<std::string>>(
"test-middleware");
ptr->append("block");
mcb(HttpResponse::newHttpResponse());
}
};
#if defined(__cpp_impl_coroutine)
class MiddlewareCoro : public drogon::HttpCoroMiddleware<MiddlewareCoro>
{
public:
MiddlewareCoro()
{
// do not omit constructor
void(0);
};
Task<HttpResponsePtr> invoke(const HttpRequestPtr &req,
MiddlewareNextAwaiter &&nextAwaiter) override
{
auto ptr = req->attributes()->get<std::shared_ptr<std::string>>(
"test-middleware");
ptr->append("coro");
auto resp = co_await nextAwaiter;
ptr->append("coro");
resp->setBody(*ptr);
co_return resp;
}
};
#endif
class MiddlewareTest : public drogon::HttpController<MiddlewareTest>
{
public:
METHOD_LIST_BEGIN
ADD_METHOD_TO(MiddlewareTest::handleRequest,
"/test-middleware",
Get,
"Middleware1",
"Middleware2",
"Middleware3",
"Middleware4");
ADD_METHOD_TO(MiddlewareTest::handleRequest,
"/test-middleware-block",
Get,
"Middleware1",
"Middleware2",
"MiddlewareBlock",
"Middleware3");
#if defined(__cpp_impl_coroutine)
ADD_METHOD_TO(MiddlewareTest::handleRequest,
"/test-middleware-coro",
Get,
"Middleware1",
"Middleware2",
"MiddlewareCoro");
#endif
METHOD_LIST_END
void handleRequest(
const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) const
{
req->attributes()
->get<std::shared_ptr<std::string>>("test-middleware")
->append("test");
callback(HttpResponse::newHttpResponse());
}
};
|