Kea 2.7.0
http_message.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <http/http_message.h>
10
11namespace isc {
12namespace http {
13
15 : direction_(direction), required_versions_(),
16 http_version_(HttpVersion::HTTP_10()), required_headers_(),
17 created_(false), finalized_(false), headers_() {
18}
19
22
23void
27
28void
29HttpMessage::requireHeader(const std::string& header_name) {
30 // Empty value denotes that the header is required but no specific
31 // value is expected.
32 HttpHeaderPtr hdr(new HttpHeader(header_name));
33 required_headers_[hdr->getLowerCaseName()] = hdr;
34}
35
36void
37HttpMessage::requireHeaderValue(const std::string& header_name,
38 const std::string& header_value) {
39 HttpHeaderPtr hdr(new HttpHeader(header_name, header_value));
40 required_headers_[hdr->getLowerCaseName()] = hdr;
41}
42
43bool
45 // If Content-Length is required the body must exist too. There may
46 // be probably some cases when Content-Length is not provided but
47 // the body is provided. But, probably not in our use cases.
48 // Use lower case header name because this is how it is indexed in
49 // the storage.
50 return (required_headers_.find("content-length") != required_headers_.end());
51}
52
58
60HttpMessage::getHeader(const std::string& header_name) const {
62
63 HttpHeader hdr(header_name);
64 auto header_it = headers_.find(hdr.getLowerCaseName());
65 if (header_it != headers_.end()) {
66 return (header_it->second);
67 }
68
69 isc_throw(HttpMessageNonExistingHeader, header_name << " HTTP header"
70 " not found in the request");
71}
72
73std::string
74HttpMessage::getHeaderValue(const std::string& header_name) const {
75 return (getHeader(header_name)->getValue());
76}
77
78uint64_t
79HttpMessage::getHeaderValueAsUint64(const std::string& header_name) const {
80 try {
81 return (getHeader(header_name)->getUint64Value());
82
83 } catch (const std::exception& ex) {
84 // The specified header does exist, but the value is not a number.
86 }
87}
88
89void
91 if (!created_) {
92 isc_throw(HttpMessageError, "unable to retrieve values of HTTP"
93 " message because the HttpMessage::create() must be"
94 " called first. This is a programmatic error");
95 }
96}
97
98void
100 if (!finalized_) {
101 isc_throw(HttpMessageError, "unable to retrieve body of HTTP"
102 " message because the HttpMessage::finalize() must be"
103 " called first. This is a programmatic error");
104 }
105}
106
107} // end of namespace isc::http
108} // end of namespace isc
int version()
returns Kea hooks version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Represents HTTP header including a header name and value.
Definition http_header.h:20
Generic exception thrown by HttpMessage class.
Exception thrown when attempt is made to retrieve a non-existing header.
HttpHeaderMap headers_
Parsed HTTP headers.
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
bool requiresBody() const
Checks if the body is required for the HTTP message.
HttpVersion http_version_
HTTP version numbers.
HttpVersion getHttpVersion() const
Returns HTTP version number (major and minor).
uint64_t getHeaderValueAsUint64(const std::string &header_name) const
Returns a value of the specified HTTP header as number.
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.
HttpMessage(const Direction &direction)
Constructor.
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
virtual ~HttpMessage()
Destructor.
std::string getHeaderValue(const std::string &header_name) const
Returns a value of the specified HTTP header.
HttpHeaderPtr getHeader(const std::string &header_name) const
Returns object encapsulating HTTP header.
Direction
Specifies the direction of the HTTP message.
void requireHeader(const std::string &header_name)
Specifies a required HTTP header for the HTTP message.
bool finalized_
Flag indicating whether finalize was called.
void requireHttpVersion(const HttpVersion &version)
Specifies HTTP version allowed.
HttpHeaderMap required_headers_
Map holding required HTTP headers.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Definition http_header.h:65
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition http_types.h:14