Using AsyncResp everywhere
Get the core using AsyncResp everywhere, and not have each individual handler
creating its own object.We can call app.handle() without fear of the response
getting ended after the first tree is done populating.
Don't use res.end() anymore.
Tested:
1. Validator passed.
Signed-off-by: zhanghaicheng <zhanghch05@inspur.com>
Change-Id: I867367ce4a0caf8c4b3f4e07e06c11feed0782e8
diff --git a/redfish-core/include/node.hpp b/redfish-core/include/node.hpp
index 43506d5..1793a50 100644
--- a/redfish-core/include/node.hpp
+++ b/redfish-core/include/node.hpp
@@ -15,6 +15,7 @@
*/
#pragma once
+#include "async_resp.hpp"
#include "http_request.hpp"
#include "http_response.hpp"
#include "privileges.hpp"
@@ -35,7 +36,8 @@
class Node
{
private:
- bool redfishPreChecks(const crow::Request& req, crow::Response& res)
+ bool redfishPreChecks(const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
std::string_view odataHeader = req.getHeaderValue("OData-Version");
if (odataHeader.empty())
@@ -45,82 +47,88 @@
}
if (odataHeader != "4.0")
{
- redfish::messages::preconditionFailed(res);
- res.end();
+ redfish::messages::preconditionFailed(asyncResp->res);
return false;
}
- res.addHeader("OData-Version", "4.0");
+ asyncResp->res.addHeader("OData-Version", "4.0");
return true;
}
public:
template <typename... Params>
- Node(App& app, std::string&& entityUrl, [[maybe_unused]] Params... paramsIn)
+ Node(App& app, std::string&& entityUrl,
+ [[maybe_unused]] Params... paramsIn) :
+ app(app)
{
crow::DynamicRule& get = app.routeDynamic(entityUrl.c_str());
getRule = &get;
get.methods(boost::beast::http::verb::get)(
- [this](const crow::Request& req, crow::Response& res,
+ [this](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Params... params) {
- if (!redfishPreChecks(req, res))
+ if (!redfishPreChecks(req, asyncResp))
{
return;
}
std::vector<std::string> paramVec = {params...};
- doGet(res, req, paramVec);
+ doGet(asyncResp, req, paramVec);
});
crow::DynamicRule& patch = app.routeDynamic(entityUrl.c_str());
patchRule = &patch;
patch.methods(boost::beast::http::verb::patch)(
- [this](const crow::Request& req, crow::Response& res,
+ [this](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Params... params) {
- if (!redfishPreChecks(req, res))
+ if (!redfishPreChecks(req, asyncResp))
{
return;
}
std::vector<std::string> paramVec = {params...};
- doPatch(res, req, paramVec);
+ doPatch(asyncResp, req, paramVec);
});
crow::DynamicRule& post = app.routeDynamic(entityUrl.c_str());
postRule = &post;
post.methods(boost::beast::http::verb::post)(
- [this](const crow::Request& req, crow::Response& res,
+ [this](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Params... params) {
- if (!redfishPreChecks(req, res))
+ if (!redfishPreChecks(req, asyncResp))
{
return;
}
std::vector<std::string> paramVec = {params...};
- doPost(res, req, paramVec);
+ doPost(asyncResp, req, paramVec);
});
crow::DynamicRule& put = app.routeDynamic(entityUrl.c_str());
putRule = &put;
put.methods(boost::beast::http::verb::put)(
- [this](const crow::Request& req, crow::Response& res,
+ [this](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Params... params) {
- if (!redfishPreChecks(req, res))
+ if (!redfishPreChecks(req, asyncResp))
{
return;
}
std::vector<std::string> paramVec = {params...};
- doPut(res, req, paramVec);
+ doPut(asyncResp, req, paramVec);
});
crow::DynamicRule& deleteR = app.routeDynamic(entityUrl.c_str());
deleteRule = &deleteR;
deleteR.methods(boost::beast::http::verb::delete_)(
- [this](const crow::Request& req, crow::Response& res,
+ [this](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Params... params) {
- if (!redfishPreChecks(req, res))
+ if (!redfishPreChecks(req, asyncResp))
{
return;
}
std::vector<std::string> paramVec = {params...};
- doDelete(res, req, paramVec);
+ doDelete(asyncResp, req, paramVec);
});
}
@@ -179,40 +187,36 @@
crow::DynamicRule* deleteRule = nullptr;
protected:
+ App& app;
// Node is designed to be an abstract class, so doGet is pure virtual
- virtual void doGet(crow::Response& res, const crow::Request&,
- const std::vector<std::string>&)
+ virtual void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const crow::Request&, const std::vector<std::string>&)
{
- res.result(boost::beast::http::status::method_not_allowed);
- res.end();
+ asyncResp->res.result(boost::beast::http::status::method_not_allowed);
}
- virtual void doPatch(crow::Response& res, const crow::Request&,
- const std::vector<std::string>&)
+ virtual void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const crow::Request&, const std::vector<std::string>&)
{
- res.result(boost::beast::http::status::method_not_allowed);
- res.end();
+ asyncResp->res.result(boost::beast::http::status::method_not_allowed);
}
- virtual void doPost(crow::Response& res, const crow::Request&,
- const std::vector<std::string>&)
+ virtual void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const crow::Request&, const std::vector<std::string>&)
{
- res.result(boost::beast::http::status::method_not_allowed);
- res.end();
+ asyncResp->res.result(boost::beast::http::status::method_not_allowed);
}
- virtual void doPut(crow::Response& res, const crow::Request&,
- const std::vector<std::string>&)
+ virtual void doPut(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const crow::Request&, const std::vector<std::string>&)
{
- res.result(boost::beast::http::status::method_not_allowed);
- res.end();
+ asyncResp->res.result(boost::beast::http::status::method_not_allowed);
}
- virtual void doDelete(crow::Response& res, const crow::Request&,
- const std::vector<std::string>&)
+ virtual void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const crow::Request&, const std::vector<std::string>&)
{
- res.result(boost::beast::http::status::method_not_allowed);
- res.end();
+ asyncResp->res.result(boost::beast::http::status::method_not_allowed);
}
/* @brief Would the operation be allowed if the user did not have the
diff --git a/redfish-core/include/utils/collection.hpp b/redfish-core/include/utils/collection.hpp
index f1b7f21..b6603b9 100644
--- a/redfish-core/include/utils/collection.hpp
+++ b/redfish-core/include/utils/collection.hpp
@@ -23,7 +23,7 @@
* @return void
*/
inline void
- getCollectionMembers(std::shared_ptr<AsyncResp> aResp,
+ getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
const std::string& collectionPath,
const std::vector<const char*>& interfaces,
const char* subtree = "/xyz/openbmc_project/inventory")
diff --git a/redfish-core/include/utils/fw_utils.hpp b/redfish-core/include/utils/fw_utils.hpp
index 1e29139..c990023 100644
--- a/redfish-core/include/utils/fw_utils.hpp
+++ b/redfish-core/include/utils/fw_utils.hpp
@@ -32,7 +32,7 @@
* @return void
*/
inline void
- populateFirmwareInformation(const std::shared_ptr<AsyncResp>& aResp,
+ populateFirmwareInformation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
const std::string& fwVersionPurpose,
const std::string& activeVersionPropName,
const bool populateLinkToImages)
@@ -324,7 +324,7 @@
*
* @return void
*/
-inline void getFwStatus(const std::shared_ptr<AsyncResp>& asyncResp,
+inline void getFwStatus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::shared_ptr<std::string>& swId,
const std::string& dbusSvc)
{
@@ -382,8 +382,9 @@
* @param[i,o] asyncResp Async response object
* @param[i] fwId The firmware ID
*/
-inline void getFwUpdateableStatus(const std::shared_ptr<AsyncResp>& asyncResp,
- const std::shared_ptr<std::string>& fwId)
+inline void
+ getFwUpdateableStatus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::shared_ptr<std::string>& fwId)
{
crow::connections::systemBus->async_method_call(
[asyncResp, fwId](const boost::system::error_code ec,
diff --git a/redfish-core/include/utils/telemetry_utils.hpp b/redfish-core/include/utils/telemetry_utils.hpp
index 0a3af5f..5872350 100644
--- a/redfish-core/include/utils/telemetry_utils.hpp
+++ b/redfish-core/include/utils/telemetry_utils.hpp
@@ -15,8 +15,9 @@
constexpr const char* metricReportUri =
"/redfish/v1/TelemetryService/MetricReports/";
-inline void getReportCollection(const std::shared_ptr<AsyncResp>& asyncResp,
- const std::string& uri)
+inline void
+ getReportCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string& uri)
{
const std::array<const char*, 1> interfaces = {reportInterface};