1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <http/http_message.h>
namespace isc {
namespace http {
HttpMessage::HttpMessage(const HttpMessage::Direction& direction)
: direction_(direction), required_versions_(),
http_version_(HttpVersion::HTTP_10()), required_headers_(),
created_(false), finalized_(false), headers_() {
}
HttpMessage::~HttpMessage() {
}
void
HttpMessage::requireHttpVersion(const HttpVersion& version) {
required_versions_.insert(version);
}
void
HttpMessage::requireHeader(const std::string& header_name) {
// Empty value denotes that the header is required but no specific
// value is expected.
HttpHeaderPtr hdr(new HttpHeader(header_name));
required_headers_[hdr->getLowerCaseName()] = hdr;
}
void
HttpMessage::requireHeaderValue(const std::string& header_name,
const std::string& header_value) {
HttpHeaderPtr hdr(new HttpHeader(header_name, header_value));
required_headers_[hdr->getLowerCaseName()] = hdr;
}
bool
HttpMessage::requiresBody() const {
// If Content-Length is required the body must exist too. There may
// be probably some cases when Content-Length is not provided but
// the body is provided. But, probably not in our use cases.
// Use lower case header name because this is how it is indexed in
// the storage.
return (required_headers_.find("content-length") != required_headers_.end());
}
HttpVersion
HttpMessage::getHttpVersion() const {
checkCreated();
return (http_version_);
}
HttpHeaderPtr
HttpMessage::getHeader(const std::string& header_name) const {
checkCreated();
HttpHeader hdr(header_name);
auto header_it = headers_.find(hdr.getLowerCaseName());
if (header_it != headers_.end()) {
return (header_it->second);
}
isc_throw(HttpMessageNonExistingHeader, header_name << " HTTP header"
" not found in the request");
}
std::string
HttpMessage::getHeaderValue(const std::string& header_name) const {
return (getHeader(header_name)->getValue());
}
uint64_t
HttpMessage::getHeaderValueAsUint64(const std::string& header_name) const {
try {
return (getHeader(header_name)->getUint64Value());
} catch (const std::exception& ex) {
// The specified header does exist, but the value is not a number.
isc_throw(HttpMessageError, ex.what());
}
}
void
HttpMessage::checkCreated() const {
if (!created_) {
isc_throw(HttpMessageError, "unable to retrieve values of HTTP"
" message because the HttpMessage::create() must be"
" called first. This is a programmatic error");
}
}
void
HttpMessage::checkFinalized() const {
if (!finalized_) {
isc_throw(HttpMessageError, "unable to retrieve body of HTTP"
" message because the HttpMessage::finalize() must be"
" called first. This is a programmatic error");
}
}
} // end of namespace isc::http
} // end of namespace isc
|