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 | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "bmcweb_config.h" |
| 6 | |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 7 | #include "http/http_request.hpp" |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 8 | #include "http_utility.hpp" |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 9 | #include "logging.hpp" |
| 10 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include <boost/beast/http/field.hpp> |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> |
| 13 | |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 14 | #include <cctype> |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 15 | #include <string_view> |
| 16 | |
| 17 | enum class JsonParseResult |
| 18 | { |
| 19 | BadContentType, |
| 20 | BadJsonData, |
| 21 | Success, |
| 22 | }; |
| 23 | |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 24 | inline bool isJsonContentType(std::string_view contentType) |
| 25 | { |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 26 | return http_helpers::getContentType(contentType) == |
| 27 | http_helpers::ContentType::JSON; |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 30 | inline JsonParseResult parseRequestAsJson(const crow::Request& req, |
| 31 | nlohmann::json& jsonOut) |
| 32 | { |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 33 | if (!isJsonContentType( |
| 34 | req.getHeaderValue(boost::beast::http::field::content_type))) |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 35 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 36 | BMCWEB_LOG_WARNING("Failed to parse content type on request"); |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 37 | if constexpr (!BMCWEB_INSECURE_IGNORE_CONTENT_TYPE) |
| 38 | { |
| 39 | return JsonParseResult::BadContentType; |
| 40 | } |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 41 | } |
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 42 | jsonOut = nlohmann::json::parse(req.body(), nullptr, false); |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 43 | if (jsonOut.is_discarded()) |
| 44 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 45 | BMCWEB_LOG_WARNING("Failed to parse json in request"); |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 46 | return JsonParseResult::BadJsonData; |
| 47 | } |
| 48 | |
| 49 | return JsonParseResult::Success; |
| 50 | } |