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
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index 61d3f7b..6a874a7 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -334,7 +334,7 @@
if (thisReq.getHeaderValue(boost::beast::http::field::host).empty())
{
res.result(boost::beast::http::status::bad_request);
- completeRequest();
+ completeRequest(res);
return;
}
}
@@ -353,25 +353,27 @@
if (res.completed)
{
- completeRequest();
+ completeRequest(res);
return;
}
#ifndef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
if (!crow::authorization::isOnAllowlist(req->url, req->method()) &&
thisReq.session == nullptr)
{
- BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed";
+ BMCWEB_LOG_WARNING << "Authentication failed";
forward_unauthorized::sendUnauthorized(
req->url, req->getHeaderValue("User-Agent"),
req->getHeaderValue("Accept"), res);
- completeRequest();
+ completeRequest(res);
return;
}
#endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
- res.setCompleteRequestHandler([self(shared_from_this())] {
- boost::asio::post(self->adaptor.get_executor(),
- [self] { self->completeRequest(); });
- });
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+ BMCWEB_LOG_DEBUG << "Setting completion handler";
+ asyncResp->res.setCompleteRequestHandler(
+ [self(shared_from_this())](crow::Response& thisRes) {
+ self->completeRequest(thisRes);
+ });
if (thisReq.isUpgrade() &&
boost::iequals(
@@ -381,10 +383,9 @@
handler->handleUpgrade(thisReq, res, std::move(adaptor));
// delete lambda with self shared_ptr
// to enable connection destruction
- res.setCompleteRequestHandler(nullptr);
+ asyncResp->res.setCompleteRequestHandler(nullptr);
return;
}
- auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
handler->handle(thisReq, asyncResp);
}
@@ -408,8 +409,7 @@
boost::asio::ip::tcp::socket>>)
{
adaptor.next_layer().close();
-#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
- if (userSession != nullptr)
+ if (sessionIsFromTransport && userSession != nullptr)
{
BMCWEB_LOG_DEBUG
<< this
@@ -417,7 +417,6 @@
persistent_data::SessionStore::getInstance().removeSession(
userSession);
}
-#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
}
else
{
@@ -425,12 +424,13 @@
}
}
- void completeRequest()
+ void completeRequest(crow::Response& thisRes)
{
if (!req)
{
return;
}
+ res = std::move(thisRes);
BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' '
<< res.resultInt() << " keepalive=" << req->keepAlive();
@@ -483,7 +483,7 @@
res.keepAlive(req->keepAlive());
- doWrite();
+ doWrite(res);
// delete lambda with self shared_ptr
// to enable connection destruction
@@ -627,11 +627,11 @@
});
}
- void doWrite()
+ void doWrite(crow::Response& thisRes)
{
BMCWEB_LOG_DEBUG << this << " doWrite";
- res.preparePayload();
- serializer.emplace(*res.stringResponse);
+ thisRes.preparePayload();
+ serializer.emplace(*thisRes.stringResponse);
startDeadline();
boost::beast::http::async_write(
adaptor, *serializer,