Kea 2.7.6
cfg_http_header.cc
Go to the documentation of this file.
1// Copyright (C) 2024 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 <cc/simple_parser.h>
11
12using namespace isc;
13using namespace isc::data;
14using namespace isc::dhcp;
15using namespace std;
16
17namespace isc {
18namespace http {
19
24 map->set("name", Element::create(name_));
25 map->set("value", Element::create(value_));
26 return (map);
27}
28
32 for (auto const& header : headers) {
33 list->add(header.toElement());
34 }
35 return (list);
36}
37
38namespace {
39
40const SimpleKeywords HTTP_HEADER_KEYWORDS = {
41 { "name", Element::string },
42 { "value", Element::string },
43 { "user-context", Element::map }
44};
45
46const SimpleRequiredKeywords HTTP_HEADER_REQUIRED = { "name", "value" };
47
48CfgHttpHeader
49parseCfgHttpHeader(const ConstElementPtr& config) {
50 if (!config) {
51 // Should not happen.
52 isc_throw(DhcpConfigError, "null 'http-headers' item");
53 }
54 if (config->getType() != Element::map) {
55 isc_throw(DhcpConfigError, "invalid type specified for 'http-headers' "
56 "item (" << config->getPosition() << ")");
57 }
58 SimpleParser::checkKeywords(HTTP_HEADER_KEYWORDS, config);
59 SimpleParser::checkRequired(HTTP_HEADER_REQUIRED, config);
60 string name = config->get("name")->stringValue();
61 if (name.empty()) {
62 isc_throw(DhcpConfigError, "empty 'name' parameter ("
63 << config->get("name")->getPosition() << ")");
64 }
65 string value = config->get("value")->stringValue();
66 if (value.empty()) {
67 isc_throw(DhcpConfigError, "empty 'value' parameter ("
68 << config->get("value")->getPosition() << ")");
69 }
70 CfgHttpHeader header(name, value);
71 ConstElementPtr user_context = config->get("user-context");
72 if (user_context) {
73 header.setContext(user_context);
74 }
75 return (header);
76}
77
78}
79
82 CfgHttpHeaders headers;
83 if (!config) {
84 return (headers);
85 }
86 if (config->getType() != Element::list) {
87 isc_throw(DhcpConfigError, "invalid type specified for parameter "
88 "'http-headers' (" << config->getPosition() << ")");
89 }
90 for (auto const& item : config->listValue()) {
91 headers.push_back(parseCfgHttpHeader(item));
92 }
93 return (headers);
94}
95
96} // namespace http
97} // namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
static void checkRequired(const SimpleRequiredKeywords &required, isc::data::ConstElementPtr scope)
Checks that all required keywords are present.
To be removed. Please use ConfigError instead.
virtual isc::data::ElementPtr toElement() const
Unparses config HTTP header.
std::string name_
Header name.
std::string value_
Header value.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::vector< std::string > SimpleRequiredKeywords
This specifies all required keywords.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
ElementPtr CfgHttpHeaderstoElement(const CfgHttpHeaders &headers)
Unparse config HTTP headers.
CfgHttpHeaders parseCfgHttpHeaders(const ConstElementPtr &config)
Parse config HTTP headers.
std::vector< CfgHttpHeader > CfgHttpHeaders
Collection of config HTTP headers.
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.