11#include <boost/date_time/local_time/local_time.hpp>
12#include <boost/date_time/time_facet.hpp>
15using namespace boost::local_time;
21const std::map<HttpStatusCode, std::string> status_code_to_description = {
22 { HttpStatusCode::OK,
"OK" },
23 { HttpStatusCode::CREATED,
"Created" },
24 { HttpStatusCode::ACCEPTED,
"Accepted" },
25 { HttpStatusCode::NO_CONTENT,
"No Content" },
26 { HttpStatusCode::MULTIPLE_CHOICES,
"Multiple Choices" },
27 { HttpStatusCode::MOVED_PERMANENTLY,
"Moved Permanently" },
28 { HttpStatusCode::MOVED_TEMPORARILY,
"Moved Temporarily" },
29 { HttpStatusCode::NOT_MODIFIED,
"Not Modified" },
30 { HttpStatusCode::BAD_REQUEST,
"Bad Request" },
31 { HttpStatusCode::UNAUTHORIZED,
"Unauthorized" },
32 { HttpStatusCode::FORBIDDEN,
"Forbidden" },
33 { HttpStatusCode::NOT_FOUND,
"Not Found" },
34 { HttpStatusCode::REQUEST_TIMEOUT,
"Request Timeout" },
35 { HttpStatusCode::INTERNAL_SERVER_ERROR,
"Internal Server Error" },
36 { HttpStatusCode::NOT_IMPLEMENTED,
"Not Implemented" },
37 { HttpStatusCode::BAD_GATEWAY,
"Bad Gateway" },
38 { HttpStatusCode::SERVICE_UNAVAILABLE,
"Service Unavailable" }
42const std::string crlf =
"\r\n";
59 context_->status_code_ =
static_cast<unsigned int>(status_code);
61 if (generic_body.
set_) {
64 setGenericBody(status_code);
83 for (
auto const& header :
context_->headers_) {
85 headers_[hdr->getLowerCaseName()] = hdr;
91 headers_[
"content-length"] = length_header;
100 auto header =
headers_.find(req_header.first);
103 <<
" not found in the HTTP response");
104 }
else if (!req_header.second->getValue().empty() &&
105 !header->second->isValueEqual(req_header.second->getValue())) {
109 <<
" value is " << req_header.second->getValue()
110 <<
", but " << header->second->getValue() <<
" was found");
114 }
catch (
const std::exception& ex) {
162 return ((c >= 400) && (c < 500));
169 return ((c >= 500) && (c < 600));
174 auto status_code_it = status_code_to_description.find(status_code);
175 if (status_code_it == status_code_to_description.end()) {
177 " description for the given status code "
178 <<
static_cast<uint16_t
>(status_code));
180 return (status_code_it->second);
185 return (
static_cast<uint16_t
>(status_code));
199 std::ostringstream s;
210 std::ostringstream s;
214 for (
auto const& header_it :
headers_) {
215 s << header_it.second->getName() <<
": " << header_it.second->getValue()
int version()
returns Kea hooks version.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This class parses and generates time values used in HTTP.
std::string rfc1123Format() const
Returns time value formatted as specified in RFC 1123.
Base class for HttpRequest and HttpResponse.
HttpHeaderMap headers_
Parsed HTTP headers.
HttpVersion http_version_
HTTP version numbers.
void checkFinalized() const
Checks if the finalize was called.
void checkCreated() const
Checks if the create was called.
bool created_
Flag indicating whether create was called.
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
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.
bool finalized_
Flag indicating whether finalize was called.
HttpHeaderMap required_headers_
Map holding required HTTP headers.
Generic exception thrown by HttpResponse class.
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
virtual std::string getBody() const
Returns HTTP response body as string.
HttpResponse()
Constructor for the inbound HTTP response.
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
HttpStatusCode getStatusCode() const
Returns HTTP status code.
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
virtual void finalize()
Completes creation of the HTTP response.
virtual std::string toString() const
Returns HTTP response as string.
std::string getStatusPhrase() const
Returns HTTP status phrase.
virtual void reset()
Reset the state of the object.
virtual void create()
Commits information held in the context into the response.
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
HttpStatusCode
HTTP status codes (cf RFC 2068)
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Defines the logger used by the top-level component of kea-lfc.
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
bool set_
A storage for the boolean flag.
unsigned minor_
Minor HTTP version.
unsigned major_
Major HTTP version.