]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
rec: allow exception to proxy protocal usage for specific listen addresses
[thirdparty/pdns.git] / pdns / json.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "json.hh"
26 #include "namespaces.hh"
27 #include "misc.hh"
28
29 using json11::Json;
30
31 static inline int intFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const int default_value)
32 {
33 const auto& val = container[key];
34 if (val.is_number()) {
35 return val.int_value();
36 }
37
38 if (val.is_string()) {
39 try {
40 return std::stoi(val.string_value());
41 } catch (std::out_of_range&) {
42 throw JsonException("Key '" + string(key) + "' is out of range");
43 }
44 }
45
46 if (have_default) {
47 return default_value;
48 }
49 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
50 }
51
52 int intFromJson(const Json& container, const std::string& key)
53 {
54 return intFromJsonInternal(container, key, false, 0);
55 }
56
57 int intFromJson(const Json& container, const std::string& key, const int default_value)
58 {
59 return intFromJsonInternal(container, key, true, default_value);
60 }
61
62 static inline unsigned int uintFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const unsigned int default_value)
63 {
64 int intval = intFromJsonInternal(container, key, have_default, static_cast<int>(default_value));
65 if (intval >= 0) {
66 return intval;
67 }
68 throw JsonException("Key '" + string(key) + "' is not a positive Integer");
69 }
70
71 unsigned int uintFromJson(const Json& container, const std::string& key)
72 {
73 return uintFromJsonInternal(container, key, false, 0);
74 }
75
76 unsigned int uintFromJson(const Json& container, const std::string& key, const unsigned int default_value)
77 {
78 return uintFromJsonInternal(container, key, true, default_value);
79 }
80
81 static inline double doubleFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const double default_value)
82 {
83 const auto& val = container[key];
84 if (val.is_number()) {
85 return val.number_value();
86 }
87
88 if (val.is_string()) {
89 try {
90 return std::stod(val.string_value());
91 } catch (std::out_of_range&) {
92 throw JsonException("Value for key '" + string(key) + "' is out of range");
93 }
94 }
95
96 if (have_default) {
97 return default_value;
98 }
99 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
100 }
101
102 double doubleFromJson(const Json& container, const std::string& key)
103 {
104 return doubleFromJsonInternal(container, key, false, 0);
105 }
106
107 double doubleFromJson(const Json& container, const std::string& key, const double default_value)
108 {
109 return doubleFromJsonInternal(container, key, true, default_value);
110 }
111
112 string stringFromJson(const Json& container, const std::string &key)
113 {
114 const auto& val = container[key];
115 if (val.is_string()) {
116 return val.string_value();
117 }
118 throw JsonException("Key '" + string(key) + "' not present or not a String");
119 }
120
121 static inline bool boolFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const bool default_value)
122 {
123 const auto& val = container[key];
124 if (val.is_bool()) {
125 return val.bool_value();
126 }
127 if (have_default) {
128 return default_value;
129 }
130 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
131 }
132
133 bool boolFromJson(const Json& container, const std::string& key)
134 {
135 return boolFromJsonInternal(container, key, false, false);
136 }
137
138 bool boolFromJson(const Json& container, const std::string& key, const bool default_value)
139 {
140 return boolFromJsonInternal(container, key, true, default_value);
141 }