Kea 2.7.5
|
Base class for HttpRequest and HttpResponse. More...
#include <http_message.h>
Public Types | |
enum | Direction { INBOUND , OUTBOUND } |
Specifies the direction of the HTTP message. More... | |
Public Member Functions | |
HttpMessage (const Direction &direction) | |
Constructor. | |
virtual | ~HttpMessage () |
Destructor. | |
virtual void | create ()=0 |
Reads parsed message from the context, validates the message and stores parsed information. | |
virtual void | finalize ()=0 |
Complete parsing HTTP message or creating an HTTP outbound message. | |
virtual std::string | getBody () const =0 |
Returns HTTP message body as string. | |
Direction | getDirection () const |
Returns HTTP message direction. | |
HttpHeaderPtr | getHeader (const std::string &header_name) const |
Returns object encapsulating HTTP header. | |
std::string | getHeaderValue (const std::string &header_name) const |
Returns a value of the specified HTTP header. | |
uint64_t | getHeaderValueAsUint64 (const std::string &header_name) const |
Returns a value of the specified HTTP header as number. | |
HttpVersion | getHttpVersion () const |
Returns HTTP version number (major and minor). | |
bool | isFinalized () const |
Checks if the message has been successfully finalized. | |
void | requireHeader (const std::string &header_name) |
Specifies a required HTTP header for the HTTP message. | |
void | requireHeaderValue (const std::string &header_name, const std::string &header_value) |
Specifies a required value of a header in the message. | |
void | requireHttpVersion (const HttpVersion &version) |
Specifies HTTP version allowed. | |
bool | requiresBody () const |
Checks if the body is required for the HTTP message. | |
virtual void | reset ()=0 |
Reset the state of the object. | |
void | setDirection (const Direction &direction) |
Sets direction for the HTTP message. | |
virtual std::string | toString () const =0 |
Returns HTTP message as text. | |
Protected Types | |
typedef std::map< std::string, HttpHeaderPtr > | HttpHeaderMap |
Map of HTTP headers indexed by lower case header names. | |
Protected Member Functions | |
void | checkCreated () const |
Checks if the create was called. | |
void | checkFinalized () const |
Checks if the finalize was called. | |
template<typename T > | |
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. | |
Protected Attributes | |
bool | created_ |
Flag indicating whether create was called. | |
Direction | direction_ |
Message direction (inbound or outbound). | |
bool | finalized_ |
Flag indicating whether finalize was called. | |
HttpHeaderMap | headers_ |
Parsed HTTP headers. | |
HttpVersion | http_version_ |
HTTP version numbers. | |
HttpHeaderMap | required_headers_ |
Map holding required HTTP headers. | |
std::set< HttpVersion > | required_versions_ |
Set of required HTTP versions. | |
Base class for HttpRequest and HttpResponse.
This abstract class provides a common functionality for the HTTP requests and responses. Each such message can be marked as outbound or inbound. An HTTP inbound request is the one received by the server and HTTP inbound response is the response received by the client. Conversely, an HTTP outbound request is the request created by the client and HTTP outbound response is the response created by the server. There are differences in how the inbound and outbound messages are created. The inbound messages are received over the TCP sockets and parsed by the parsers. The parsed information is stored in a context, i.e. structure holding raw information and associated with the given HttpMessage
instance. Once the message is parsed and all required information is stored in the context, the create
method is called to validate and fetch information from the context into the message. The finalize
method is called to commit the HTTP message body into the message.
The outbound message is created locally from the known data, e.g. HTTP version number, URI, method etc. The headers can be then appended to the message via the context. In order to use this message the finalize
method must be called to commit this information. Them, toString
method can be called to generate the message in the textual form, which can be transferred via TCP socket.
Definition at line 62 of file http_message.h.
|
protected |
Map of HTTP headers indexed by lower case header names.
Definition at line 241 of file http_message.h.
Specifies the direction of the HTTP message.
Enumerator | |
---|---|
INBOUND | |
OUTBOUND |
Definition at line 66 of file http_message.h.
|
explicit |
Constructor.
direction | Direction of the message (inbound or outbound). |
Definition at line 14 of file http_message.cc.
|
virtual |
Destructor.
Definition at line 20 of file http_message.cc.
|
protected |
Checks if the create was called.
HttpMessageError | if create wasn't called. |
Definition at line 90 of file http_message.cc.
References created_, and isc_throw.
Referenced by getHeader(), getHttpVersion(), isc::http::HttpRequest::getMethod(), isc::http::HttpResponse::getStatusCode(), isc::http::HttpResponse::getStatusPhrase(), and isc::http::HttpRequest::getUri().
|
protected |
Checks if the finalize was called.
HttpMessageError | if finalize wasn't called. |
Definition at line 99 of file http_message.cc.
References finalized_, and isc_throw.
Referenced by isc::http::HttpRequest::getBody(), isc::http::HttpResponse::getBody(), isc::http::PostHttpRequestJson::getBodyAsJson(), isc::http::HttpResponseJson::getBodyAsJson(), isc::http::HttpRequest::toBriefString(), isc::http::HttpResponse::toBriefString(), and isc::http::HttpRequest::toString().
|
pure virtual |
Reads parsed message from the context, validates the message and stores parsed information.
This method must be called before retrieving parsed data using accessors. This method doesn't parse the HTTP request body.
Implemented in isc::http::HttpRequest, and isc::http::HttpResponse.
|
pure virtual |
Complete parsing HTTP message or creating an HTTP outbound message.
This method is used in two situations: when a message has been received into a context and may be fully parsed (including the body) or when the data for the creation of the outbound message have been stored in a context and the message can be now created from the context.
This method should call create
method if it hasn't been called yet and then read the message body from the context and interpret it. If the body doesn't adhere to the requirements for the message (in particular, when the content type of the body is invalid) an exception should be thrown.
Implemented in isc::http::PostHttpRequestJson, isc::http::HttpRequest, isc::http::HttpResponse, and isc::http::HttpResponseJson.
Referenced by isc::http::HttpMessageParserBase::parseEndedHandler().
|
pure virtual |
Returns HTTP message body as string.
Implemented in isc::http::HttpRequest, and isc::http::HttpResponse.
|
inline |
Returns HTTP message direction.
Definition at line 80 of file http_message.h.
References direction_.
Referenced by isc::http::HttpRequest::create(), and isc::http::HttpResponse::create().
HttpHeaderPtr isc::http::HttpMessage::getHeader | ( | const std::string & | header_name | ) | const |
Returns object encapsulating HTTP header.
header_name | HTTP header name. |
HttpMessageNonExistingHeader | if header with the specified name doesn't exist. |
HttpMessageError | if the request hasn't been created. |
Definition at line 60 of file http_message.cc.
References checkCreated(), headers_, and isc_throw.
Referenced by getHeaderValue(), getHeaderValueAsUint64(), isc::http::HttpRequest::isPersistent(), and isc::http::HttpRequest::toString().
std::string isc::http::HttpMessage::getHeaderValue | ( | const std::string & | header_name | ) | const |
Returns a value of the specified HTTP header.
header_name | Name of the HTTP header. |
HttpMessageError | if the header doesn't exist. |
Definition at line 74 of file http_message.cc.
References getHeader().
uint64_t isc::http::HttpMessage::getHeaderValueAsUint64 | ( | const std::string & | header_name | ) | const |
Returns a value of the specified HTTP header as number.
header_name | Name of the HTTP header. |
HttpMessageError | if the header doesn't exist or if the header value is not number. |
Definition at line 79 of file http_message.cc.
References getHeader(), isc_throw, and isc::Exception::what().
HttpVersion isc::http::HttpMessage::getHttpVersion | ( | ) | const |
Returns HTTP version number (major and minor).
Definition at line 54 of file http_message.cc.
References checkCreated(), and http_version_.
Referenced by isc::http::HttpRequest::isPersistent(), and isc::http::HttpRequest::toBriefString().
|
inlineprotected |
Checks if the set is empty or the specified element belongs to this set.
This is a convenience method used by the class to verify that the given HTTP method belongs to "required methods", HTTP version belongs to "required versions" etc.
element | Reference to the element. |
element_set | Reference to the set of elements. |
T | Element type, HttpVersion etc. |
Definition at line 224 of file http_message.h.
Referenced by isc::http::HttpRequest::create(), and isc::http::HttpResponse::create().
|
inline |
Checks if the message has been successfully finalized.
The message gets finalized on successful call to finalize
.
Definition at line 194 of file http_message.h.
References finalized_.
void isc::http::HttpMessage::requireHeader | ( | const std::string & | header_name | ) |
Specifies a required HTTP header for the HTTP message.
Required headers must be specified prior to calling create method. The specified header must exist in the received HTTP request. This puts no requirement on the header value.
header_name | Required header name. |
Definition at line 29 of file http_message.cc.
References required_headers_.
Referenced by isc::http::PostHttpRequest::PostHttpRequest(), and isc::http::PostHttpRequest::PostHttpRequest().
void isc::http::HttpMessage::requireHeaderValue | ( | const std::string & | header_name, |
const std::string & | header_value ) |
Specifies a required value of a header in the message.
Required header values must be specified prior to calling create method. The specified header must exist and its value must be equal to the value specified as second parameter.
header_name | HTTP header name. |
header_value | HTTP header value. |
Definition at line 37 of file http_message.cc.
References required_headers_.
Referenced by isc::http::PostHttpRequestJson::PostHttpRequestJson(), and isc::http::PostHttpRequestJson::PostHttpRequestJson().
void isc::http::HttpMessage::requireHttpVersion | ( | const HttpVersion & | version | ) |
Specifies HTTP version allowed.
Allowed HTTP versions must be specified prior to calling create method. If no version is specified, all versions are allowed.
version | Version number allowed for the request. |
Definition at line 24 of file http_message.cc.
References required_versions_, and version().
bool isc::http::HttpMessage::requiresBody | ( | ) | const |
Checks if the body is required for the HTTP message.
Current implementation simply checks if the "Content-Length" header is required.
Definition at line 44 of file http_message.cc.
References required_headers_.
|
pure virtual |
Reset the state of the object.
Implemented in isc::http::PostHttpRequestJson, isc::http::HttpRequest, isc::http::HttpResponse, and isc::http::HttpResponseJson.
Referenced by isc::http::PostHttpRequestJson::reset().
|
inline |
Sets direction for the HTTP message.
This is mostly useful in unit testing.
direction | New direction of the HTTP message. |
Definition at line 89 of file http_message.h.
References direction_.
|
pure virtual |
Returns HTTP message as text.
This method is called to generate the outbound HTTP message. Make sure to call finalize
prior to calling this method.
Implemented in isc::http::HttpRequest, and isc::http::HttpResponse.
|
protected |
Flag indicating whether create was called.
Definition at line 253 of file http_message.h.
Referenced by checkCreated(), isc::http::HttpRequest::create(), isc::http::HttpResponse::create(), isc::http::PostHttpRequestJson::finalize(), isc::http::HttpRequest::finalize(), isc::http::HttpResponse::finalize(), isc::http::HttpResponseJson::finalize(), isc::http::HttpRequest::reset(), and isc::http::HttpResponse::reset().
|
protected |
Message direction (inbound or outbound).
Definition at line 230 of file http_message.h.
Referenced by getDirection(), and setDirection().
|
protected |
Flag indicating whether finalize was called.
Definition at line 256 of file http_message.h.
Referenced by checkFinalized(), isc::http::PostHttpRequestJson::finalize(), isc::http::HttpRequest::finalize(), isc::http::HttpResponse::finalize(), isc::http::HttpResponseJson::finalize(), isFinalized(), isc::http::HttpRequest::reset(), and isc::http::HttpResponse::reset().
|
protected |
Parsed HTTP headers.
Definition at line 259 of file http_message.h.
Referenced by isc::http::HttpRequest::create(), isc::http::HttpResponse::create(), getHeader(), isc::http::HttpRequest::reset(), isc::http::HttpResponse::reset(), isc::http::HttpRequest::toString(), and isc::http::HttpResponse::toString().
|
protected |
HTTP version numbers.
Definition at line 238 of file http_message.h.
Referenced by isc::http::HttpRequest::create(), isc::http::HttpResponse::create(), getHttpVersion(), and isc::http::HttpResponse::toBriefString().
|
protected |
Map holding required HTTP headers.
The key of this map specifies the lower case HTTP header name. If the value of the HTTP header is empty, the header is required but the value of the header is not checked. If the value is non-empty, the value in the HTTP request must be equal (case insensitive) to the value in the map.
Definition at line 250 of file http_message.h.
Referenced by isc::http::HttpRequest::create(), isc::http::HttpResponse::create(), requireHeader(), requireHeaderValue(), and requiresBody().
|
protected |
Set of required HTTP versions.
If the set is empty, all versions are allowed.
Definition at line 235 of file http_message.h.
Referenced by isc::http::HttpRequest::create(), isc::http::HttpResponse::create(), and requireHttpVersion().