Kea 2.0.2
cmd_response_creator.cc
Go to the documentation of this file.
1// Copyright (C) 2021 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#include <config/command_mgr.h>
11#include <cc/data.h>
13#include <http/response_json.h>
14#include <boost/pointer_cast.hpp>
15#include <iostream>
16
17using namespace isc::data;
18using namespace isc::http;
19
20namespace isc {
21namespace config {
22
25 return (HttpRequestPtr(new PostHttpRequestJson()));
26}
27
31 const HttpStatusCode& status_code) const {
32 HttpResponsePtr response = createStockHttpResponseInternal(request, status_code);
33 response->finalize();
34 return (response);
35}
36
38CmdResponseCreator::
39createStockHttpResponseInternal(const HttpRequestPtr& 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
59CmdResponseCreator::
60createDynamicHttpResponse(HttpRequestPtr request) {
61 HttpResponseJsonPtr http_response;
62
64 const HttpAuthConfigPtr& auth_config = getHttpAuthConfig();
65 if (auth_config) {
66 http_response = auth_config->checkAuth(*this, request);
67 if (http_response) {
68 return (http_response);
69 }
70 }
71
72 // The request is always non-null, because this is verified by the
73 // createHttpResponse method. Let's try to convert it to the
74 // PostHttpRequestJson type as this is the type generated by the
75 // createNewHttpRequest. If the conversion result is null it means that
76 // the caller did not use createNewHttpRequest method to create this
77 // instance. This is considered an error in the server logic.
78 PostHttpRequestJsonPtr request_json = boost::dynamic_pointer_cast<
79 PostHttpRequestJson>(request);
80 if (!request_json) {
81 // Notify the client that we have a problem with our server.
82 return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
83 }
84
85 // We have already checked that the request is finalized so the call
86 // to getBodyAsJson must not trigger an exception.
87 ConstElementPtr command = request_json->getBodyAsJson();
88
89 // Process command doesn't generate exceptions but can possibly return
90 // null response, if the handler is not implemented properly. This is
91 // again an internal server issue.
93
94 if (!response) {
95 // Notify the client that we have a problem with our server.
96 return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
97 }
98
99 // Normal Responses coming from the Kea Control Agent must always be wrapped in
100 // a list as they may contain responses from multiple daemons.
101 // If we're emulating that for backward compatibility, then we need to wrap
102 // the answer in a list if it isn't in one already.
103 if (emulateAgentResponse() && (response->getType() != Element::list)) {
104 ElementPtr response_list = Element::createList();
105 response_list->add(boost::const_pointer_cast<Element>(response));
106 response = response_list;
107 }
108
109 // The response is OK, so let's create new HTTP response with the status OK.
110 http_response = boost::dynamic_pointer_cast<
111 HttpResponseJson>(createStockHttpResponseInternal(request, HttpStatusCode::OK));
112 http_response->setBodyAsJson(response);
113 http_response->finalize();
114
115 return (http_response);
116}
117
118} // end of namespace isc::config
119} // end of namespace isc
virtual isc::data::ConstElementPtr processCommand(const isc::data::ConstElementPtr &cmd)
Triggers command processing.
const http::HttpAuthConfigPtr & getHttpAuthConfig()
Fetches the current authentication configuration.
virtual http::HttpResponsePtr createStockHttpResponse(const http::HttpRequestPtr &request, const http::HttpStatusCode &status_code) const
Creates stock HTTP response.
virtual http::HttpRequestPtr createNewHttpRequest() const
Create a new request.
bool emulateAgentResponse()
Indicates whether or not agent response emulation is enabled.
static CommandMgr & instance()
CommandMgr is a singleton class.
Definition: command_mgr.cc:651
Represents HTTP response with JSON content.
Definition: response_json.h:34
void setBodyAsJson(const data::ConstElementPtr &json_body)
Generates JSON content from the data structures represented as data::ConstElementPtr.
Represents HTTP POST request with JSON body.
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30
boost::shared_ptr< PostHttpRequestJson > PostHttpRequestJsonPtr
Pointer to PostHttpRequestJson.
boost::shared_ptr< HttpAuthConfig > HttpAuthConfigPtr
Type of shared pointers to HTTP authentication configuration.
Definition: auth_config.h:79
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
Definition: response_json.h:24
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:27
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition: http_types.h:14