blob: 41314b8952e641af3d3d210505460108de7b4164 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanoused5f8952023-06-22 14:06:22 -07003#pragma once
4
Ed Tanous95c63072024-03-26 13:19:52 -07005#include "boost_formatters.hpp"
Ed Tanousb2539062024-03-12 16:58:35 -07006#include "http_body.hpp"
Ed Tanoused5f8952023-06-22 14:06:22 -07007#include "http_response.hpp"
8#include "http_utility.hpp"
9#include "json_html_serializer.hpp"
10#include "logging.hpp"
11#include "security_headers.hpp"
Ed Tanoused5f8952023-06-22 14:06:22 -070012
Ed Tanousd7857202025-01-28 15:32:26 -080013#include <boost/beast/http/field.hpp>
Ed Tanoused5f8952023-06-22 14:06:22 -070014#include <nlohmann/json.hpp>
15
16#include <array>
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <string>
18#include <string_view>
19#include <utility>
Ed Tanoused5f8952023-06-22 14:06:22 -070020
21namespace crow
22{
23
Ed Tanousb2539062024-03-12 16:58:35 -070024inline void handleEncoding(std::string_view acceptEncoding, Response& res)
25{
26 using bmcweb::CompressionType;
27 using enum bmcweb::CompressionType;
28 using http_helpers::Encoding;
29 using enum http_helpers::Encoding;
30 // If the payload is currently compressed, see if we can avoid
31 // decompressing it by sending it to the client directly
32 switch (res.response.body().compressionType)
33 {
34 case Zstd:
35 {
36 std::array<Encoding, 1> allowedEnc{ZSTD};
37 Encoding encoding =
38 http_helpers::getPreferredEncoding(acceptEncoding, allowedEnc);
39
40 if (encoding == ZSTD)
41 {
42 // If the client supports returning zstd directly, allow that.
43 res.response.body().clientCompressionType = Zstd;
44 }
45 }
46 break;
47 case Gzip:
48 {
49 std::array<Encoding, 1> allowedEnc{GZIP};
50 Encoding encoding =
51 http_helpers::getPreferredEncoding(acceptEncoding, allowedEnc);
52 if (encoding != GZIP)
53 {
54 BMCWEB_LOG_WARNING(
55 "Unimplemented: Returning gzip payload to client that did not explicitly allow it.");
56 }
57 }
58 break;
59 default:
60 break;
61 }
62}
63
64inline void completeResponseFields(
65 std::string_view accepts, std::string_view acceptEncoding, Response& res)
Ed Tanoused5f8952023-06-22 14:06:22 -070066{
Ed Tanous89cda632024-04-16 08:45:54 -070067 BMCWEB_LOG_INFO("Response: {}", res.resultInt());
68 addSecurityHeaders(res);
Ed Tanoused5f8952023-06-22 14:06:22 -070069
70 res.setHashAndHandleNotModified();
Ed Tanous27b0cf92023-08-07 12:02:40 -070071 if (res.jsonValue.is_structured())
Ed Tanoused5f8952023-06-22 14:06:22 -070072 {
73 using http_helpers::ContentType;
74 std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON,
75 ContentType::HTML};
Ed Tanous89cda632024-04-16 08:45:54 -070076 ContentType preferred = getPreferredContentType(accepts, allowed);
Ed Tanoused5f8952023-06-22 14:06:22 -070077
Ed Tanous8ece0e42024-01-02 13:16:50 -080078 if (preferred == ContentType::HTML)
Ed Tanoused5f8952023-06-22 14:06:22 -070079 {
80 json_html_util::prettyPrintJson(res);
81 }
Ed Tanous8ece0e42024-01-02 13:16:50 -080082 else if (preferred == ContentType::CBOR)
Ed Tanoused5f8952023-06-22 14:06:22 -070083 {
84 res.addHeader(boost::beast::http::field::content_type,
85 "application/cbor");
Ed Tanous27b0cf92023-08-07 12:02:40 -070086 std::string cbor;
87 nlohmann::json::to_cbor(res.jsonValue, cbor);
88 res.write(std::move(cbor));
Ed Tanoused5f8952023-06-22 14:06:22 -070089 }
90 else
91 {
Ed Tanous8ece0e42024-01-02 13:16:50 -080092 // Technically preferred could also be NoMatch here, but we'd
Ed Tanoused5f8952023-06-22 14:06:22 -070093 // like to default to something rather than return 400 for
94 // backward compatibility.
95 res.addHeader(boost::beast::http::field::content_type,
96 "application/json");
Ed Tanous27b0cf92023-08-07 12:02:40 -070097 res.write(res.jsonValue.dump(
98 2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanoused5f8952023-06-22 14:06:22 -070099 }
100 }
Ed Tanousb2539062024-03-12 16:58:35 -0700101
102 handleEncoding(acceptEncoding, res);
Ed Tanoused5f8952023-06-22 14:06:22 -0700103}
104} // namespace crow