Kea 2.7.5
isc::http::HttpResponse Class Reference

Represents HTTP response message. More...

#include <response.h>

+ Inheritance diagram for isc::http::HttpResponse:

Public Member Functions

 HttpResponse ()
 Constructor for the inbound HTTP response.
 
 HttpResponse (const HttpVersion &version, const HttpStatusCode &status_code, const CallSetGenericBody &generic_body=CallSetGenericBody::yes())
 Constructor for outbound HTTP response.
 
const HttpResponseContextPtrcontext () const
 Returns pointer to the HttpResponseContext.
 
virtual void create ()
 Commits information held in the context into the response.
 
virtual void finalize ()
 Completes creation of the HTTP response.
 
virtual std::string getBody () const
 Returns HTTP response body as string.
 
virtual std::string getDateHeaderValue () const
 Returns current time formatted as required by RFC 1123.
 
data::ConstElementPtr getJsonElement (const std::string &element_name) const
 Retrieves a single JSON element.
 
HttpStatusCode getStatusCode () const
 Returns HTTP status code.
 
std::string getStatusPhrase () const
 Returns HTTP status phrase.
 
virtual void reset ()
 Reset the state of the object.
 
std::string toBriefString () const
 Returns HTTP version and HTTP status as a string.
 
virtual std::string toString () const
 Returns HTTP response as string.
 
- Public Member Functions inherited from isc::http::HttpMessage
 HttpMessage (const Direction &direction)
 Constructor.
 
virtual ~HttpMessage ()
 Destructor.
 
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.
 
void setDirection (const Direction &direction)
 Sets direction for the HTTP message.
 

Static Public Member Functions

static bool isClientError (const HttpStatusCode &status_code)
 Checks if the status code indicates client error.
 
static bool isServerError (const HttpStatusCode &status_code)
 Checks if the status code indicates server error.
 
static uint16_t statusCodeToNumber (const HttpStatusCode &status_code)
 Convenience method converting status code to numeric value.
 
static std::string statusCodeToString (const HttpStatusCode &status_code)
 Converts status code to string.
 

Protected Attributes

HttpResponseContextPtr context_
 Pointer to the HttpResponseContext holding parsed data.
 
- Protected Attributes inherited from isc::http::HttpMessage
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< HttpVersionrequired_versions_
 Set of required HTTP versions.
 

Additional Inherited Members

- Public Types inherited from isc::http::HttpMessage
enum  Direction { INBOUND , OUTBOUND }
 Specifies the direction of the HTTP message. More...
 
- Protected Types inherited from isc::http::HttpMessage
typedef std::map< std::string, HttpHeaderPtrHttpHeaderMap
 Map of HTTP headers indexed by lower case header names.
 
- Protected Member Functions inherited from isc::http::HttpMessage
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.
 

Detailed Description

Represents HTTP response message.

This derivation of the HttpMessage class is specialized to represent HTTP responses. This class provides two constructors for creating an inbound and outbound response instance respectively. This class is associated with an instance of the HttpResponseContext, which is used to provide response specific values, such as HTTP status and headers.

The derivations of this class provide specializations and specify the HTTP versions and headers supported/required in the specific use cases. For example, the HttpResponseJson class derives from the HttpResponse and it requires that response includes a body in the JSON format.

Definition at line 98 of file response.h.

Constructor & Destructor Documentation

◆ HttpResponse() [1/2]

isc::http::HttpResponse::HttpResponse ( )
explicit

Constructor for the inbound HTTP response.

Definition at line 49 of file response.cc.

◆ HttpResponse() [2/2]

isc::http::HttpResponse::HttpResponse ( const HttpVersion & version,
const HttpStatusCode & status_code,
const CallSetGenericBody & generic_body = CallSetGenericBody::yes() )
explicit

Constructor for outbound HTTP response.

Creates basic instance of the object. It sets the HTTP version and the status code to be included in the response.

Parameters
versionHTTP version.
status_codeHTTP status code.
generic_bodyIndicates if the constructor should call setGenericBody to create a generic content for the given status code. This should be set to "no" when the constructor is called by the derived class which provides its own implementation of the setGenericBody method.

Definition at line 53 of file response.cc.

References context_, and version().

+ Here is the call graph for this function:

Member Function Documentation

◆ context()

const HttpResponseContextPtr & isc::http::HttpResponse::context ( ) const
inline

Returns pointer to the HttpResponseContext.

The context holds intermediate data for creating a response. The response parser stores parsed raw data in the context. When parsing is finished, the data are validated and committed into the HttpResponse.

Returns
Pointer to the underlying HttpResponseContext.

Definition at line 129 of file response.h.

References context_.

Referenced by isc::http::HttpResponseJson::HttpResponseJson(), isc::http::HttpResponseJson::HttpResponseJson(), and isc::http::HttpResponseJson::setBodyAsJson().

◆ create()

void isc::http::HttpResponse::create ( )
virtual

Commits information held in the context into the response.

This function reads HTTP version, status code and headers from the context and validates their values. For the outbound messages, it automatically appends Content-Length and Date headers to the response. The Content-Length is set to the body size. The Date is set to the current date and time.

Implements isc::http::HttpMessage.

Definition at line 69 of file response.cc.

