Kea  1.9.9-git
response.h
Go to the documentation of this file.
1 // Copyright (C) 2016-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 #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>
13 #include <http/response_context.h>
14 #include <boost/lexical_cast.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <string>
17 #include <vector>
18 
19 namespace isc {
20 namespace http {
21 
24 public:
25  HttpResponseError(const char* file, size_t line, const char* what) :
26  HttpMessageError(file, line, what) { };
27 };
28 
30 enum class HttpStatusCode : std::uint16_t {
31  OK = 200,
32  CREATED = 201,
33  ACCEPTED = 202,
34  NO_CONTENT = 204,
35  MULTIPLE_CHOICES = 300,
36  MOVED_PERMANENTLY = 301,
37  MOVED_TEMPORARILY = 302,
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 
79 
81 typedef boost::shared_ptr<HttpResponse> HttpResponsePtr;
82 
84 typedef boost::shared_ptr<const HttpResponse> ConstHttpResponsePtr;
85 
98 class HttpResponse : public HttpMessage {
99 public:
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 std::string statusCodeToString(const HttpStatusCode& status_code);
189 
191  std::string toBriefString() const;
192 
197  virtual std::string toString() const;
198 
199 protected:
200 
207  virtual std::string getDateHeaderValue() const;
208 
213  static uint16_t statusCodeToNumber(const HttpStatusCode& status_code);
214 
215 private:
216 
237  void setGenericBody(const HttpStatusCode& /*status_code*/) { };
238 
239 protected:
240 
244 };
245 
246 } // namespace http
247 } // namespace isc
248 
249 #endif
static const CallSetGenericBody & no()
Returns encapsulated false.
Definition: response.h:69
boost::shared_ptr< const HttpResponse > ConstHttpResponsePtr
Pointer to the const HttpResponse object.
Definition: response.h:84
virtual void finalize()
Completes creation of the HTTP response.
Definition: response.cc:129
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
Generic exception thrown by HttpMessage class.
Definition: http_message.h:22
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition: response.cc:163
std::string getStatusPhrase() const
Returns HTTP status phrase.
Definition: response.cc:151
HTTP protocol version.
Definition: http_types.h:14
virtual std::string toString() const
Returns HTTP response as string.
Definition: response.cc:212
virtual std::string getBody() const
Returns HTTP response body as string.
Definition: response.cc:157
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition: response.h:129
virtual void create()
Commits information held in the context into the response.
Definition: response.cc:69
CallSetGenericBody(const bool set)
Constructor.
Definition: response.h:58
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition: response.h:237
Base class for HttpRequest and HttpResponse.
Definition: http_message.h:62
Represents HTTP response message.
Definition: response.h:98
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
HttpResponseError(const char *file, size_t line, const char *what)
Definition: response.h:25
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
int version()
returns Kea hooks version.
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
Definition: response.cc:193
static const CallSetGenericBody & yes()
Returns encapsulated true.
Definition: response.h:63
Generic exception thrown by HttpResponse class.
Definition: response.h:23
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition: response.cc:170
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HttpStatusCode getStatusCode() const
Returns HTTP status code.
Definition: response.cc:145
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition: response.cc:177
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual void reset()
Reset the state of the object.
Definition: response.cc:138
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
Definition: response.cc:200
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition: response.cc:188
HttpResponse()
Constructor for the inbound HTTP response.
Definition: response.cc:49
boost::shared_ptr< HttpResponseContext > HttpResponseContextPtr
Pointer to the HttpResponseContext.
bool set_
A storage for the boolean flag.
Definition: response.h:75
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
Definition: response.h:52
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30