diff options
author | Ed Tanous <ed@tanous.net> | 2023-08-07 22:02:40 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-10-31 20:43:04 +0300 |
commit | 27b0cf90f6cba207837f5c263a45c6ea5651975b (patch) | |
tree | 314cda7432edfe44fdd1297664db1399a1d6ba22 /http/complete_response_fields.hpp | |
parent | 522377dcb85082da598403e104a44d621b4c2bb4 (diff) | |
download | bmcweb-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 'http/complete_response_fields.hpp')
-rw-r--r-- | http/complete_response_fields.hpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/http/complete_response_fields.hpp b/http/complete_response_fields.hpp index 66a4bb79af..a5b3eec3d0 100644 --- a/http/complete_response_fields.hpp +++ b/http/complete_response_fields.hpp @@ -27,8 +27,7 @@ inline void completeResponseFields(const Request& req, Response& res) authentication::cleanupTempSession(req); res.setHashAndHandleNotModified(); - - if (res.body().empty() && res.jsonValue.is_structured()) + if (res.jsonValue.is_structured()) { using http_helpers::ContentType; std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON, @@ -44,7 +43,9 @@ inline void completeResponseFields(const Request& req, Response& res) { res.addHeader(boost::beast::http::field::content_type, "application/cbor"); - nlohmann::json::to_cbor(res.jsonValue, res.body()); + std::string cbor; + nlohmann::json::to_cbor(res.jsonValue, cbor); + res.write(std::move(cbor)); } else { @@ -53,8 +54,8 @@ inline void completeResponseFields(const Request& req, Response& res) // backward compatibility. res.addHeader(boost::beast::http::field::content_type, "application/json"); - res.body() = res.jsonValue.dump( - 2, ' ', true, nlohmann::json::error_handler_t::replace); + res.write(res.jsonValue.dump( + 2, ' ', true, nlohmann::json::error_handler_t::replace)); } } } |