Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | 95c6307 | 2024-03-26 13:19:52 -0700 | [diff] [blame] | 5 | #include "boost_formatters.hpp" |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 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" |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 11 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 12 | #include <boost/beast/http/field.hpp> |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 13 | #include <nlohmann/json.hpp> |
| 14 | |
| 15 | #include <array> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 16 | #include <string> |
| 17 | #include <string_view> |
| 18 | #include <utility> |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 19 | |
| 20 | namespace crow |
| 21 | { |
| 22 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 23 | inline void completeResponseFields(std::string_view accepts, Response& res) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 24 | { |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 25 | BMCWEB_LOG_INFO("Response: {}", res.resultInt()); |
| 26 | addSecurityHeaders(res); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 27 | |
| 28 | res.setHashAndHandleNotModified(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 29 | if (res.jsonValue.is_structured()) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 30 | { |
| 31 | using http_helpers::ContentType; |
| 32 | std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON, |
| 33 | ContentType::HTML}; |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 34 | ContentType preferred = getPreferredContentType(accepts, allowed); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 35 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 36 | if (preferred == ContentType::HTML) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 37 | { |
| 38 | json_html_util::prettyPrintJson(res); |
| 39 | } |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 40 | else if (preferred == ContentType::CBOR) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 41 | { |
| 42 | res.addHeader(boost::beast::http::field::content_type, |
| 43 | "application/cbor"); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 44 | std::string cbor; |
| 45 | nlohmann::json::to_cbor(res.jsonValue, cbor); |
| 46 | res.write(std::move(cbor)); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 47 | } |
| 48 | else |
| 49 | { |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 50 | // Technically preferred could also be NoMatch here, but we'd |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 51 | // like to default to something rather than return 400 for |
| 52 | // backward compatibility. |
| 53 | res.addHeader(boost::beast::http::field::content_type, |
| 54 | "application/json"); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 55 | res.write(res.jsonValue.dump( |
| 56 | 2, ' ', true, nlohmann::json::error_handler_t::replace)); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | } // namespace crow |