Kea 3.3.3
http_message.h
Go to the documentation of this file.
1// Copyright (C) 2017-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_MESSAGE_H
8#define HTTP_MESSAGE_H
9
11#include <http/http_header.h>
12#include <http/http_types.h>
13
14#include <cstdint>
15#include <map>
16#include <set>
17#include <string>
18
19namespace isc {
20namespace http {
21
24public:
25 HttpMessageError(const char* file, size_t line, const char* what) :
26 isc::Exception(file, line, what) { }
27};
28
32public:
33 HttpMessageNonExistingHeader(const char* file, size_t line,
34 const char* what) :
35 HttpMessageError(file, line, what) { }
36};
37
38
64public:
65
67 enum Direction : uint16_t {
70 };
71
75 explicit HttpMessage(const Direction& direction);
76
78 virtual ~HttpMessage();
79
82 return (direction_);
83 }
84
90 void setDirection(const Direction& direction) {
91 direction_ = direction;
92 }
93
101
109 void requireHeader(const std::string& header_name);
110
119 void requireHeaderValue(const std::string& header_name,
120 const std::string& header_value);
121
128 bool requiresBody() const;
129
135 virtual void create() = 0;
136
148 virtual void finalize() = 0;
149
151 virtual void reset() = 0;
152
155
164 HttpHeaderPtr getHeader(const std::string& header_name) const;
165
171 std::string getHeaderValue(const std::string& header_name) const;
172
179 uint64_t getHeaderValueAsUint64(const std::string& header_name) const;
180
182 virtual std::string getBody() const = 0;
183
188 virtual std::string toString() const = 0;
189
195 bool isFinalized() const {
196 return (finalized_);
197 }
198
199protected:
200
204 void checkCreated() const;
205
209 void checkFinalized() const;
210
224 template<typename T>
225 bool inRequiredSet(const T& element,
226 const std::set<T>& element_set) const {
227 return (element_set.empty() || element_set.count(element) > 0);
228 }
229
232
236 std::set<HttpVersion> required_versions_;
237
240
242 typedef std::map<std::string, HttpHeaderPtr> HttpHeaderMap;
243
252
255
258
261};
262
263} // end of namespace isc::http
264} // end of namespace isc
265
266#endif // HTTP_MESSAGE_H
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
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)
HttpMessageNonExistingHeader(const char *file, size_t line, const char *what)
Direction direction_
Message direction (inbound or outbound).
HttpHeaderMap headers_
Parsed HTTP headers.
virtual std::string toString() const =0
Returns HTTP message as text.
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
bool requiresBody() const
Checks if the body is required for the HTTP message.
HttpVersion http_version_
HTTP version numbers.
HttpVersion getHttpVersion() const
Returns HTTP version number (major and minor).
uint64_t getHeaderValueAsUint64(const std::string &header_name) const
Returns a value of the specified HTTP header as number.
void checkFinalized() const
Checks if the finalize was called.
void checkCreated() const
Checks if the create was called.
virtual void create()=0
Reads parsed message from the context, validates the message and stores parsed information.
bool created_
Flag indicating whether create was called.
HttpMessage(const Direction &direction)
Constructor.
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
Direction
Specifies the direction of the HTTP message.
std::map< std::string, HttpHeaderPtr > HttpHeaderMap
Map of HTTP headers indexed by lower case header names.
virtual ~HttpMessage()
Destructor.
virtual void finalize()=0
Complete parsing HTTP message or creating an HTTP outbound message.
Direction getDirection() const
Returns HTTP message direction.
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.
std::string getHeaderValue(const std::string &header_name) const
Returns a value of the specified HTTP header.
bool isFinalized() const
Checks if the message has been successfully finalized.
HttpHeaderPtr getHeader(const std::string &header_name) const
Returns object encapsulating HTTP header.
virtual void reset()=0
Reset the state of the object.
void requireHeader(const std::string &header_name)
Specifies a required HTTP header for the HTTP message.
bool finalized_
Flag indicating whether finalize was called.
void requireHttpVersion(const HttpVersion &version)
Specifies HTTP version allowed.
void setDirection(const Direction &direction)
Sets direction for the HTTP message.
HttpHeaderMap required_headers_
Map holding required HTTP headers.
virtual std::string getBody() const =0
Returns HTTP message body as string.
int version()
returns Kea hooks version.
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Definition http_header.h:65
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition http_types.h:14