| 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 | b253906 | 2024-03-12 16:58:35 -0700 | [diff] [blame] | 6 | #include "http_body.hpp" | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 7 | #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 Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 12 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include <boost/beast/http/field.hpp> | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 14 | #include <nlohmann/json.hpp> | 
|  | 15 |  | 
|  | 16 | #include <array> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 17 | #include <string> | 
|  | 18 | #include <string_view> | 
|  | 19 | #include <utility> | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | namespace crow | 
|  | 22 | { | 
|  | 23 |  | 
| Ed Tanous | b253906 | 2024-03-12 16:58:35 -0700 | [diff] [blame] | 24 | inline 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 |  | 
|  | 64 | inline void completeResponseFields( | 
|  | 65 | std::string_view accepts, std::string_view acceptEncoding, Response& res) | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 66 | { | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 67 | BMCWEB_LOG_INFO("Response: {}", res.resultInt()); | 
|  | 68 | addSecurityHeaders(res); | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 69 |  | 
| Corey Ethington | 08fad5d | 2025-07-31 12:14:27 -0400 | [diff] [blame] | 70 | res.setResponseEtagAndHandleNotModified(); | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 71 | if (res.jsonValue.is_structured()) | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 72 | { | 
|  | 73 | using http_helpers::ContentType; | 
|  | 74 | std::array<ContentType, 3> allowed{ContentType::CBOR, ContentType::JSON, | 
|  | 75 | ContentType::HTML}; | 
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 76 | ContentType preferred = getPreferredContentType(accepts, allowed); | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 77 |  | 
| Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 78 | if (preferred == ContentType::HTML) | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 79 | { | 
|  | 80 | json_html_util::prettyPrintJson(res); | 
|  | 81 | } | 
| Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 82 | else if (preferred == ContentType::CBOR) | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 83 | { | 
|  | 84 | res.addHeader(boost::beast::http::field::content_type, | 
|  | 85 | "application/cbor"); | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 86 | std::string cbor; | 
|  | 87 | nlohmann::json::to_cbor(res.jsonValue, cbor); | 
|  | 88 | res.write(std::move(cbor)); | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 89 | } | 
|  | 90 | else | 
|  | 91 | { | 
| Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 92 | // Technically preferred could also be NoMatch here, but we'd | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 93 | // 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 Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 97 | res.write(res.jsonValue.dump( | 
|  | 98 | 2, ' ', true, nlohmann::json::error_handler_t::replace)); | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 99 | } | 
|  | 100 | } | 
| Ed Tanous | b253906 | 2024-03-12 16:58:35 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | handleEncoding(acceptEncoding, res); | 
| Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 103 | } | 
|  | 104 | } // namespace crow |