References context_, isc::http::HttpMessage::created_, getDateHeaderValue(), isc::http::HttpMessage::getDirection(), isc::http::HttpMessage::headers_, isc::http::HttpMessage::http_version_, isc::http::HttpMessage::inRequiredSet(), isc_throw, isc::http::HttpVersion::major_, isc::http::HttpVersion::minor_, isc::http::HttpMessage::OUTBOUND, isc::http::HttpMessage::required_headers_, isc::http::HttpMessage::required_versions_, reset(), and isc::Exception::what().

Referenced by finalize(), and isc::http::HttpResponseJson::finalize().

+ Here is the call graph for this function:

◆ finalize()

void isc::http::HttpResponse::finalize ( )
virtual

Completes creation of the HTTP response.

This method marks the response as finalized. The outbound response may now be sent over the TCP socket. The information from the inbound message may be read, including the response body.

Implements isc::http::HttpMessage.

Reimplemented in isc::http::HttpResponseJson.

Definition at line 125 of file response.cc.

References create(), isc::http::HttpMessage::created_, and isc::http::HttpMessage::finalized_.

+ Here is the call graph for this function:

◆ getBody()

std::string isc::http::HttpResponse::getBody ( ) const
virtual

Returns HTTP response body as string.

Implements isc::http::HttpMessage.

Definition at line 153 of file response.cc.

References isc::http::HttpMessage::checkFinalized(), and context_.

Referenced by toString().

+ Here is the call graph for this function:

◆ getDateHeaderValue()

std::string isc::http::HttpResponse::getDateHeaderValue ( ) const
virtual

Returns current time formatted as required by RFC 1123.

This method is virtual so as it can be overridden in unit tests to return a "predictable" value of time, e.g. constant value.

Returns
Current time formatted as required by RFC 1123.

Definition at line 189 of file response.cc.

Referenced by create().

◆ getJsonElement()

data::ConstElementPtr isc::http::HttpResponse::getJsonElement ( const std::string & element_name) const

Retrieves a single JSON element.

The element must be at top level of the JSON structure.

Parameters
element_nameElement name.
Returns
Pointer to the specified element or NULL if such element doesn't exist.
Exceptions
HttpResponseJsonErrorif an error occurred.

◆ getStatusCode()

HttpStatusCode isc::http::HttpResponse::getStatusCode ( ) const

Returns HTTP status code.

Definition at line 141 of file response.cc.

References isc::http::HttpMessage::checkCreated(), and context_.

+ Here is the call graph for this function:

◆ getStatusPhrase()

std::string isc::http::HttpResponse::getStatusPhrase ( ) const

Returns HTTP status phrase.

Definition at line 147 of file response.cc.

References isc::http::HttpMessage::checkCreated(), and context_.

+ Here is the call graph for this function:

◆ isClientError()

bool isc::http::HttpResponse::isClientError ( const HttpStatusCode & status_code)
static

Checks if the status code indicates client error.

Parameters
status_codeHTTP status code.
Returns
true if the status code indicates client error.

Definition at line 159 of file response.cc.

References statusCodeToNumber().

+ Here is the call graph for this function:

◆ isServerError()

bool isc::http::HttpResponse::isServerError ( const HttpStatusCode & status_code)
static

Checks if the status code indicates server error.

Parameters
status_codeHTTP status code.
Returns
true if the status code indicates server error.

Definition at line 166 of file response.cc.

References statusCodeToNumber().

+ Here is the call graph for this function:

◆ reset()

void isc::http::HttpResponse::reset ( )
virtual

Reset the state of the object.

Implements isc::http::HttpMessage.

Reimplemented in isc::http::HttpResponseJson.

Definition at line 134 of file response.cc.

References isc::http::HttpMessage::created_, isc::http::HttpMessage::finalized_, and isc::http::HttpMessage::headers_.

Referenced by create(), and isc::http::HttpResponseJson::reset().

◆ statusCodeToNumber()

uint16_t isc::http::HttpResponse::statusCodeToNumber ( const HttpStatusCode & status_code)
static

Convenience method converting status code to numeric value.

Parameters
status_codeStatus code represented as enum.
Returns
Numeric representation of the status code.

Definition at line 184 of file response.cc.

Referenced by isc::config::CmdResponseCreator::filterCommand(), isClientError(), and isServerError().

◆ statusCodeToString()

std::string isc::http::HttpResponse::statusCodeToString ( const HttpStatusCode & status_code)
static

Converts status code to string.

Parameters
status_codeHTTP status code.
Returns
Textual representation of the status code.

Definition at line 173 of file response.cc.

References isc_throw.

Referenced by isc::config::CmdResponseCreator::filterCommand(), and toBriefString().

◆ toBriefString()

std::string isc::http::HttpResponse::toBriefString ( ) const

Returns HTTP version and HTTP status as a string.

Definition at line 196 of file response.cc.

References isc::http::HttpMessage::checkFinalized(), context_, isc::http::HttpMessage::http_version_, isc::http::HttpVersion::major_, isc::http::HttpVersion::minor_, and statusCodeToString().

Referenced by toString().

+ Here is the call graph for this function:

◆ toString()

std::string isc::http::HttpResponse::toString ( ) const
virtual

Returns HTTP response as string.

This method is called to generate the outbound HTTP response. Make sure to call finalize prior to calling this method.

Implements isc::http::HttpMessage.

Definition at line 208 of file response.cc.

References getBody(), isc::http::HttpMessage::headers_, and toBriefString().

+ Here is the call graph for this function:

Member Data Documentation

◆ context_

HttpResponseContextPtr isc::http::HttpResponse::context_
protected

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