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