Break out request completion from connection

There's a large part of the http::Connection class that has nothing to
do with the connection at all, and is all about parsing, and finalizing
the response.  Break that portion out into its own method that can (in
the future) be unit tested.

Tested: Redfish service validator passes

Change-Id: Ic608d432e69e25c0e0a1555ecc24ed62adba2664
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index 11964e0..3548870 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -3,12 +3,11 @@
 
 #include "async_resp.hpp"
 #include "authentication.hpp"
+#include "complete_response_fields.hpp"
 #include "http_response.hpp"
 #include "http_utility.hpp"
-#include "json_html_serializer.hpp"
 #include "logging.hpp"
 #include "mutual_tls.hpp"
-#include "security_headers.hpp"
 #include "ssl_key_handler.hpp"
 #include "utility.hpp"
 
@@ -314,66 +313,14 @@
         res = std::move(thisRes);
         res.keepAlive(keepAlive);
 
-        BMCWEB_LOG_INFO << "Response: " << this << ' '
-                        << req->url().encoded_path() << ' ' << res.resultInt()
-                        << " keepalive=" << keepAlive;
-
-        addSecurityHeaders(*req, res);
-
-        crow::authentication::cleanupTempSession(*req);
+        completeResponseFields(*req, res);
 
         if (!isAlive())
         {
-            // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " <<
-            // isReading
-            // << ' ' << isWriting;
-            // delete this;
-
-            // delete lambda with self shared_ptr
-            // to enable connection destruction
             res.setCompleteRequestHandler(nullptr);
             return;
         }
 
-        res.setHashAndHandleNotModified();
-
-        if (res.body().empty() && res.jsonValue.is_structured())
-        {
-            using http_helpers::ContentType;
-            std::array<ContentType, 3> allowed{
-                ContentType::CBOR, ContentType::JSON, ContentType::HTML};
-            ContentType prefered =
-                getPreferedContentType(req->getHeaderValue("Accept"), allowed);
-
-            if (prefered == ContentType::HTML)
-            {
-                json_html_util::prettyPrintJson(res);
-            }
-            else if (prefered == ContentType::CBOR)
-            {
-                res.addHeader(boost::beast::http::field::content_type,
-                              "application/cbor");
-                nlohmann::json::to_cbor(res.jsonValue, res.body());
-            }
-            else
-            {
-                // Technically prefered could also be NoMatch here, but we'd
-                // like to default to something rather than return 400 for
-                // 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);
-            }
-        }
-
-        if (res.resultInt() >= 400 && res.body().empty())
-        {
-            res.body() = std::string(res.reason());
-        }
-
-        res.addHeader(boost::beast::http::field::date, getCachedDateStr());
-
         doWrite(res);
 
         // delete lambda with self shared_ptr