| Kea 3.0.2-git

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
11 namespace isc {
12 namespace http {
13
14 HttpMessage::HttpMessage(const HttpMessage::Direction& direction)
15 : direction_(direction), required_versions_(),
16 http_version_(HttpVersion::HTTP_10()), required_headers_(),
17 created_(false), finalized_(false), headers_() {
18}
19
20 HttpMessage::~HttpMessage() {
21}
22
23 void
24 HttpMessage::requireHttpVersion(const HttpVersion& version) {
25 required_versions_.insert(version);
26}
27
28 void
29 HttpMessage::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
36 void
37 HttpMessage::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
43 bool
44 HttpMessage::requiresBody() const {
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
53 HttpVersion
54 HttpMessage::getHttpVersion() const {
55 checkCreated();
56 return (http_version_);
57}
58
59 HttpHeaderPtr
60 HttpMessage::getHeader(const std::string& header_name) const {
61 checkCreated();
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
74 HttpMessage::getHeaderValue(const std::string& header_name) const {
75 return (getHeader(header_name)->getValue());
76}
77
78uint64_t
79 HttpMessage::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.
85 isc_throw(HttpMessageError, ex.what());
86 }
87}
88
89 void
90 HttpMessage::checkCreated() const {
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
98 void
99 HttpMessage::checkFinalized() const {
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
isc::http::HttpHeader
Represents HTTP header including a header name and value.
Definition http_header.h:20
isc::http::HttpHeader::getLowerCaseName
std::string getLowerCaseName() const
Returns lower case header name.
Definition http_header.cc:34
isc::http::HttpMessageError
Generic exception thrown by HttpMessage class.
Definition http_message.h:22
isc::http::HttpMessageNonExistingHeader
Exception thrown when attempt is made to retrieve a non-existing header.
Definition http_message.h:30
isc::http::HttpMessage::direction_
Direction direction_
Message direction (inbound or outbound).
Definition http_message.h:230
isc::http::HttpMessage::headers_
HttpHeaderMap headers_
Parsed HTTP headers.
Definition http_message.h:259
isc::http::HttpMessage::requireHeaderValue
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
Definition http_message.cc:37
isc::http::HttpMessage::requiresBody
bool requiresBody() const
Checks if the body is required for the HTTP message.
Definition http_message.cc:44
isc::http::HttpMessage::http_version_
HttpVersion http_version_
HTTP version numbers.
Definition http_message.h:238
isc::http::HttpMessage::getHttpVersion
HttpVersion getHttpVersion() const
Returns HTTP version number (major and minor).
Definition http_message.cc:54
isc::http::HttpMessage::getHeaderValueAsUint64
uint64_t getHeaderValueAsUint64(const std::string &header_name) const
Returns a value of the specified HTTP header as number.
Definition http_message.cc:79
isc::http::HttpMessage::checkFinalized
void checkFinalized() const
Checks if the finalize was called.
Definition http_message.cc:99
isc::http::HttpMessage::checkCreated
void checkCreated() const
Checks if the create was called.
Definition http_message.cc:90
isc::http::HttpMessage::created_
bool created_
Flag indicating whether create was called.
Definition http_message.h:253
isc::http::HttpMessage::HttpMessage
HttpMessage(const Direction &direction)
Constructor.
Definition http_message.cc:14
isc::http::HttpMessage::required_versions_
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
Definition http_message.h:235
isc::http::HttpMessage::~HttpMessage
virtual ~HttpMessage()
Destructor.
Definition http_message.cc:20
isc::http::HttpMessage::getHeaderValue
std::string getHeaderValue(const std::string &header_name) const
Returns a value of the specified HTTP header.
Definition http_message.cc:74
isc::http::HttpMessage::getHeader
HttpHeaderPtr getHeader(const std::string &header_name) const
Returns object encapsulating HTTP header.
Definition http_message.cc:60
isc::http::HttpMessage::Direction
Direction
Specifies the direction of the HTTP message.
Definition http_message.h:66
isc::http::HttpMessage::requireHeader
void requireHeader(const std::string &header_name)
Specifies a required HTTP header for the HTTP message.
Definition http_message.cc:29
isc::http::HttpMessage::finalized_
bool finalized_
Flag indicating whether finalize was called.
Definition http_message.h:256
isc::http::HttpMessage::requireHttpVersion
void requireHttpVersion(const HttpVersion &version)
Specifies HTTP version allowed.
Definition http_message.cc:24
isc::http::HttpMessage::required_headers_
HttpHeaderMap required_headers_
Map holding required HTTP headers.
Definition http_message.h:250
version
int version()
returns Kea hooks version.
Definition d2/gss_tsig/version.cc:13
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
http_message.h
isc::http
Definition auth_config.h:19
isc::http::HttpHeaderPtr
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Definition http_header.h:65
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::http::HttpVersion
HTTP protocol version.
Definition http_types.h:14