blob: b68411a205b551d14f2bb813c66f7c31e5d89564 [file] [log] [blame]
#ifndef THIRD_PARTY_GBMCWEB_HTTP_PARSING_H_
#define THIRD_PARTY_GBMCWEB_HTTP_PARSING_H_
#include <cstdint>
#include <string_view>
#include "boost/algorithm/string/predicate.hpp" // NOLINT
#include "http_request.hpp"
#include "logging.hpp"
#include <nlohmann/json.hpp>
enum class JsonParseResult : std::uint8_t {
BadContentType,
BadJsonData,
Success,
};
inline JsonParseResult parseRequestAsJson(const crow::Request& req,
nlohmann::json& jsonOut) {
std::string_view content_type =
req.getHeaderValue(boost::beast::http::field::content_type);
if (!boost::iequals(content_type, "application/json") &&
!boost::iequals(content_type, "application/json; charset=utf-8")) {
BMCWEB_LOG_WARNING << "Failed to parse content type on request";
#ifndef BMCWEB_INSECURE_IGNORE_CONTENT_TYPE
return JsonParseResult::BadContentType;
#endif
}
jsonOut = nlohmann::json::parse(req.body(), nullptr, false);
if (jsonOut.is_discarded()) {
BMCWEB_LOG_WARNING << "Failed to parse json in request";
return JsonParseResult::BadJsonData;
}
return JsonParseResult::Success;
}
#endif // THIRD_PARTY_GBMCWEB_HTTP_PARSING_H_