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/http2_connection.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/http2_connection.hpp')
-rw-r--r-- | http/http2_connection.hpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/http/http2_connection.hpp b/http/http2_connection.hpp index d815119e96..ee3a218807 100644 --- a/http/http2_connection.hpp +++ b/http/http2_connection.hpp @@ -106,19 +106,42 @@ class HTTP2Connection : BMCWEB_LOG_DEBUG("File read callback length: {}", length); crow::Response& res = stream.res; - BMCWEB_LOG_DEBUG("total: {} send_sofar: {}", res.body().size(), - stream.sentSofar); + Response::string_response* body = + boost::variant2::get_if<Response::string_response>(&res.response); + Response::file_response* fbody = + boost::variant2::get_if<Response::file_response>(&res.response); - size_t toSend = std::min(res.body().size() - stream.sentSofar, length); + size_t size = res.size(); + BMCWEB_LOG_DEBUG("total: {} send_sofar: {}", size, stream.sentSofar); + + size_t toSend = std::min(size - stream.sentSofar, length); BMCWEB_LOG_DEBUG("Copying {} bytes to buf", toSend); - std::string::iterator bodyBegin = res.body().begin(); - std::advance(bodyBegin, stream.sentSofar); + if (body != nullptr) + { + std::string::const_iterator bodyBegin = body->body().begin(); + std::advance(bodyBegin, stream.sentSofar); + + memcpy(buf, &*bodyBegin, toSend); + } + else if (fbody != nullptr) + { + boost::system::error_code ec; + + size_t nread = fbody->body().file().read(buf, toSend, ec); + if (ec || nread != toSend) + { + return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; + } + } + else + { + return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; + } - memcpy(buf, &*bodyBegin, toSend); stream.sentSofar += toSend; - if (stream.sentSofar >= res.body().size()) + if (stream.sentSofar >= size) { BMCWEB_LOG_DEBUG("Setting OEF flag"); *dataFlags |= NGHTTP2_DATA_FLAG_EOF; @@ -154,8 +177,8 @@ class HTTP2Connection : completeResponseFields(thisReq, thisRes); thisRes.addHeader(boost::beast::http::field::date, getCachedDateStr()); - boost::beast::http::fields& fields = thisRes.stringResponse.base(); - std::string code = std::to_string(thisRes.stringResponse.result_int()); + boost::beast::http::fields& fields = thisRes.fields(); + std::string code = std::to_string(thisRes.resultInt()); hdr.emplace_back(headerFromStringViews(":status", code)); for (const boost::beast::http::fields::value_type& header : fields) { |