Revert "Change the completionhandler to accept Res"

This reverts commit 91995f3272010875e1559397e98ca93354066a0e.

Seeing bumps fail.
https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/48864

Please fix, test, and resubmit.

Change-Id: Id539fe66d5a093caf8f22a393f7af7b58ead5247
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index a0ab1ec..b169309 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -332,7 +332,7 @@
             if (thisReq.getHeaderValue(boost::beast::http::field::host).empty())
             {
                 res.result(boost::beast::http::status::bad_request);
-                completeRequest(res);
+                completeRequest();
                 return;
             }
         }
@@ -351,27 +351,25 @@
 
         if (res.completed)
         {
-            completeRequest(res);
+            completeRequest();
             return;
         }
 
         if (!crow::authorization::isOnAllowlist(req->url, req->method()) &&
             thisReq.session == nullptr)
         {
-            BMCWEB_LOG_WARNING << "Authentication failed";
+            BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed";
             forward_unauthorized::sendUnauthorized(
                 req->url, req->getHeaderValue("User-Agent"),
                 req->getHeaderValue("Accept"), res);
-            completeRequest(res);
+            completeRequest();
             return;
         }
 
-        auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
-        BMCWEB_LOG_DEBUG << "Setting completion handler for connection";
-        asyncResp->res.setCompleteRequestHandler(
-            [self(shared_from_this())](crow::Response& res) {
-                self->completeRequest(res);
-            });
+        res.setCompleteRequestHandler([self(shared_from_this())] {
+            boost::asio::post(self->adaptor.get_executor(),
+                              [self] { self->completeRequest(); });
+        });
 
         if (thisReq.isUpgrade() &&
             boost::iequals(
@@ -381,9 +379,10 @@
             handler->handleUpgrade(thisReq, res, std::move(adaptor));
             // delete lambda with self shared_ptr
             // to enable connection destruction
-            asyncResp->res.setCompleteRequestHandler(nullptr);
+            res.setCompleteRequestHandler(nullptr);
             return;
         }
+        auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
         handler->handle(thisReq, asyncResp);
     }
 
@@ -407,7 +406,8 @@
                                          boost::asio::ip::tcp::socket>>)
         {
             adaptor.next_layer().close();
-            if (sessionIsFromTransport && userSession != nullptr)
+#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
+            if (userSession != nullptr)
             {
                 BMCWEB_LOG_DEBUG
                     << this
@@ -415,6 +415,7 @@
                 persistent_data::SessionStore::getInstance().removeSession(
                     userSession);
             }
+#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
         }
         else
         {
@@ -422,13 +423,12 @@
         }
     }
 
-    void completeRequest(crow::Response& thisRes)
+    void completeRequest()
     {
         if (!req)
         {
             return;
         }
-        res = std::move(thisRes);
         BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' '
                         << res.resultInt() << " keepalive=" << req->keepAlive();
 
@@ -481,7 +481,7 @@
 
         res.keepAlive(req->keepAlive());
 
-        doWrite(res);
+        doWrite();
 
         // delete lambda with self shared_ptr
         // to enable connection destruction
@@ -655,7 +655,7 @@
             });
     }
 
-    void doWrite(crow::Response& thisRes)
+    void doWrite()
     {
         bool loggedIn = req && req->session;
         if (loggedIn)
@@ -667,8 +667,8 @@
             startDeadline(loggedOutAttempts);
         }
         BMCWEB_LOG_DEBUG << this << " doWrite";
