177dd8813SKowalski, Kamil /* 277dd8813SKowalski, Kamil // Copyright (c) 2018 Intel Corporation 377dd8813SKowalski, Kamil // 477dd8813SKowalski, Kamil // Licensed under the Apache License, Version 2.0 (the "License"); 577dd8813SKowalski, Kamil // you may not use this file except in compliance with the License. 677dd8813SKowalski, Kamil // You may obtain a copy of the License at 777dd8813SKowalski, Kamil // 877dd8813SKowalski, Kamil // http://www.apache.org/licenses/LICENSE-2.0 977dd8813SKowalski, Kamil // 1077dd8813SKowalski, Kamil // Unless required by applicable law or agreed to in writing, software 1177dd8813SKowalski, Kamil // distributed under the License is distributed on an "AS IS" BASIS, 1277dd8813SKowalski, Kamil // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1377dd8813SKowalski, Kamil // See the License for the specific language governing permissions and 1477dd8813SKowalski, Kamil // limitations under the License. 1577dd8813SKowalski, Kamil */ 1677dd8813SKowalski, Kamil #include "utils/json_utils.hpp" 171abe55efSEd Tanous 181aa0c2b8SEd Tanous #include "error_messages.hpp" 191aa0c2b8SEd Tanous #include "http/http_request.hpp" 201aa0c2b8SEd Tanous #include "http/http_response.hpp" 211aa0c2b8SEd Tanous #include "http/parsing.hpp" 221aa0c2b8SEd Tanous 231aa0c2b8SEd Tanous #include <nlohmann/json.hpp> 241aa0c2b8SEd Tanous 251abe55efSEd Tanous namespace redfish 261abe55efSEd Tanous { 2777dd8813SKowalski, Kamil 281abe55efSEd Tanous namespace json_util 291abe55efSEd Tanous { 3077dd8813SKowalski, Kamil 3155c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 321abe55efSEd Tanous nlohmann::json& reqJson) 331abe55efSEd Tanous { 341aa0c2b8SEd Tanous JsonParseResult ret = parseRequestAsJson(req, reqJson); 351aa0c2b8SEd Tanous if (ret == JsonParseResult::BadContentType) 361aa0c2b8SEd Tanous { 371aa0c2b8SEd Tanous messages::unrecognizedRequestBody(res); 381aa0c2b8SEd Tanous return false; 391aa0c2b8SEd Tanous } 4033c6b580SEd Tanous reqJson = nlohmann::json::parse(req.body(), nullptr, false); 4177dd8813SKowalski, Kamil 421abe55efSEd Tanous if (reqJson.is_discarded()) 431abe55efSEd Tanous { 44f12894f8SJason M. Bills messages::malformedJSON(res); 4577dd8813SKowalski, Kamil return false; 4677dd8813SKowalski, Kamil } 4777dd8813SKowalski, Kamil 4877dd8813SKowalski, Kamil return true; 4977dd8813SKowalski, Kamil } 5077dd8813SKowalski, Kamil 51*8a7c4b47SNan Zhou uint64_t getEstimatedJsonSize(const nlohmann::json& root) 52*8a7c4b47SNan Zhou { 53*8a7c4b47SNan Zhou if (root.is_null()) 54*8a7c4b47SNan Zhou { 55*8a7c4b47SNan Zhou return 4; 56*8a7c4b47SNan Zhou } 57*8a7c4b47SNan Zhou if (root.is_number()) 58*8a7c4b47SNan Zhou { 59*8a7c4b47SNan Zhou return 8; 60*8a7c4b47SNan Zhou } 61*8a7c4b47SNan Zhou if (root.is_boolean()) 62*8a7c4b47SNan Zhou { 63*8a7c4b47SNan Zhou return 5; 64*8a7c4b47SNan Zhou } 65*8a7c4b47SNan Zhou if (root.is_string()) 66*8a7c4b47SNan Zhou { 67*8a7c4b47SNan Zhou constexpr uint64_t quotesSize = 2; 68*8a7c4b47SNan Zhou return root.get<std::string>().size() + quotesSize; 69*8a7c4b47SNan Zhou } 70*8a7c4b47SNan Zhou if (root.is_binary()) 71*8a7c4b47SNan Zhou { 72*8a7c4b47SNan Zhou return root.get_binary().size(); 73*8a7c4b47SNan Zhou } 74*8a7c4b47SNan Zhou const nlohmann::json::array_t* arr = 75*8a7c4b47SNan Zhou root.get_ptr<const nlohmann::json::array_t*>(); 76*8a7c4b47SNan Zhou if (arr != nullptr) 77*8a7c4b47SNan Zhou { 78*8a7c4b47SNan Zhou uint64_t sum = 0; 79*8a7c4b47SNan Zhou for (const auto& element : *arr) 80*8a7c4b47SNan Zhou { 81*8a7c4b47SNan Zhou sum += getEstimatedJsonSize(element); 82*8a7c4b47SNan Zhou } 83*8a7c4b47SNan Zhou return sum; 84*8a7c4b47SNan Zhou } 85*8a7c4b47SNan Zhou const nlohmann::json::object_t* object = 86*8a7c4b47SNan Zhou root.get_ptr<const nlohmann::json::object_t*>(); 87*8a7c4b47SNan Zhou if (object != nullptr) 88*8a7c4b47SNan Zhou { 89*8a7c4b47SNan Zhou uint64_t sum = 0; 90*8a7c4b47SNan Zhou for (const auto& [k, v] : root.items()) 91*8a7c4b47SNan Zhou { 92*8a7c4b47SNan Zhou constexpr uint64_t colonQuoteSpaceSize = 4; 93*8a7c4b47SNan Zhou sum += k.size() + getEstimatedJsonSize(v) + colonQuoteSpaceSize; 94*8a7c4b47SNan Zhou } 95*8a7c4b47SNan Zhou return sum; 96*8a7c4b47SNan Zhou } 97*8a7c4b47SNan Zhou return 0; 98*8a7c4b47SNan Zhou } 99*8a7c4b47SNan Zhou 10077dd8813SKowalski, Kamil } // namespace json_util 10177dd8813SKowalski, Kamil } // namespace redfish 102