Kea 3.3.3
response.h
Go to the documentation of this file.
1// Copyright (C) 2016-2026 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
15#include <boost/lexical_cast.hpp>
16#include <boost/shared_ptr.hpp>
17#include <cstdint>
18#include <string>
19
20namespace isc {
21namespace http {
22
25public:
26 HttpResponseError(const char* file, size_t line, const char* what) :
27 HttpMessageError(file, line, what) { }
28};
29
50
54
59 explicit CallSetGenericBody(const bool set)
60 : set_(set) {
61 }
62
64 static const CallSetGenericBody& yes() {
65 static CallSetGenericBody yes(true);
66 return (yes);
67 }
68
70 static const CallSetGenericBody& no() {
71 static CallSetGenericBody no(false);
72 return (no);
73 }
74
76 bool set_;
77};
78
79class HttpResponse;
80
82typedef boost::shared_ptr<HttpResponse> HttpResponsePtr;
83
85typedef boost::shared_ptr<const HttpResponse> ConstHttpResponsePtr;
86
99class HttpResponse : public HttpMessage {
100public:
101
103 explicit HttpResponse();
104
105
118 explicit HttpResponse(const HttpVersion& version,
119 const HttpStatusCode& status_code,
120 const CallSetGenericBody& generic_body =
122
131 return (context_);
132 }
133
141 virtual void create();
142
148 virtual void finalize();
149
151 virtual void reset();
152
157 void resetContentLength();
158
161
163 std::string getStatusPhrase() const;
164
166 virtual std::string getBody() const;
167
177 data::ConstElementPtr getJsonElement(const std::string& element_name) const;
178
183 static bool isClientError(const HttpStatusCode& status_code);
184
189 static bool isServerError(const HttpStatusCode& status_code);
190
195 static uint16_t statusCodeToNumber(const HttpStatusCode& status_code);
196
201 static std::string statusCodeToString(const HttpStatusCode& status_code);
202
204 std::string toBriefString() const;
205
210 virtual std::string toString() const;
211
218 virtual std::string getDateHeaderValue() const;
219
220private:
221
242 void setGenericBody(const HttpStatusCode& /*status_code*/) { }
243
244protected:
245
249};
250
251} // namespace http
252} // namespace isc
253
254#endif
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HttpMessageError(const char *file, size_t line, const char *what)
HttpMessage(const Direction &direction)
Constructor.
HttpResponseError(const char *file, size_t line, const char *what)
Definition response.h:26
Represents HTTP response message.
Definition response.h:99
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition response.h:248
virtual std::string getBody() const
Returns HTTP response body as string.
Definition response.cc:170
HttpResponse()
Constructor for the inbound HTTP response.
Definition response.cc:49
void resetContentLength()
Reset the Content-Length header.
Definition response.cc:141
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition response.cc:201
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition response.cc:176
HttpStatusCode getStatusCode() const
Returns HTTP status code.
Definition response.cc:158
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
Definition response.cc:206
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition response.cc:183
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
Definition response.cc:213
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:225
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition response.h:130
std::string getStatusPhrase() const
Returns HTTP status phrase.
Definition response.cc:164
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:190
int version()
returns Kea hooks version.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
HttpStatusCode
HTTP status codes (cf RFC 2068).
Definition response.h:31
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition response.h:85
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition response.h:82
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:53
CallSetGenericBody(const bool set)
Constructor.
Definition response.h:59
bool set_
A storage for the boolean flag.
Definition response.h:76
static const CallSetGenericBody & no()
Returns encapsulated false.
Definition response.h:70
static const CallSetGenericBody & yes()
Returns encapsulated true.
Definition response.h:64
HTTP protocol version.
Definition http_types.h:14