Kea  2.1.7-git
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 
11 using namespace isc::data;
12 
13 namespace isc {
14 namespace http {
15 
16 PostHttpRequestJson::PostHttpRequestJson()
17  : PostHttpRequest(), json_() {
18  requireHeaderValue("Content-Type", "application/json");
19 }
20 
21 PostHttpRequestJson::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 
31 void
33  if (!created_) {
34  create();
35  }
36 
37  // Parse JSON body and store.
39  finalized_ = true;
40 }
41 
42 void
45  json_.reset();
46 }
47 
51  return (json_);
52 }
53 
54 void
56  if (body) {
57  context_->body_ = body->str();
58  json_ = body;
59 
60  } else {
61  context_->body_.clear();
62  }
63 }
64 
66 PostHttpRequestJson::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 
84 void
86  try {
87  // Only parse the body if it hasn't been parsed yet.
88  if (!json_ && !context_->body_.empty()) {
89  ElementPtr json = Element::fromJSON(context_->body_);
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
Represents HTTP Host header.
Definition: http_header.h:68
std::string remote_
Remote address.
Definition: request.h:291
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
bool finalized_
Flag indicating whether finalize was called.
Definition: http_message.h:256
virtual void finalize()
Complete parsing of the HTTP request.
HTTP protocol version.
Definition: http_types.h:14
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
HttpRequestContextPtr context_
Pointer to the HttpRequestContext holding parsed data.
Definition: request.h:288
Represents HTTP POST request.
Definition: post_request.h:29
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
Exception thrown when body of the HTTP message is not JSON.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool created_
Flag indicating whether create was called.
Definition: http_message.h:253
const HttpRequestContextPtr & context() const
Returns pointer to the HttpRequestContext.
Definition: request.h:98
virtual void reset()
Reset the state of the object.
PostHttpRequestJson()
Constructor for inbound HTTP request.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
int version()
returns Kea hooks version.
Method
HTTP methods.
Definition: request.h:61
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
Defines the logger used by the top-level component of kea-lfc.
void setBodyAsJson(const data::ConstElementPtr &body)
Sets JSON body for an outbound message.
data::ConstElementPtr json_
Pointer to the parsed JSON body.
virtual void create()
Commits information held in the context into the request.
Definition: request.cc:68
boost::shared_ptr< BasicHttpAuth > BasicHttpAuthPtr
Type of pointers to basic HTTP authentication objects.
Definition: basic_auth.h:70
HTTP header context.
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects. ...
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
Definition: http_message.cc:37
virtual void reset()
Reset the state of the object.
Definition: request.cc:147