diff options
author | zhanghch05 <zhanghch05@inspur.com> | 2021-10-20 13:43:55 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2021-11-16 01:56:22 +0300 |
commit | 91995f3272010875e1559397e98ca93354066a0e (patch) | |
tree | 82704b2dd24a1f149e8dd6901e7936825ca4474c /http/http_response.hpp | |
parent | f6674220424eaacda4450a50e8650dc04399126f (diff) | |
download | bmcweb-91995f3272010875e1559397e98ca93354066a0e.tar.xz |
Change the completionhandler to accept Res
These modifications are from WIP:Redfish:Query parameters:Only
(https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/47474). And they
will be used in Redfish:Query Parameters:Only.
(https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/38952)
The code changed the completion handle to accept Res to be able to
recall handle with a new Response object.
AsyncResp owns a new res, so there is no need to pass in a res.
Tested:
1.Basic and Token auth both still work.
2.Use scripts/websocket_test.py to test websockets. It is still work
correctly.
python3 websocket_test.py --host 127.0.0.1:2443
This modification is a public part, so you can use any URL to test
this function. The response is the same as before.
Signed-off-by: zhanghaicheng <zhanghch05@inspur.com>
Change-Id: I570e32fb47a9a90fe111fcd1f4054060cd21def3
Diffstat (limited to 'http/http_response.hpp')
-rw-r--r-- | http/http_response.hpp | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/http/http_response.hpp b/http/http_response.hpp index a983d4ae51..c99b2c378b 100644 --- a/http/http_response.hpp +++ b/http/http_response.hpp @@ -39,6 +39,8 @@ struct Response Response() : stringResponse(response_type{}) {} + Response(Response&& res) = delete; + Response& operator=(const Response& r) = delete; Response& operator=(Response&& r) noexcept @@ -113,37 +115,53 @@ struct Response { if (completed) { - BMCWEB_LOG_ERROR << "Response was ended twice"; + BMCWEB_LOG_ERROR << this << " Response was ended twice"; return; } completed = true; - BMCWEB_LOG_DEBUG << "calling completion handler"; + BMCWEB_LOG_DEBUG << this << " calling completion handler"; if (completeRequestHandler) { - BMCWEB_LOG_DEBUG << "completion handler was valid"; - completeRequestHandler(); + BMCWEB_LOG_DEBUG << this << " completion handler was valid"; + completeRequestHandler(*this); } } - void end(std::string_view bodyPart) + bool isAlive() { - write(bodyPart); - end(); + return isAliveHelper && isAliveHelper(); } - bool isAlive() + void setCompleteRequestHandler(std::function<void(Response&)>&& handler) { - return isAliveHelper && isAliveHelper(); + BMCWEB_LOG_DEBUG << this << " setting completion handler"; + completeRequestHandler = std::move(handler); + } + + std::function<void(Response&)> releaseCompleteRequestHandler() + { + BMCWEB_LOG_DEBUG << this << " releasing completion handler" + << static_cast<bool>(completeRequestHandler); + std::function<void(Response&)> ret = completeRequestHandler; + completeRequestHandler = nullptr; + return ret; + } + + void setIsAliveHelper(std::function<bool()>&& handler) + { + isAliveHelper = std::move(handler); } - void setCompleteRequestHandler(std::function<void()> newHandler) + std::function<bool()> releaseIsAliveHelper() { - completeRequestHandler = std::move(newHandler); + std::function<bool()> ret = std::move(isAliveHelper); + isAliveHelper = nullptr; + return ret; } private: - bool completed{}; - std::function<void()> completeRequestHandler; + bool completed = false; + std::function<void(Response&)> completeRequestHandler; std::function<bool()> isAliveHelper; // In case of a JSON object, set the Content-Type header |