summaryrefslogtreecommitdiff
path: root/http/complete_response_fields.hpp
blob: 41314b8952e641af3d3d210505460108de7b4164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once

#include "boost_formatters.hpp"
#include "http_body.hpp"
#include "http_response.hpp"
#include "http_utility.hpp"
#include "json_html_serializer.hpp"
#include "logging.hpp"
#include "security_headers.hpp"

#include <boost/beast/http/field.hpp>
#include <nlohmann/json.hpp>

#include <array>
#include <string>
#include <string_view>
#include <utility>

namespace crow
{

inline void handleEncoding(std::string_view acceptEncoding, Response& res)
{
    using bmcweb::CompressionType;
    using enum bmcweb::CompressionType;
    using http_helpers::Encoding;
    using enum http_helpers::Encoding;
    // If the payload is currently compressed, see if we can avoid
    // decompressing it by sending it to the client directly
    switch (res.response.body().compressionType)
    {
        case Zstd:
        {
            std::array<Encoding, 1> allowedEnc{ZSTD};
            Encoding encoding =
                http_helpers::getPreferredEncoding(acceptEncoding, allowedEnc);

            if (encoding == ZSTD)
            {
                // If the client supports returning zstd directly, allow that.
                res.response.body().clientCompressionType = Zstd;
            }
        }
        break;
        case Gzip:
        {
            std::array<Encoding, 1> allowedEnc{GZIP};
            Encoding encoding =
                http_helpers::getPreferredEncoding(acceptEncoding, allowedEnc);
            if (encoding != GZIP)
            {
                BMCWEB_LOG_WARNING(
                    "Unimplemented: Returning gzip payload to client that did not explicitly allow it.");
            }
        }
        break;
        default:
            break;
    }
}

inline void completeResponseFields(
    std::string_view accepts, std::string_view acceptEncoding, Response& res)
{
    BMCWEB_LOG_INFO("Response: {}", res.resultInt());
    addSecurityHeaders(res);

    res.setHashAndHandleNotModified();
    if (res.jsonValue.is_structured())
    {
        using http_helpers::ContentType;
        std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON,
                                           ContentType::HTML};
        ContentType preferred = getPreferredContentType(accepts, allowed);

        if (preferred == ContentType::HTML)
        {
            json_html_util::prettyPrintJson(res);
        }
        else if (preferred == ContentType::CBOR)
        {
            res.addHeader(boost::beast::http::field::content_type,
                          "application/cbor");
            std::string cbor;
            nlohmann::json::to_cbor(res.jsonValue, cbor);
            res.write(std::move(cbor));
        }
        else
        {
            // Technically preferred 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.write(res.jsonValue.dump(
                2, ' ', true, nlohmann::json::error_handler_t::replace));
        }
    }

    handleEncoding(acceptEncoding, res);
}
} // namespace crow