-        thisRes.preparePayload();
-        serializer.emplace(*thisRes.stringResponse);
+        res.preparePayload();
+        serializer.emplace(*res.stringResponse);
         boost::beast::http::async_write(
             adaptor, *serializer,
             [this,
diff --git a/http/http_response.hpp b/http/http_response.hpp
index c99b2c3..a983d4a 100644
--- a/http/http_response.hpp
+++ b/http/http_response.hpp
@@ -39,8 +39,6 @@
     Response() : stringResponse(response_type{})
     {}
 
-    Response(Response&& res) = delete;
-
     Response& operator=(const Response& r) = delete;
 
     Response& operator=(Response&& r) noexcept
@@ -115,53 +113,37 @@
     {
         if (completed)
         {
-            BMCWEB_LOG_ERROR << this << " Response was ended twice";
+            BMCWEB_LOG_ERROR << "Response was ended twice";
             return;
         }
         completed = true;
-        BMCWEB_LOG_DEBUG << this << " calling completion handler";
+        BMCWEB_LOG_DEBUG << "calling completion handler";
         if (completeRequestHandler)
         {
-            BMCWEB_LOG_DEBUG << this << " completion handler was valid";
-            completeRequestHandler(*this);
+            BMCWEB_LOG_DEBUG << "completion handler was valid";
+            completeRequestHandler();
         }
     }
 
+    void end(std::string_view bodyPart)
+    {
+        write(bodyPart);
+        end();
+    }
+
     bool isAlive()
     {
         return isAliveHelper && isAliveHelper();
     }
 
-    void setCompleteRequestHandler(std::function<void(Response&)>&& handler)
+    void setCompleteRequestHandler(std::function<void()> newHandler)
     {
-        BMCWEB_LOG_DEBUG << this << " setting completion handler";
-        completeRequestHandler = std::move(handler);
-    }
-
-    std::function<void(Response&)> releaseCompleteRequestHandler()
-    {
-        BMCWEB_LOG_DEBUG << this << " releasing completion handler"
-                         << static_cast<bool>(completeRequestHandler);
-        std::function<void(Response&)> ret = completeRequestHandler;
-        completeRequestHandler = nullptr;
-        return ret;
-    }
-
-    void setIsAliveHelper(std::function<bool()>&& handler)
-    {
-        isAliveHelper = std::move(handler);
-    }
-
-    std::function<bool()> releaseIsAliveHelper()
-    {
-        std::function<bool()> ret = std::move(isAliveHelper);
-        isAliveHelper = nullptr;
-        return ret;
+        completeRequestHandler = std::move(newHandler);
     }
 
   private:
-    bool completed = false;
-    std::function<void(Response&)> completeRequestHandler;
+    bool completed{};
+    std::function<void()> completeRequestHandler;
     std::function<bool()> isAliveHelper;
 
     // In case of a JSON object, set the Content-Type header
diff --git a/http/websocket.hpp b/http/websocket.hpp
index 324ffd5..30a9b9f 100644
--- a/http/websocket.hpp
+++ b/http/websocket.hpp
@@ -193,10 +193,10 @@
     {
         BMCWEB_LOG_DEBUG << "Websocket accepted connection";
 
-        auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+        auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
+            res, [this, self(shared_from_this())]() { doRead(); });
 
         asyncResp->res.result(boost::beast::http::status::ok);
-        doRead();
 
         if (openHandler)
         {
diff --git a/include/async_resp.hpp b/include/async_resp.hpp
index 7306017..8e9584c 100644
--- a/include/async_resp.hpp
+++ b/include/async_resp.hpp
@@ -15,17 +15,28 @@
 class AsyncResp
 {
   public:
-    AsyncResp() = default;
+    AsyncResp(crow::Response& response) : res(response)
+    {}
+
+    AsyncResp(crow::Response& response, std::function<void()>&& function) :
+        res(response), func(std::move(function))
+    {}
 
     AsyncResp(const AsyncResp&) = delete;
     AsyncResp(AsyncResp&&) = delete;
 
     ~AsyncResp()
     {
+        if (func && res.result() == boost::beast::http::status::ok)
+        {
+            func();
+        }
+
         res.end();
     }
 
-    crow::Response res;
+    crow::Response& res;
+    std::function<void()> func;
 };
 
 } // namespace bmcweb
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 0096e86..3188188 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -2233,7 +2233,9 @@
             [](const crow::Request&,
                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                const std::string& connection) {
-                introspectObjects(connection, "/", asyncResp);
+                introspectObjects(
+                    connection, "/",
+                    std::make_shared<bmcweb::AsyncResp>(asyncResp->res));
             });
 
     BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index efbb980..af9f466 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -2972,9 +2972,10 @@
         return;
     }
 
-    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+    auto res = std::make_shared<crow::Response>();
+    auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res);
     auto callback =
-        [asyncResp, mapCompleteCb{std::move(mapComplete)}](
+        [res, asyncResp, mapCompleteCb{std::move(mapComplete)}](
             const boost::beast::http::status status,
             const boost::container::flat_map<std::string, std::string>&
                 uriToDbus) { mapCompleteCb(status, uriToDbus); };