1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation 477dd8813SKowalski, Kamil #include "utils/json_utils.hpp" 51abe55efSEd Tanous 61aa0c2b8SEd Tanous #include "error_messages.hpp" 71aa0c2b8SEd Tanous #include "http/http_request.hpp" 81aa0c2b8SEd Tanous #include "http/http_response.hpp" 91aa0c2b8SEd Tanous #include "http/parsing.hpp" 101aa0c2b8SEd Tanous 111aa0c2b8SEd Tanous #include <nlohmann/json.hpp> 121aa0c2b8SEd Tanous 13f0b59af4SEd Tanous #include <cstdint> 14f0b59af4SEd Tanous #include <string> 15f0b59af4SEd Tanous 161abe55efSEd Tanous namespace redfish 171abe55efSEd Tanous { 1877dd8813SKowalski, Kamil 191abe55efSEd Tanous namespace json_util 201abe55efSEd Tanous { 2177dd8813SKowalski, Kamil 2255c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 231abe55efSEd Tanous nlohmann::json& reqJson) 241abe55efSEd Tanous { 251aa0c2b8SEd Tanous JsonParseResult ret = parseRequestAsJson(req, reqJson); 261aa0c2b8SEd Tanous if (ret == JsonParseResult::BadContentType) 271aa0c2b8SEd Tanous { 281aa0c2b8SEd Tanous messages::unrecognizedRequestBody(res); 291aa0c2b8SEd Tanous return false; 301aa0c2b8SEd Tanous } 3133c6b580SEd Tanous reqJson = nlohmann::json::parse(req.body(), nullptr, false); 3277dd8813SKowalski, Kamil 331abe55efSEd Tanous if (reqJson.is_discarded()) 341abe55efSEd Tanous { 35f12894f8SJason M. Bills messages::malformedJSON(res); 3677dd8813SKowalski, Kamil return false; 3777dd8813SKowalski, Kamil } 3877dd8813SKowalski, Kamil 3977dd8813SKowalski, Kamil return true; 4077dd8813SKowalski, Kamil } 4177dd8813SKowalski, Kamil 428a7c4b47SNan Zhou uint64_t getEstimatedJsonSize(const nlohmann::json& root) 438a7c4b47SNan Zhou { 448a7c4b47SNan Zhou if (root.is_null()) 458a7c4b47SNan Zhou { 468a7c4b47SNan Zhou return 4; 478a7c4b47SNan Zhou } 488a7c4b47SNan Zhou if (root.is_number()) 498a7c4b47SNan Zhou { 508a7c4b47SNan Zhou return 8; 518a7c4b47SNan Zhou } 528a7c4b47SNan Zhou if (root.is_boolean()) 538a7c4b47SNan Zhou { 548a7c4b47SNan Zhou return 5; 558a7c4b47SNan Zhou } 568a7c4b47SNan Zhou if (root.is_string()) 578a7c4b47SNan Zhou { 588a7c4b47SNan Zhou constexpr uint64_t quotesSize = 2; 598a7c4b47SNan Zhou return root.get<std::string>().size() + quotesSize; 608a7c4b47SNan Zhou } 618a7c4b47SNan Zhou if (root.is_binary()) 628a7c4b47SNan Zhou { 638a7c4b47SNan Zhou return root.get_binary().size(); 648a7c4b47SNan Zhou } 658a7c4b47SNan Zhou const nlohmann::json::array_t* arr = 668a7c4b47SNan Zhou root.get_ptr<const nlohmann::json::array_t*>(); 678a7c4b47SNan Zhou if (arr != nullptr) 688a7c4b47SNan Zhou { 698a7c4b47SNan Zhou uint64_t sum = 0; 708a7c4b47SNan Zhou for (const auto& element : *arr) 718a7c4b47SNan Zhou { 728a7c4b47SNan Zhou sum += getEstimatedJsonSize(element); 738a7c4b47SNan Zhou } 748a7c4b47SNan Zhou return sum; 758a7c4b47SNan Zhou } 768a7c4b47SNan Zhou const nlohmann::json::object_t* object = 778a7c4b47SNan Zhou root.get_ptr<const nlohmann::json::object_t*>(); 788a7c4b47SNan Zhou if (object != nullptr) 798a7c4b47SNan Zhou { 808a7c4b47SNan Zhou uint64_t sum = 0; 810bdda665SEd Tanous for (const auto& [k, v] : *object) 828a7c4b47SNan Zhou { 838a7c4b47SNan Zhou constexpr uint64_t colonQuoteSpaceSize = 4; 848a7c4b47SNan Zhou sum += k.size() + getEstimatedJsonSize(v) + colonQuoteSpaceSize; 858a7c4b47SNan Zhou } 868a7c4b47SNan Zhou return sum; 878a7c4b47SNan Zhou } 888a7c4b47SNan Zhou return 0; 898a7c4b47SNan Zhou } 908a7c4b47SNan Zhou 9177dd8813SKowalski, Kamil } // namespace json_util 9277dd8813SKowalski, Kamil } // namespace redfish 93