Kea 2.6.0
response.h
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#ifndef HTTP_RESPONSE_H
8#define HTTP_RESPONSE_H
9
10#include <cc/data.h>
11#include <http/header_context.h>
12#include <http/http_message.h>
14#include <boost/lexical_cast.hpp>
15#include <boost/shared_ptr.hpp>
16#include <string>
17#include <vector>
18
19namespace isc {
20namespace http {
21
24public:
25 HttpResponseError(const char* file, size_t line, const char* what) :
26 HttpMessageError(file, line, what) { };
27};
28
30enum class HttpStatusCode : std::uint16_t {
31 OK = 200,
32 CREATED = 201,
33 ACCEPTED = 202,
34 NO_CONTENT = 204,
35 MULTIPLE_CHOICES = 300,
38 NOT_MODIFIED = 304,
39 BAD_REQUEST = 400,
40 UNAUTHORIZED = 401,
41 FORBIDDEN = 403,
42 NOT_FOUND = 404,
43 REQUEST_TIMEOUT = 408,
45 NOT_IMPLEMENTED = 501,
46 BAD_GATEWAY = 502,
48};
49
53
58 explicit CallSetGenericBody(const bool set)
59 : set_(set) {
60 }
61
63 static const CallSetGenericBody& yes() {
64 static CallSetGenericBody yes(true);
65 return (yes);
66 }
67
69 static const CallSetGenericBody& no() {
70 static CallSetGenericBody no(false);
71 return (no);
72 }
73
75 bool set_;
76};
77
78class HttpResponse;
79
81typedef boost::shared_ptr<HttpResponse> HttpResponsePtr;
82
84typedef boost::shared_ptr<const HttpResponse> ConstHttpResponsePtr;
85
98class HttpResponse : public HttpMessage {
99public:
100
102 explicit HttpResponse();
103
104
117 explicit HttpResponse(const HttpVersion& version,
118 const HttpStatusCode& status_code,
119 const CallSetGenericBody& generic_body =
121
130 return (context_);
131 }
132
140 virtual void create();
141
147 virtual void finalize();
148
150 virtual void reset();
151
154
156 std::string getStatusPhrase() const;
157
159 virtual std::string getBody() const;
160
170 data::ConstElementPtr getJsonElement(const std::string& element_name) const;
171
176 static bool isClientError(const HttpStatusCode& status_code);
177
182 static bool isServerError(const HttpStatusCode& status_code);
183
188 static uint16_t statusCodeToNumber(const HttpStatusCode& status_code);
189
194 static std::string statusCodeToString(const HttpStatusCode& status_code);
195
197 std::string toBriefString() const;
198
203 virtual std::string toString() const;
204
211 virtual std::string getDateHeaderValue() const;
212
213private:
214
235 void setGenericBody(const HttpStatusCode& /*status_code*/) { };
236
237protected:
238
242};
243
244} // namespace http
245} // namespace isc
246
247#endif
int version()
returns Kea hooks version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Generic exception thrown by HttpMessage class.
Definition: http_message.h:22
Base class for HttpRequest and HttpResponse.
Definition: http_message.h:62
Generic exception thrown by HttpResponse class.
Definition: response.h:23
HttpResponseError(const char *file, size_t line, const char *what)
Definition: response.h:25
Represents HTTP response message.
Definition: response.h:98
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition: response.h:241
virtual std::string getBody() const
Returns HTTP response body as string.
Definition: response.cc:153
HttpResponse()
Constructor for the inbound HTTP response.
Definition: response.cc:49
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition: response.cc:184
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition: response.cc:159
HttpStatusCode getStatusCode() const
Returns HTTP status code.
Definition: response.cc:141
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
Definition: response.cc:189
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition: response.cc:166
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
Definition: response.cc:196
virtual void finalize()
Completes creation of the HTTP response.
Definition: response.cc:125
virtual std::string toString() const
Returns HTTP response as string.
Definition: response.cc:208
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition: response.h:129
std::string getStatusPhrase() const
Returns HTTP status phrase.
Definition: response.cc:147
virtual void reset()
Reset the state of the object.
Definition: response.cc:134
virtual void create()
Commits information held in the context into the response.
Definition: response.cc:69
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition: response.cc:173
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition: response.h:84
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:81
boost::shared_ptr< HttpResponseContext > HttpResponseContextPtr
Pointer to the HttpResponseContext.
Defines the logger used by the top-level component of kea-lfc.
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
Definition: response.h:52
CallSetGenericBody(const bool set)
Constructor.
Definition: response.h:58
bool set_
A storage for the boolean flag.
Definition: response.h:75
static const CallSetGenericBody & no()
Returns encapsulated false.
Definition: response.h:69
static const CallSetGenericBody & yes()
Returns encapsulated true.
Definition: response.h:63
HTTP protocol version.
Definition: http_types.h:14