Kea  1.5.0
ca_response_creator.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 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 
9 #include <agent/ca_command_mgr.h>
11 #include <cc/data.h>
12 #include <http/post_request_json.h>
13 #include <http/response_json.h>
14 #include <boost/pointer_cast.hpp>
15 #include <iostream>
16 
17 using namespace isc::data;
18 using namespace isc::http;
19 
20 namespace isc {
21 namespace agent {
22 
24 CtrlAgentResponseCreator::createNewHttpRequest() const {
25  return (HttpRequestPtr(new PostHttpRequestJson()));
26 }
27 
29 CtrlAgentResponseCreator::
30 createStockHttpResponse(const ConstHttpRequestPtr& request,
31  const HttpStatusCode& status_code) const {
32  HttpResponsePtr response = createStockHttpResponseInternal(request, status_code);
33  response->finalize();
34  return (response);
35 }
36 
38 CtrlAgentResponseCreator::
39 createStockHttpResponseInternal(const ConstHttpRequestPtr& request,
40  const HttpStatusCode& status_code) const {
41  // The request hasn't been finalized so the request object
42  // doesn't contain any information about the HTTP version number
43  // used. But, the context should have this data (assuming the
44  // HTTP version is parsed ok).
45  HttpVersion http_version(request->context()->http_version_major_,
46  request->context()->http_version_minor_);
47  // We only accept HTTP version 1.0 or 1.1. If other version number is found
48  // we fall back to HTTP/1.0.
49  if ((http_version < HttpVersion(1, 0)) || (HttpVersion(1, 1) < http_version)) {
50  http_version.major_ = 1;
51  http_version.minor_ = 0;
52  }
53  // This will generate the response holding JSON content.
54  HttpResponsePtr response(new HttpResponseJson(http_version, status_code));
55  return (response);
56 }
57 
59 CtrlAgentResponseCreator::
60 createDynamicHttpResponse(const ConstHttpRequestPtr& request) {
61  // The request is always non-null, because this is verified by the
62  // createHttpResponse method. Let's try to convert it to the
63  // ConstPostHttpRequestJson type as this is the type generated by the
64  // createNewHttpRequest. If the conversion result is null it means that
65  // the caller did not use createNewHttpRequest method to create this
66  // instance. This is considered an error in the server logic.
67  ConstPostHttpRequestJsonPtr request_json = boost::dynamic_pointer_cast<
68  const PostHttpRequestJson>(request);
69  if (!request_json) {
70  // Notify the client that we have a problem with our server.
71  return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
72  }
73 
74  // We have already checked that the request is finalized so the call
75  // to getBodyAsJson must not trigger an exception.
76  ConstElementPtr command = request_json->getBodyAsJson();
77 
78  // Process command doesn't generate exceptions but can possibly return
79  // null response, if the handler is not implemented properly. This is
80  // again an internal server issue.
81  ConstElementPtr response = CtrlAgentCommandMgr::instance().processCommand(command);
82  if (!response) {
83  // Notify the client that we have a problem with our server.
84  return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
85  }
86  // The response is ok, so let's create new HTTP response with the status OK.
87  HttpResponseJsonPtr http_response = boost::dynamic_pointer_cast<
88  HttpResponseJson>(createStockHttpResponseInternal(request, HttpStatusCode::OK));
89  http_response->setBodyAsJson(response);
90  http_response->finalize();
91 
92  return (http_response);
93 }
94 
95 
96 
97 } // end of namespace isc::agent
98 } // end of namespace isc
Represents HTTP POST request with JSON body.
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
Definition: response_json.h:24
HTTP protocol version.
Definition: http_types.h:14
unsigned major_
Major HTTP version.
Definition: http_types.h:15
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< const HttpRequest > ConstHttpRequestPtr
Pointer to the const HttpRequest object.
Definition: request.h:31
Represents HTTP response with JSON content.
Definition: response_json.h:34
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:25
boost::shared_ptr< const PostHttpRequestJson > ConstPostHttpRequestJsonPtr
Pointer to const PostHttpRequestJson.
void setBodyAsJson(const data::ConstElementPtr &json_body)
Generates JSON content from the data structures represented as data::ConstElementPtr.
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30