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