Kea  1.9.9-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 
15  : direction_(direction), required_versions_(),
16  http_version_(HttpVersion::HTTP_10()), required_headers_(),
17  created_(false), finalized_(false), headers_() {
18 }
19 
21 }
22 
23 void
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
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 
55  checkCreated();
56  return (http_version_);
57 }
58 
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 
73 std::string
74 HttpMessage::getHeaderValue(const std::string& header_name) const {
75  return (getHeader(header_name)->getValue());
76 }
77 
78 uint64_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.
86  }
87 }
88 
89 void
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
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
boost::shared_ptr< HttpHeader > HttpHeaderPtr
Pointer to the HttpHeader class.
Definition: http_header.h:65
Exception thrown when attempt is made to retrieve a non-existing header.
Definition: http_message.h:30
Generic exception thrown by HttpMessage class.
Definition: http_message.h:22
std::string getHeaderValue(const std::string &header_name) const
Returns a value of the specified HTTP header.
Definition: http_message.cc:74
bool finalized_
Flag indicating whether finalize was called.
Definition: http_message.h:256
HTTP protocol version.
Definition: http_types.h:14
void requireHttpVersion(const HttpVersion &version)
Specifies HTTP version allowed.
Definition: http_message.cc:24
HttpHeaderPtr getHeader(const std::string &header_name) const
Returns object encapsulating HTTP header.
Definition: http_message.cc:60
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual ~HttpMessage()
Destructor.
Definition: http_message.cc:20
bool created_
Flag indicating whether create was called.
Definition: http_message.h:253
HttpHeaderMap required_headers_
Map holding required HTTP headers.
Definition: http_message.h:250
Direction
Specifies the direction of the HTTP message.
Definition: http_message.h:66
HttpVersion getHttpVersion() const
Returns HTTP version number (major and minor).
Definition: http_message.cc:54
int version()
returns Kea hooks version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
HttpVersion http_version_
HTTP version numbers.
Definition: http_message.h:238
std::set< HttpVersion > required_versions_
Set of required HTTP versions.
Definition: http_message.h:235
void requireHeader(const std::string &header_name)
Specifies a required HTTP header for the HTTP message.
Definition: http_message.cc:29
HttpHeaderMap headers_
Parsed HTTP headers.
Definition: http_message.h:259
void checkCreated() const
Checks if the create was called.
Definition: http_message.cc:90
Represents HTTP header including a header name and value.
Definition: http_header.h:20
std::string getLowerCaseName() const
Returns lower case header name.
Definition: http_header.cc:34
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
uint64_t getHeaderValueAsUint64(const std::string &header_name) const
Returns a value of the specified HTTP header as number.
Definition: http_message.cc:79
HttpMessage(const Direction &direction)
Constructor.
Definition: http_message.cc:14
bool requiresBody() const
Checks if the body is required for the HTTP message.
Definition: http_message.cc:44