summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEd Tanous <ed@tanous.net>2023-08-07 22:02:40 +0300
committerEd Tanous <ed@tanous.net>2023-10-31 20:43:04 +0300
commit27b0cf90f6cba207837f5c263a45c6ea5651975b (patch)
tree314cda7432edfe44fdd1297664db1399a1d6ba22 /include
parent522377dcb85082da598403e104a44d621b4c2bb4 (diff)
downloadbmcweb-27b0cf90f6cba207837f5c263a45c6ea5651975b.tar.xz
Move to file_body in boost
As is, it reads the whole file into memory before sending it. While fairly fast for the user, this wastes ram, and makes bmcweb less useful on less capable systems. This patch enables using the boost::beast::http::file_body type, which has more efficient serialization semantics than using a std::string. To do this, it adds a openFile() handler to http::Response, which can be used to properly open a file. Once the file is opened, the existing string body is ignored, and the file payload is sent instead. openFile() also returns success or failure, to allow users to properly handle 404s and other errors. To prove that it works, I moved over every instance of direct use of the body() method over to using this, including the webasset handler. The webasset handler specifically should help with system load when doing an initial page load of the webui. Tested: Redfish service validator passes. Change-Id: Ic7ea9ffefdbc81eb985de7edc0fac114822994ad Signed-off-by: Ed Tanous <ed@tanous.net>
Diffstat (limited to 'include')
-rw-r--r--include/forward_unauthorized.hpp4
-rw-r--r--include/openbmc_dbus_rest.hpp5
-rw-r--r--include/webassets.hpp6
3 files changed, 4 insertions, 11 deletions
diff --git a/include/forward_unauthorized.hpp b/include/forward_unauthorized.hpp
index 5d5e744f47..07836cefd1 100644
--- a/include/forward_unauthorized.hpp
+++ b/include/forward_unauthorized.hpp
@@ -34,8 +34,8 @@ inline void sendUnauthorized(std::string_view url,
// If we don't have a webui installed, just return an unauthorized
// body
res.result(boost::beast::http::status::unauthorized);
- res.body() =
- "No authentication provided, and no login UI present to forward to.";
+ res.write(
+ "No authentication provided, and no login UI present to forward to.");
return;
}
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index bade59e11b..e0c16d9e8f 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -2534,8 +2534,7 @@ inline void requestRoutes(App& app)
for (const auto& file : files)
{
- std::ifstream readFile(file.path());
- if (!readFile.good())
+ if (!asyncResp->res.openFile(file))
{
continue;
}
@@ -2564,8 +2563,6 @@ inline void requestRoutes(App& app)
boost::beast::http::field::content_disposition,
contentDispositionParam);
- asyncResp->res.body() = {std::istreambuf_iterator<char>(readFile),
- std::istreambuf_iterator<char>()};
return;
}
asyncResp->res.result(boost::beast::http::status::not_found);
diff --git a/include/webassets.hpp b/include/webassets.hpp
index 4deb7a78ee..5ab03231e4 100644
--- a/include/webassets.hpp
+++ b/include/webassets.hpp
@@ -165,17 +165,13 @@ inline void requestRoutes(App& app)
}
// res.set_header("Cache-Control", "public, max-age=86400");
- std::ifstream inf(absolutePath);
- if (!inf)
+ if (!asyncResp->res.openFile(absolutePath))
{
BMCWEB_LOG_DEBUG("failed to read file");
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
return;
}
-
- asyncResp->res.body() = {std::istreambuf_iterator<char>(inf),
- std::istreambuf_iterator<char>()};
});
}
}