Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "authentication.hpp" |
| 4 | #include "http_request.hpp" |
| 5 | #include "http_response.hpp" |
| 6 | #include "http_utility.hpp" |
| 7 | #include "json_html_serializer.hpp" |
| 8 | #include "logging.hpp" |
| 9 | #include "security_headers.hpp" |
| 10 | #include "utils/hex_utils.hpp" |
| 11 | |
| 12 | #include <boost/beast/http/message.hpp> |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 13 | #include <nlohmann/json.hpp> |
| 14 | |
| 15 | #include <array> |
| 16 | |
| 17 | namespace crow |
| 18 | { |
| 19 | |
| 20 | inline void completeResponseFields(const Request& req, Response& res) |
| 21 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 22 | BMCWEB_LOG_INFO("Response: {} {}", req.url().encoded_path(), |
| 23 | res.resultInt()); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 24 | addSecurityHeaders(req, res); |
| 25 | |
| 26 | authentication::cleanupTempSession(req); |
| 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 | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 34 | ContentType preferred = |
| 35 | getPreferredContentType(req.getHeaderValue("Accept"), allowed); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 36 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 37 | if (preferred == ContentType::HTML) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 38 | { |
| 39 | json_html_util::prettyPrintJson(res); |
| 40 | } |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 41 | else if (preferred == ContentType::CBOR) |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 42 | { |
| 43 | res.addHeader(boost::beast::http::field::content_type, |
| 44 | "application/cbor"); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 45 | std::string cbor; |
| 46 | nlohmann::json::to_cbor(res.jsonValue, cbor); |
| 47 | res.write(std::move(cbor)); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 48 | } |
| 49 | else |
| 50 | { |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 51 | // Technically preferred could also be NoMatch here, but we'd |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 52 | // like to default to something rather than return 400 for |
| 53 | // backward compatibility. |
| 54 | res.addHeader(boost::beast::http::field::content_type, |
| 55 | "application/json"); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 56 | res.write(res.jsonValue.dump( |
| 57 | 2, ' ', true, nlohmann::json::error_handler_t::replace)); |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | } // namespace crow |