From f5f56e1a1c4d9e9496fcb9d81131066a964ccd23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 14:15:43 +0200 Subject: Adding upstream version 2.4.1. Signed-off-by: Daniel Baumann --- src/lib/http/post_request_json.cc | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/lib/http/post_request_json.cc (limited to 'src/lib/http/post_request_json.cc') diff --git a/src/lib/http/post_request_json.cc b/src/lib/http/post_request_json.cc new file mode 100644 index 0000000..909a901 --- /dev/null +++ b/src/lib/http/post_request_json.cc @@ -0,0 +1,102 @@ +// Copyright (C) 2016-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include + +using namespace isc::data; + +namespace isc { +namespace http { + +PostHttpRequestJson::PostHttpRequestJson() + : PostHttpRequest(), json_() { + requireHeaderValue("Content-Type", "application/json"); +} + +PostHttpRequestJson::PostHttpRequestJson(const Method& method, const std::string& uri, + const HttpVersion& version, + const HostHttpHeader& host_header, + const BasicHttpAuthPtr& basic_auth) + : PostHttpRequest(method, uri, version, host_header, basic_auth) { + requireHeaderValue("Content-Type", "application/json"); + context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json")); +} + + +void +PostHttpRequestJson::finalize() { + if (!created_) { + create(); + } + + // Parse JSON body and store. + parseBodyAsJson(); + finalized_ = true; +} + +void +PostHttpRequestJson::reset() { + PostHttpRequest::reset(); + json_.reset(); +} + +ConstElementPtr +PostHttpRequestJson::getBodyAsJson() const { + checkFinalized(); + return (json_); +} + +void +PostHttpRequestJson::setBodyAsJson(const data::ConstElementPtr& body) { + if (body) { + context_->body_ = body->str(); + json_ = body; + + } else { + context_->body_.clear(); + } +} + +ConstElementPtr +PostHttpRequestJson::getJsonElement(const std::string& element_name) const { + try { + ConstElementPtr body = getBodyAsJson(); + if (body) { + const std::map& map_value = body->mapValue(); + auto map_element = map_value.find(element_name); + if (map_element != map_value.end()) { + return (map_element->second); + } + } + + } catch (const std::exception& ex) { + isc_throw(HttpRequestJsonError, "unable to get JSON element " + << element_name << ": " << ex.what()); + } + return (ConstElementPtr()); +} + +void +PostHttpRequestJson::parseBodyAsJson() { + try { + // Only parse the body if it hasn't been parsed yet. + if (!json_ && !context_->body_.empty()) { + ElementPtr json = Element::fromJSON(context_->body_); + if (!remote_.empty() && (json->getType() == Element::map)) { + json->set("remote-address", Element::create(remote_)); + } + json_ = json; + } + } catch (const std::exception& ex) { + isc_throw(HttpRequestJsonError, "unable to parse the body of the HTTP" + " request: " << ex.what()); + } +} + +} // namespace http +} // namespace isc -- cgit v1.2.3