Kea: http_message.h Source File

| Kea 2.2.0

http_message.h
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#ifndef HTTP_MESSAGE_H
8#define HTTP_MESSAGE_H
9
10#include <exceptions/exceptions.h>
11#include <http/http_header.h>
12#include <http/http_types.h>
13#include <map>
14#include <set>
15#include <cstdint>
16#include <string>
17
18namespace isc {
19namespace http {
20
22 class HttpMessageError : public Exception {
23public:
24 HttpMessageError(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) { };
26};
27
30 class HttpMessageNonExistingHeader : public HttpMessageError {
31public:
32 HttpMessageNonExistingHeader(const char* file, size_t line,
33 const char* what) :
34 HttpMessageError(file, line, what) { };
35};
36
37
62 class HttpMessage {
63public:
64
66 enum Direction {
67 INBOUND,
68 OUTBOUND
69 };
70
74 explicit HttpMessage(const Direction& direction);
75
77 virtual ~HttpMessage();
78
80 Direction getDirection() const {
81 return (direction_);
82 }
83
89 void setDirection(const Direction& direction) {
90 direction_ = direction;
91 }
92
99 void requireHttpVersion(const HttpVersion& version);
100
108 void requireHeader(const std::string& header_name);
109
118 void requireHeaderValue(const std::string& header_name,
119 const std::string& header_value);
120
127 bool requiresBody() const;
128
134 virtual void create() = 0;
135
147 virtual void finalize() = 0;
148
150 virtual void reset() = 0;
151
153 HttpVersion getHttpVersion() const;
154
163 HttpHeaderPtr getHeader(const std::string& header_name) const;
164
170 std::string getHeaderValue(const std::string& header_name) const;
171
178 uint64_t getHeaderValueAsUint64(const std::string& header_name) const;
179
181 virtual std::string getBody() const = 0;
182
187 virtual std::string toString() const = 0;
188
194 bool isFinalized() const {
195 return (finalized_);
196 }
197
198protected:
199
203 void checkCreated() const;
204
208 void checkFinalized() const;
209
223 template<typename T>
224 bool inRequiredSet(const T& element,
225 const std::set<T>& element_set) const {
226 return (element_set.empty() || element_set.count(element) > 0);
227 }
228
230 Direction direction_;
231
235 std::set<HttpVersion> required_versions_;
236
238 HttpVersion http_version_;
239
241 typedef std::map<std::string, HttpHeaderPtr> HttpHeaderMap;
242
250 HttpHeaderMap required_headers_;
251
253 bool created_;
254
256 bool finalized_;
257
259 HttpHeaderMap headers_;
260};
261
262} // end of namespace isc::http
263} // end of namespace isc
264
265#endif // HTTP_MESSAGE_H
version
int version()
returns Kea hooks version.
Definition: bootp/version.cc:13
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::http::HttpMessageError
Generic exception thrown by HttpMessage class.
Definition: http_message.h:22
isc::http::HttpMessageError::HttpMessageError
HttpMessageError(const char *file, size_t line, const char *what)
Definition: http_message.h:24
isc::http::HttpMessageNonExistingHeader
Exception thrown when attempt is made to retrieve a non-existing header.
Definition: http_message.h:30
isc::http::HttpMessageNonExistingHeader::HttpMessageNonExistingHeader
HttpMessageNonExistingHeader(const char *file, size_t line, const char *what)
Definition: http_message.h:32
isc::http::HttpMessage
Base class for HttpRequest and HttpResponse.
Definition: http_message.h:62
isc::http::HttpMessage::direction_
Direction direction_
Message direction (inbound or outbound).
Definition: http_message.h:230
isc::http::HttpMessage::headers_
HttpHeaderMap headers_
Parsed HTTP headers.
Definition: http_message.h:259
isc::http::HttpMessage::toString
virtual std::string toString() const =0
Returns HTTP message as text.
isc::http::HttpMessage::requireHeaderValue
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
Definition: http_message.cc:37
isc::http::HttpMessage::requiresBody
bool requiresBody() const
Checks if the body is required for the HTTP message.
Definition: http_message.cc:44
isc::http::HttpMessage::http_version_
HttpVersion http_version_
HTTP version numbers.
Definition: http_message.h:238
isc::http::HttpMessage::getHttpVersion
HttpVersion getHttpVersion() const
Returns HTTP version number (major and minor).
Definition: http_message.cc:54
isc::http::HttpMessage::getHeaderValueAsUint64
uint64_t getHeaderValueAsUint64(const std::string &header_name) const
Returns a value of the specified HTTP header as number.
Definition: http_message.cc:79
isc::http::HttpMessage::checkFinalized
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
isc::http::HttpMessage::checkCreated
void checkCreated() const
Checks if the create was called.
Definition: http_message.cc:90
isc::http::HttpMessage::create
virtual void create()=0
Reads parsed message from the context, validates the message and stores parsed information.
isc::http::HttpMessage::created_
bool created_
Flag indicating whether create was called.
Definition: http_message.h:253
isc::http::HttpMessage::HttpMessage
HttpMessage(const Direction &direction)
Constructor.
Definition: http_message.cc:14
isc::http::HttpMessage::required_versions_
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
Definition: http_message.h:235
isc::http::HttpMessage::HttpHeaderMap
std::map< std::string, HttpHeaderPtr > HttpHeaderMap
Map of HTTP headers indexed by lower case header names.
Definition: http_message.h:241
isc::http::HttpMessage::~HttpMessage
virtual ~HttpMessage()
Destructor.
Definition: http_message.cc:20
isc::http::HttpMessage::finalize
virtual void finalize()=0
Complete parsing HTTP message or creating an HTTP outbound message.
isc::http::HttpMessage::getDirection
Direction getDirection() const
Returns HTTP message direction.
Definition: http_message.h:80
isc::http::HttpMessage::inRequiredSet
bool inRequiredSet(const T &element, const std::set< T > &element_set) const
Checks if the set is empty or the specified element belongs to this set.
Definition: http_message.h:224
isc::http::HttpMessage::getHeaderValue
std::string getHeaderValue(const std::string &header_name) const
Returns a value of the specified HTTP header.
Definition: http_message.cc:74
isc::http::HttpMessage::isFinalized
bool isFinalized() const
Checks if the message has been successfully finalized.
Definition: http_message.h:194
isc::http::HttpMessage::getHeader
HttpHeaderPtr getHeader(const std::string &header_name) const
Returns object encapsulating HTTP header.
Definition: http_message.cc:60
isc::http::HttpMessage::reset
virtual void reset()=0
Reset the state of the object.
isc::http::HttpMessage::Direction
Direction
Specifies the direction of the HTTP message.
Definition: http_message.h:66
isc::http::HttpMessage::OUTBOUND
@ OUTBOUND
Definition: http_message.h:68
isc::http::HttpMessage::INBOUND
@ INBOUND
Definition: http_message.h:67
isc::http::HttpMessage::requireHeader
void requireHeader(const std::string &header_name)
Specifies a required HTTP header for the HTTP message.
Definition: http_message.cc:29
isc::http::HttpMessage::finalized_
bool finalized_
Flag indicating whether finalize was called.
Definition: http_message.h:256
isc::http::HttpMessage::requireHttpVersion
void requireHttpVersion(const HttpVersion &version)
Specifies HTTP version allowed.
Definition: http_message.cc:24
isc::http::HttpMessage::setDirection
void setDirection(const Direction &direction)
Sets direction for the HTTP message.
Definition: http_message.h:89
isc::http::HttpMessage::required_headers_
HttpHeaderMap required_headers_
Map holding required HTTP headers.
Definition: http_message.h:250
isc::http::HttpMessage::getBody
virtual std::string getBody() const =0
Returns HTTP message body as string.
exceptions.h
http_header.h
http_types.h
isc::http::HttpHeaderPtr
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Definition: http_header.h:65
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
isc::http::HttpVersion
HTTP protocol version.
Definition: http_types.h:14