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/include/image_upload.hpp b/include/image_upload.hpp
index 3786c30..651d242 100644
--- a/include/image_upload.hpp
+++ b/include/image_upload.hpp
@@ -17,14 +17,15 @@
static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
-inline void uploadImageHandler(const crow::Request& req, crow::Response& res)
+inline void
+ uploadImageHandler(const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
// Only allow one FW update at a time
if (fwUpdateMatcher != nullptr)
{
- res.addHeader("Retry-After", "30");
- res.result(boost::beast::http::status::service_unavailable);
- res.end();
+ asyncResp->res.addHeader("Retry-After", "30");
+ asyncResp->res.result(boost::beast::http::status::service_unavailable);
return;
}
// Make this const static so it survives outside this method
@@ -33,7 +34,7 @@
timeout.expires_after(std::chrono::seconds(15));
- auto timeoutHandler = [&res](const boost::system::error_code& ec) {
+ auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
fwUpdateMatcher = nullptr;
if (ec == boost::asio::error::operation_aborted)
{
@@ -48,18 +49,17 @@
return;
}
- res.result(boost::beast::http::status::bad_request);
- res.jsonValue = {
+ asyncResp->res.result(boost::beast::http::status::bad_request);
+ asyncResp->res.jsonValue = {
{"data",
{{"description",
"Version already exists or failed to be extracted"}}},
{"message", "400 Bad Request"},
{"status", "error"}};
- res.end();
};
std::function<void(sdbusplus::message::message&)> callback =
- [&res](sdbusplus::message::message& m) {
+ [asyncResp](sdbusplus::message::message& m) {
BMCWEB_LOG_DEBUG << "Match fired";
sdbusplus::message::object_path path;
@@ -82,10 +82,9 @@
leaf = path.str;
}
- res.jsonValue = {
+ asyncResp->res.jsonValue = {
{"data", leaf}, {"message", "200 OK"}, {"status", "ok"}};
BMCWEB_LOG_DEBUG << "ending response";
- res.end();
fwUpdateMatcher = nullptr;
}
};
@@ -111,14 +110,16 @@
BMCWEB_ROUTE(app, "/upload/image/<str>")
.privileges({"ConfigureComponents", "ConfigureManager"})
.methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
- [](const crow::Request& req, crow::Response& res,
- const std::string&) { uploadImageHandler(req, res); });
+ [](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string&) { uploadImageHandler(req, asyncResp); });
BMCWEB_ROUTE(app, "/upload/image")
.privileges({"ConfigureComponents", "ConfigureManager"})
.methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
- [](const crow::Request& req, crow::Response& res) {
- uploadImageHandler(req, res);
+ [](const crow::Request& req,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
+ uploadImageHandler(req, asyncResp);
});
}
} // namespace image_upload