summaryrefslogtreecommitdiff
path: root/http/http_response.hpp
diff options
context:
space:
mode:
authorNan Zhou <nanzhoumails@gmail.com>2022-01-28 04:06:51 +0300
committerEd Tanous <ed@tanous.net>2022-03-01 02:30:04 +0300
commit72374eb7fe42257e866dd088bc13520b0b28cffa (patch)
treee3245cf0282b19fc7d2e50300e7dfc41229b6d62 /http/http_response.hpp
parenta94ac61f69fd3768cf609adc75cfd646c558eb7e (diff)
downloadbmcweb-72374eb7fe42257e866dd088bc13520b0b28cffa.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). It will be used in future CLs for Query Parameters. 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. Also fixed a self-move assignment bug. Context: Originally submitted: https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/480020 Reveted here: https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/48880 Because of failures here: https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/48864 Tested: 1. Romulus QEMU + Robot tests; all passed 2. Use scripts/websocket_test.py to test websockets. It is still work correctly. 3. Tested in real hardware; no new validator errors; tested both authless, session, and basic auth. 4. Hacked codes to return 500 errors on certain resource; response is expected; 5. Tested Eventing, the push style one (not SSE which is still under review), worked as expected. 6. Tested 404 errors; response is expected. Signed-off-by: Nan Zhou <nanzhoumails@gmail.com> Signed-off-by: John Edward Broadbent <jebr@google.com> Change-Id: I52adb174476e0f6656335baa6657456752a031be
Diffstat (limited to 'http/http_response.hpp')
-rw-r--r--http/http_response.hpp54
1 files changed, 40 insertions, 14 deletions
diff --git a/http/http_response.hpp b/http/http_response.hpp
index 08b76f2478..3c2a3f98b9 100644
--- a/http/http_response.hpp
+++ b/http/http_response.hpp
@@ -43,15 +43,25 @@ struct Response
Response(const Response&) = delete;
Response(Response&&) = delete;
+
Response& operator=(const Response& r) = delete;
Response& operator=(Response&& r) noexcept
{
- BMCWEB_LOG_DEBUG << "Moving response containers";
+ BMCWEB_LOG_DEBUG << "Moving response containers; this: " << this
+ << "; other: " << &r;
+ if (this == &r)
+ {
+ return *this;
+ }
stringResponse = std::move(r.stringResponse);
r.stringResponse.emplace(response_type{});
jsonValue = std::move(r.jsonValue);
completed = r.completed;
+ completeRequestHandler = std::move(r.completeRequestHandler);
+ isAliveHelper = std::move(r.isAliveHelper);
+ r.completeRequestHandler = nullptr;
+ r.isAliveHelper = nullptr;
return *this;
}
@@ -117,37 +127,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