Kea 2.7.5
|
Represents HTTP response message. More...
#include <response.h>
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 HttpResponseContextPtr & | context () 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< HttpVersion > | required_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, HttpHeaderPtr > | HttpHeaderMap |
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. | |
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.
|
explicit |
Constructor for the inbound HTTP response.
Definition at line 49 of file response.cc.
|
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.
version | HTTP version. |
status_code | HTTP status code. |
generic_body | Indicates 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().
|
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
.
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().
|
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().
|
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_.
|
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().
|
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.
Definition at line 189 of file response.cc.
Referenced by create().
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.
element_name | Element name. |
HttpResponseJsonError | if an error occurred. |
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_.
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_.
|
static |
Checks if the status code indicates client error.
status_code | HTTP status code. |
Definition at line 159 of file response.cc.
References statusCodeToNumber().
|
static |
Checks if the status code indicates server error.
status_code | HTTP status code. |
Definition at line 166 of file response.cc.
References statusCodeToNumber().
|
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().
|
static |
Convenience method converting status code to numeric value.
status_code | Status code represented as enum. |
Definition at line 184 of file response.cc.
Referenced by isc::config::CmdResponseCreator::filterCommand(), isClientError(), and isServerError().
|
static |
Converts status code to string.
status_code | HTTP status code. |
Definition at line 173 of file response.cc.
References isc_throw.
Referenced by isc::config::CmdResponseCreator::filterCommand(), and 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().
|
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().
|
protected |
Pointer to the HttpResponseContext holding parsed data.
Definition at line 241 of file response.h.
Referenced by HttpResponse(), context(), create(), getBody(), getStatusCode(), getStatusPhrase(), isc::http::HttpResponseJson::parseBodyAsJson(), and toBriefString().