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.
virtual void finalize()
Completes creation of the HTTP response.
virtual std::string getBody() const
Returns HTTP response body as string.
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
void checkCreated() const
Checks if the create was called.
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.
std::string toBriefString() const
Returns HTTP version and HTTP status as a string.
unsigned major_
Major HTTP version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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.
std::string rfc1123Format() const
Returns time value formatted as specified in RFC 1123.
int version()
returns Kea hooks version.
Generic exception thrown by HttpResponse class.
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
void checkFinalized() const
Checks if the finalize was called.
This class parses and generates time values used in HTTP.
std::string getStatusPhrase() const
Returns HTTP status phrase.
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Defines the logger used by the top-level component of kea-lfc.
HttpStatusCode getStatusCode() const
Returns HTTP status code.
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.
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
virtual std::string toString() const
Returns HTTP response as string.
Direction getDirection() const
Returns HTTP message direction.
HttpHeaderMap headers_
Parsed HTTP headers.
virtual std::string getDateHeaderValue() const
Returns current time formatted as required by RFC 1123.
HttpResponse()
Constructor for the inbound HTTP response.
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)