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>
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index 86cc49a..1ec80ae 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -17,11 +17,11 @@
 #include <boost/asio/ip/tcp.hpp>
 #include <boost/asio/ssl/stream.hpp>
 #include <boost/asio/steady_timer.hpp>
+#include <boost/beast/core/buffers_generator.hpp>
 #include <boost/beast/core/flat_static_buffer.hpp>
 #include <boost/beast/http/error.hpp>
 #include <boost/beast/http/parser.hpp>
 #include <boost/beast/http/read.hpp>
-#include <boost/beast/http/serializer.hpp>
 #include <boost/beast/http/write.hpp>
 #include <boost/beast/ssl/ssl_stream.hpp>
 #include <boost/beast/websocket.hpp>
@@ -532,47 +532,50 @@
         });
     }
 
+    void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
+                      const boost::system::error_code& ec,
+                      std::size_t bytesTransferred)
+    {
+        BMCWEB_LOG_DEBUG("{} async_write {} bytes", logPtr(this),
+                         bytesTransferred);
+
+        cancelDeadlineTimer();
+
+        if (ec)
+        {
+            BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
+            return;
+        }
+        if (!keepAlive)
+        {
+            close();
+            BMCWEB_LOG_DEBUG("{} from write(1)", logPtr(this));
+            return;
+        }
+
+        BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
+        res.clear();
+        parser.emplace(std::piecewise_construct, std::make_tuple());
+        parser->body_limit(httpReqBodyLimit); // reset body limit for
+                                              // newly created parser
+        buffer.consume(buffer.size());
+
+        userSession = nullptr;
+
+        // Destroy the Request via the std::optional
+        req.reset();
+        doReadHeaders();
+    }
+
     void doWrite(crow::Response& thisRes)
     {
         BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
         thisRes.preparePayload();
-        serializer.emplace(thisRes.stringResponse);
+
         startDeadline();
-        boost::beast::http::async_write(adaptor, *serializer,
-                                        [this, self(shared_from_this())](
-                                            const boost::system::error_code& ec,
-                                            std::size_t bytesTransferred) {
-            BMCWEB_LOG_DEBUG("{} async_write {} bytes", logPtr(this),
-                             bytesTransferred);
-
-            cancelDeadlineTimer();
-
-            if (ec)
-            {
-                BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
-                return;
-            }
-            if (!keepAlive)
-            {
-                close();
-                BMCWEB_LOG_DEBUG("{} from write(1)", logPtr(this));
-                return;
-            }
-
-            serializer.reset();
-            BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
-            res.clear();
-            parser.emplace(std::piecewise_construct, std::make_tuple());
-            parser->body_limit(httpReqBodyLimit); // reset body limit for
-                                                  // newly created parser
-            buffer.consume(buffer.size());
-
-            userSession = nullptr;
-
-            // Destroy the Request via the std::optional
-            req.reset();
-            doReadHeaders();
-        });
+        boost::beast::async_write(adaptor, thisRes.generator(),
+                                  std::bind_front(&self_type::afterDoWrite,
+                                                  this, shared_from_this()));
     }
 
     void cancelDeadlineTimer()
@@ -637,10 +640,6 @@
 
     boost::beast::flat_static_buffer<8192> buffer;
 
-    std::optional<boost::beast::http::response_serializer<
-        boost::beast::http::string_body>>
-        serializer;
-
     std::optional<crow::Request> req;
     crow::Response res;