Kea  1.9.9-git
isc::http::HttpMessage Class Referenceabstract

Base class for HttpRequest and HttpResponse. More...

#include <http_message.h>

+ Inheritance diagram for isc::http::HttpMessage:

Public Types

enum  Direction { INBOUND, OUTBOUND }
 Specifies the direction of the HTTP message. More...
 

Public Member Functions

 HttpMessage (const Direction &direction)
 Constructor. More...
 
virtual ~HttpMessage ()
 Destructor. More...
 
virtual void create ()=0
 Reads parsed message from the context, validates the message and stores parsed information. More...
 
virtual void finalize ()=0
 Complete parsing HTTP message or creating an HTTP outbound message. More...
 
virtual std::string getBody () const =0
 Returns HTTP message body as string. More...
 
Direction getDirection () const
 Returns HTTP message direction. More...
 
HttpHeaderPtr getHeader (const std::string &header_name) const
 Returns object encapsulating HTTP header. More...
 
std::string getHeaderValue (const std::string &header_name) const
 Returns a value of the specified HTTP header. More...
 
uint64_t getHeaderValueAsUint64 (const std::string &header_name) const
 Returns a value of the specified HTTP header as number. More...
 
HttpVersion getHttpVersion () const
 Returns HTTP version number (major and minor). More...
 
bool isFinalized () const
 Checks if the message has been successfully finalized. More...
 
void requireHeader (const std::string &header_name)
 Specifies a required HTTP header for the HTTP message. More...
 
void requireHeaderValue (const std::string &header_name, const std::string &header_value)
 Specifies a required value of a header in the message. More...
 
void requireHttpVersion (const HttpVersion &version)
 Specifies HTTP version allowed. More...
 
bool requiresBody () const
 Checks if the body is required for the HTTP message. More...
 
virtual void reset ()=0
 Reset the state of the object. More...
 
void setDirection (const Direction &direction)
 Sets direction for the HTTP message. More...
 
virtual std::string toString () const =0
 Returns HTTP message as text. More...
 

Protected Types

typedef std::map< std::string, HttpHeaderPtrHttpHeaderMap
 Map of HTTP headers indexed by lower case header names. More...
 

Protected Member Functions

void checkCreated () const
 Checks if the create was called. More...
 
void checkFinalized () const
 Checks if the finalize was called. More...
 
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. More...
 

Protected Attributes

bool created_
 Flag indicating whether create was called. More...
 
Direction direction_
 Message direction (inbound or outbound). More...
 
bool finalized_
 Flag indicating whether finalize was called. More...
 
HttpHeaderMap headers_
 Parsed HTTP headers. More...
 
HttpVersion http_version_
 HTTP version numbers. More...
 
HttpHeaderMap required_headers_
 Map holding required HTTP headers. More...
 
std::set< HttpVersionrequired_versions_
 Set of required HTTP versions. More...
 

Detailed Description

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.

Member Typedef Documentation

typedef std::map<std::string, HttpHeaderPtr> isc::http::HttpMessage::HttpHeaderMap
protected

Map of HTTP headers indexed by lower case header names.

Definition at line 241 of file http_message.h.

Member Enumeration Documentation

Specifies the direction of the HTTP message.

Enumerator
INBOUND 
OUTBOUND 

Definition at line 66 of file http_message.h.

Constructor & Destructor Documentation

isc::http::HttpMessage::HttpMessage ( const Direction direction)
explicit

Constructor.

Parameters
directionDirection of the message (inbound or outbound).

Definition at line 14 of file http_message.cc.

isc::http::HttpMessage::~HttpMessage ( )
virtual

Destructor.

Definition at line 20 of file http_message.cc.

Member Function Documentation

void isc::http::HttpMessage::checkCreated ( ) const
protected
virtual void isc::http::HttpMessage::create ( )
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::HttpResponse, and isc::http::HttpRequest.

