Kea  1.9.9-git
post_request_json.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2020 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  json_ = Element::fromJSON(context_->body_);
90  }
91  } catch (const std::exception& ex) {
92  isc_throw(HttpRequestJsonError, "unable to parse the body of the HTTP"
93  " request: " << ex.what());
94  }
95 }
96 
97 } // namespace http
98 } // namespace isc
Represents HTTP Host header.
Definition: http_header.h:68
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
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
HttpRequestContextPtr context_
Pointer to the HttpRequestContext holding parsed data.
Definition: request.h:181
Represents HTTP POST request.
Definition: post_request.h:29
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
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
virtual void reset()
Reset the state of the object.
PostHttpRequestJson()
Constructor for inbound HTTP request.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
int version()
returns Kea hooks version.
Method
HTTP methods.
Definition: request.h:53
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
void setBodyAsJson(const data::ConstElementPtr &body)
Sets JSON body for an outbound message.
data::ConstElementPtr json_
Pointer to the parsed JSON body.
const HttpRequestContextPtr & context() const
Returns pointer to the HttpRequestContext.
Definition: request.h:90
virtual void create()
Commits information held in the context into the request.
Definition: request.cc:58
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:137