Kea  2.1.7-git
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>
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 
153  HttpStatusCode getStatusCode() const;
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 
213 private:
214 
235  void setGenericBody(const HttpStatusCode& /*status_code*/) { };
236 
237 protected:
238 
242 };
243 
244 } // namespace http
245 } // namespace isc
246 
247 #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
Generic exception thrown by HttpMessage class.
Definition: http_message.h:22
HTTP protocol version.
Definition: http_types.h:14
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
CallSetGenericBody(const bool set)
Constructor.
Definition: response.h:58
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition: response.h:235
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:27
int version()
returns Kea hooks version.
static const CallSetGenericBody & yes()
Returns encapsulated true.
Definition: response.h:63
Generic exception thrown by HttpResponse class.
Definition: response.h:23
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition: response.h:129
Defines the logger used by the top-level component of kea-lfc.
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