virtual void isc::http::HttpMessage::finalize ( )
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::HttpResponse, isc::http::HttpRequest, isc::http::PostHttpRequestJson, and isc::http::HttpResponseJson.

Referenced by isc::http::HttpMessageParserBase::parseEndedHandler().

virtual std::string isc::http::HttpMessage::getBody ( ) const
pure virtual

Returns HTTP message body as string.

Implemented in isc::http::HttpResponse, and isc::http::HttpRequest.

Direction isc::http::HttpMessage::getDirection ( ) const
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.

Parameters
header_nameHTTP header name.
Returns
Non-null pointer to the header.
Exceptions
HttpMessageNonExistingHeaderif header with the specified name doesn't exist.
HttpMessageErrorif the request hasn't been created.

Definition at line 60 of file http_message.cc.

References checkCreated(), isc::http::HttpHeader::getLowerCaseName(), headers_, and isc_throw.

Referenced by getHeaderValue(), getHeaderValueAsUint64(), isc::http::HttpRequest::isPersistent(), and isc::http::HttpRequest::toString().

+ Here is the call graph for this function:

std::string isc::http::HttpMessage::getHeaderValue ( const std::string &  header_name) const

Returns a value of the specified HTTP header.

Parameters
header_nameName of the HTTP header.
Exceptions
HttpMessageErrorif the header doesn't exist.

Definition at line 74 of file http_message.cc.

References getHeader().

+ Here is the call graph for this function:

uint64_t isc::http::HttpMessage::getHeaderValueAsUint64 ( const std::string &  header_name) const

Returns a value of the specified HTTP header as number.

Parameters
header_nameName of the HTTP header.
Exceptions
HttpMessageErrorif 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().

+ Here is the call graph for this function:

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().

+ Here is the call graph for this function:

template<typename T >
bool isc::http::HttpMessage::inRequiredSet ( const T &  element,
const std::set< T > &  element_set 
) const
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.

Parameters
elementReference to the element.
element_setReference to the set of elements.
Template Parameters
TElement type, HttpVersion etc.
Returns
true if the element set is empty or if the element belongs to the set.

Definition at line 224 of file http_message.h.

Referenced by isc::http::HttpRequest::create(), and isc::http::HttpResponse::create().

bool isc::http::HttpMessage::isFinalized ( ) const
inline

Checks if the message has been successfully finalized.

The message gets finalized on successful call to finalize.

Returns
true if the message has been finalized, false otherwise.

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.

Parameters
header_nameRequired header name.

Definition at line 29 of file http_message.cc.

References required_headers_.

Referenced by 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.

Parameters
header_nameHTTP header name.
header_valueHTTP header value.

Definition at line 37 of file http_message.cc.

References required_headers_.

Referenced by 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.

Parameters
versionVersion number allowed for the request.

Definition at line 24 of file http_message.cc.

References required_versions_.

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.

Returns
true if the body is required, false otherwise.

Definition at line 44 of file http_message.cc.

References required_headers_.

virtual void isc::http::HttpMessage::reset ( )
pure virtual
void isc::http::HttpMessage::setDirection ( const Direction direction)
inline

Sets direction for the HTTP message.

This is mostly useful in unit testing.

Parameters
directionNew direction of the HTTP message.

Definition at line 89 of file http_message.h.

References direction_.

virtual std::string isc::http::HttpMessage::toString ( ) const
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::HttpResponse, and isc::http::HttpRequest.

Member Data Documentation

Direction isc::http::HttpMessage::direction_
protected

Message direction (inbound or outbound).

Definition at line 230 of file http_message.h.

Referenced by getDirection(), and setDirection().

HttpVersion isc::http::HttpMessage::http_version_
protected
HttpHeaderMap isc::http::HttpMessage::required_headers_
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().

std::set<HttpVersion> isc::http::HttpMessage::required_versions_
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().


The documentation for this class was generated from the following files: