Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task<HttpResponsePtr> doFilter(const HttpRequestPtr &req) 这个怎么实现? #2240

Open
mrclassfree opened this issue Jan 16, 2025 · 2 comments

Comments

@mrclassfree
Copy link

我是想在自定义的过滤器中,实现doFilter, 但是我不想用异步回调方式,我想使用协程方式实现。
我看基类 HttpFilter 中有个 virtual Task doFilter(const HttpRequestPtr &req) = 0;

具体有实现这个协程方式doFilter的示例吗?

@an-tao
Copy link
Member

an-tao commented Jan 16, 2025

Task<HttpResponsePtr> CoroFilter::doFilter(const HttpRequestPtr& req)
{
int secs = std::stoi(req->getParameter("secs"));
co_await sleepCoro(trantor::EventLoop::getEventLoopOfCurrentThread(), secs);
co_return {};
}

大概是这个样子,返回空就通过,返回一个构造好的HttpResponsePtr对象就拦截了。

@mrclassfree
Copy link
Author

drogon/lib/tests/integration_test/server/CoroFilter.cpp

Lines 7 to 12 in a3b4779

Task CoroFilter::doFilter(const HttpRequestPtr& req)
{
int secs = std::stoi(req->getParameter("secs"));
co_await sleepCoro(trantor::EventLoop::getEventLoopOfCurrentThread(), secs);
co_return {};
}

大概是这个样子,返回空就通过,返回一个构造好的HttpResponsePtr对象就拦截了。

感谢,我去试一下!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants