11 #include <boost/date_time/local_time/local_time.hpp>
12 #include <boost/date_time/time_facet.hpp>
21 const 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" }
42 const std::string crlf =
"\r\n";
49 HttpResponse::HttpResponse()
59 context_->status_code_ =
static_cast<unsigned int>(status_code);
61 if (generic_body.
set_) {
64 setGenericBody(status_code);
83 for (
auto header =
context_->headers_.begin();
87 headers_[hdr->getLowerCaseName()] = hdr;
93 headers_[
"content-length"] = length_header;
96 headers_[
"date"] = date_header;
104 auto header =
headers_.find(req_header->first);
107 <<
" not found in the HTTP response");
108 }
else if (!req_header->second->getValue().empty() &&
109 !header->second->isValueEqual(req_header->second->getValue())) {
113 <<
" value is " << req_header->second->getValue()
114 <<
", but " << header->second->getValue() <<
" was found");
118 }
catch (
const std::exception& ex) {
147 return (static_cast<HttpStatusCode>(
context_->status_code_));
166 return ((c >= 400) && (c < 500));
173 return ((c >= 500) && (c < 600));
178 auto status_code_it = status_code_to_description.find(status_code);
179 if (status_code_it == status_code_to_description.end()) {
181 " description for the given status code "
182 << static_cast<uint16_t>(status_code));
184 return (status_code_it->second);
189 return (static_cast<uint16_t>(status_code));
203 std::ostringstream s;
214 std::ostringstream s;
220 s << header_it->second->getName() <<
": " << header_it->second->getValue()
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
std::string rfc1123Format() const
Returns time value formatted as specified in RFC 1123.
virtual void finalize()
Completes creation of the HTTP response.
Direction getDirection() const
Returns HTTP message direction.
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
bool finalized_
Flag indicating whether finalize was called.
std::string getStatusPhrase() const
Returns HTTP status phrase.
virtual std::string toString() const
Returns HTTP response as string.
virtual std::string getBody() const
Returns HTTP response body as string.
unsigned major_
Major HTTP version.
virtual void create()
Commits information held in the context into the response.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
bool created_
Flag indicating whether create was called.
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
HttpHeaderMap required_headers_
Map holding required HTTP headers.
Base class for HttpRequest and HttpResponse.
int version()
returns Kea hooks version.
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
Generic exception thrown by HttpResponse class.
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
This class parses and generates time values used in HTTP.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
HttpStatusCode getStatusCode() const
Returns HTTP status code.
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void checkFinalized() const
Checks if the finalize was called.
virtual void reset()
Reset the state of the object.
HttpVersion http_version_
HTTP version numbers.
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
void checkCreated() const
Checks if the create was called.
HttpHeaderMap headers_
Parsed HTTP headers.
HttpResponse()
Constructor for the inbound HTTP response.
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.
unsigned minor_
Minor HTTP version.
bool set_
A storage for the boolean flag.
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
HttpStatusCode
HTTP status codes (cf RFC 2068)