Kea 2.7.6
post_request_json.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2022 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10
11using namespace isc::data;
12
13namespace isc {
14namespace http {
15
17 : PostHttpRequest(), json_() {
18 requireHeaderValue("Content-Type", "application/json");
19}
20
21PostHttpRequestJson::PostHttpRequestJson(const Method& method, const std::string& uri,
22 const HttpVersion& version,
23 const HostHttpHeader& host_header,
24 const BasicHttpAuthPtr& basic_auth)
25 : PostHttpRequest(method, uri, version, host_header, basic_auth) {
26 requireHeaderValue("Content-Type", "application/json");
27 context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
28}
29
30
31void
33 if (!created_) {
34 create();
35 }
36
37 // Parse JSON body and store.
39 finalized_ = true;
40}
41
42void
47
53
54void
56 if (body) {
57 context_->body_ = body->str();
58 json_ = body;
59
60 } else {
61 context_->body_.clear();
62 }
63}
64
66PostHttpRequestJson::getJsonElement(const std::string& element_name) const {
67 try {
69 if (body) {
70 const std::map<std::string, ConstElementPtr>& map_value = body->mapValue();
71 auto map_element = map_value.find(element_name);
72 if (map_element != map_value.end()) {
73 return (map_element->second);
74 }
75 }
76
77 } catch (const std::exception& ex) {
78 isc_throw(HttpRequestJsonError, "unable to get JSON element "
79 << element_name << ": " << ex.what());
80 }
81 return (ConstElementPtr());
82}
83
84void
86 try {
87 // Only parse the body if it hasn't been parsed yet.
88 if (!json_ && !context_->body_.empty()) {
90 if (!remote_.empty() && (json->getType() == Element::map)) {
91 json->set("remote-address", Element::create(remote_));
92 }
93 json_ = json;
94 }
95 } catch (const std::exception& ex) {
96 isc_throw(HttpRequestJsonError, "unable to parse the body of the HTTP"
97 " request: " << ex.what());
98 }
99}
100
101} // namespace http
102} // namespace isc
int version()
returns Kea hooks version.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition data.cc:798
Represents HTTP Host header.
Definition http_header.h:68
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
void checkFinalized() const
Checks if the finalize was called.
bool created_
Flag indicating whether create was called.
virtual void reset()=0
Reset the state of the object.
bool finalized_
Flag indicating whether finalize was called.
Exception thrown when body of the HTTP message is not JSON.
Method
HTTP methods.
Definition request.h:61
HttpRequestContextPtr context_
Pointer to the HttpRequestContext holding parsed data.
Definition request.h:288
virtual void create()
Commits information held in the context into the request.
Definition request.cc:68
const HttpRequestContextPtr & context() const
Returns pointer to the HttpRequestContext.
Definition request.h:98
std::string remote_
Remote address.
Definition request.h:291
virtual void finalize()
Complete parsing of the HTTP request.
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
void setBodyAsJson(const data::ConstElementPtr &body)
Sets JSON body for an outbound message.
PostHttpRequestJson()
Constructor for inbound HTTP request.
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
virtual void reset()
Reset the state of the object.
data::ConstElementPtr json_
Pointer to the parsed JSON body.
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects.
Represents HTTP POST request.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< BasicHttpAuth > BasicHttpAuthPtr
Type of pointers to basic HTTP authentication objects.
Definition basic_auth.h:70
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition http_types.h:14