Change the completionhandler to accept Res

These modifications are from WIP:Redfish:Query parameters:Only
(https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/47474). And they
will be used in Redfish:Query Parameters:Only.
(https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/38952)
The code changed the completion handle to accept Res to be able to
recall handle with a new Response object.
AsyncResp owns a new res, so there is no need to pass in a res.

Tested:
1.Basic and Token auth both still work.
2.Use scripts/websocket_test.py to test websockets. It is still work
correctly.
python3 websocket_test.py --host 127.0.0.1:2443
This modification is a public part, so you can use any URL to test
this function. The response is the same as before.

Signed-off-by: zhanghaicheng <zhanghch05@inspur.com>
Change-Id: I570e32fb47a9a90fe111fcd1f4054060cd21def3
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index b169309..a0ab1ec 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();
+                completeRequest(res);
                 return;
             }
         }
@@ -351,25 +351,27 @@
 
         if (res.completed)
         {
-            completeRequest();
+            completeRequest(res);
             return;
         }
 
         if (!crow::authorization::isOnAllowlist(req->url, req->method()) &&
             thisReq.session == nullptr)
         {
-            BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed";
+            BMCWEB_LOG_WARNING << "Authentication failed";
             forward_unauthorized::sendUnauthorized(
                 req->url, req->getHeaderValue("User-Agent"),
                 req->getHeaderValue("Accept"), res);
-            completeRequest();
+            completeRequest(res);
             return;
         }
 
-        res.setCompleteRequestHandler([self(shared_from_this())] {
-            boost::asio::post(self->adaptor.get_executor(),
-                              [self] { self->completeRequest(); });
-        });
+        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);
+            });
 
         if (thisReq.isUpgrade() &&
             boost::iequals(
@@ -379,10 +381,9 @@
             handler->handleUpgrade(thisReq, res, std::move(adaptor));
             // delete lambda with self shared_ptr
             // to enable connection destruction
-            res.setCompleteRequestHandler(nullptr);
+            asyncResp->res.setCompleteRequestHandler(nullptr);
             return;
         }
-        auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
         handler->handle(thisReq, asyncResp);
     }
 
@@ -406,8 +407,7 @@
                                          boost::asio::ip::tcp::socket>>)
         {
             adaptor.next_layer().close();
-#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
-            if (userSession != nullptr)
+            if (sessionIsFromTransport && userSession != nullptr)
             {
                 BMCWEB_LOG_DEBUG
                     << this
@@ -415,7 +415,6 @@
                 persistent_data::SessionStore::getInstance().removeSession(
                     userSession);
             }
-#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
         }
         else
         {
@@ -423,12 +422,13 @@
         }
     }
 
-    void completeRequest()
+    void completeRequest(crow::Response& thisRes)
     {
         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();
+        doWrite(res);
 
         // delete lambda with self shared_ptr
         // to enable connection destruction
@@ -655,7 +655,7 @@
             });
     }
 
-    void doWrite()
+    void doWrite(crow::Response& thisRes)
     {
         bool loggedIn = req && req->session;
         if (loggedIn)
@@ -667,8 +667,8 @@
             startDeadline(loggedOutAttempts);
         }
         BMCWEB_LOG_DEBUG << this << " doWrite";
-        res.preparePayload();
-        serializer.emplace(*res.stringResponse);
+        thisRes.preparePayload();
+        serializer.emplace(*thisRes.stringResponse);
         boost::beast::http::async_write(
             adaptor, *serializer,
             [this,