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
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 agent {
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
38CtrlAgentResponseCreator::
39createStockHttpResponseInternal(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
59CtrlAgentResponseCreator::
60createDynamicHttpResponse(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.
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
static CtrlAgentCommandMgr & instance()
Returns sole instance of the Command Manager.
virtual http::HttpResponsePtr createStockHttpResponse(const http::ConstHttpRequestPtr &request, const http::HttpStatusCode &status_code) const
Creates stock HTTP response.
virtual http::HttpRequestPtr createNewHttpRequest() const
Create a new request.
isc::data::ConstElementPtr processCommand(const isc::data::ConstElementPtr &cmd)
Triggers command processing.
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:23
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
Definition: response_json.h:27
boost::shared_ptr< const PostHttpRequestJson > ConstPostHttpRequestJsonPtr
Pointer to const PostHttpRequestJson.
boost::shared_ptr< const HttpRequest > ConstHttpRequestPtr
Pointer to the const HttpRequest object.
Definition: request.h:31
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:81
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition: request.h:28
Defines the logger used by the top-level component of kea-dhcp-ddns.
HTTP protocol version.
Definition: http_types.h:14