1 #pragma once
2 
3 #include "authentication.hpp"
4 #include "boost_formatters.hpp"
5 #include "http_request.hpp"
6 #include "http_response.hpp"
7 #include "http_utility.hpp"
8 #include "json_html_serializer.hpp"
9 #include "logging.hpp"
10 #include "security_headers.hpp"
11 #include "utils/hex_utils.hpp"
12 
13 #include <boost/beast/http/message.hpp>
14 #include <nlohmann/json.hpp>
15 
16 #include <array>
17 
18 namespace crow
19 {
20 
21 inline void completeResponseFields(std::string_view accepts, Response& res)
22 {
23     BMCWEB_LOG_INFO("Response: {}", res.resultInt());
24     addSecurityHeaders(res);
25 
26     res.setHashAndHandleNotModified();
27     if (res.jsonValue.is_structured())
28     {
29         using http_helpers::ContentType;
30         std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON,
31                                            ContentType::HTML};
32         ContentType preferred = getPreferredContentType(accepts, allowed);
33 
34         if (preferred == ContentType::HTML)
35         {
36             json_html_util::prettyPrintJson(res);
37         }
38         else if (preferred == ContentType::CBOR)
39         {
40             res.addHeader(boost::beast::http::field::content_type,
41                           "application/cbor");
42             std::string cbor;
43             nlohmann::json::to_cbor(res.jsonValue, cbor);
44             res.write(std::move(cbor));
45         }
46         else
47         {
48             // Technically preferred could also be NoMatch here, but we'd
49             // like to default to something rather than return 400 for
50             // backward compatibility.
51             res.addHeader(boost::beast::http::field::content_type,
52                           "application/json");
53             res.write(res.jsonValue.dump(
54                 2, ' ', true, nlohmann::json::error_handler_t::replace));
55         }
56     }
57 }
58 } // namespace crow
59