OpenVPN 3 Core Library
Loading...
Searching...
No Matches
urlparm.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_HTTP_URLPARM_H
13#define OPENVPN_HTTP_URLPARM_H
14
15#include <string>
16#include <sstream>
17#include <vector>
18
23
24namespace openvpn::URL {
25OPENVPN_EXCEPTION(url_parameter_error);
26
27struct Parm
28{
29 Parm() = default;
30
31 Parm(const std::string &name_arg, const std::string &value_arg)
32 : name(name_arg), value(value_arg)
33 {
34 }
35
36 std::string to_string() const
37 {
38 std::ostringstream out;
39 out << name << '=' << value;
40 return out.str();
41 }
42
43 std::string name;
44 std::string value;
45};
46
47class ParmList : public std::vector<Parm>
48{
49 public:
50 ParmList(const std::string &uri)
51 {
52 try
53 {
54 const std::vector<std::string> req_parms = string::split(uri, '?', 1);
55 request_ = req_parms[0];
56 if (req_parms.size() == 2)
57 {
58 const std::vector<std::string> kv_list = string::split(req_parms[1], '&');
59 for (auto &kvstr : kv_list)
60 {
61 const std::vector<std::string> kv = string::split(kvstr, '=', 1);
62 Parm p;
63 p.name = decode(kv[0]);
64 if (kv.size() == 2)
65 p.value = decode(kv[1]);
66 push_back(std::move(p));
67 }
68 }
69 }
70 catch (const std::exception &e)
71 {
73 }
74 }
75
76 const Parm *get(const std::string &key) const
77 {
78 for (auto &p : *this)
79 {
80 if (key == p.name)
81 return &p;
82 }
83 return nullptr;
84 }
85
86 std::string get_value(const std::string &key) const
87 {
88 const Parm *p = get(key);
89 if (p)
90 return p->value;
91 else
92 return "";
93 }
94
95 const std::string &get_value_required(const std::string &key) const
96 {
97 const Parm *p = get(key);
98 if (p)
99 return p->value;
100 else
101 throw url_parameter_error(key + " : not found");
102 }
103
104 template <typename T>
105 T get_num(const std::string &name, const std::string &short_name, const T default_value) const
106 {
107 const Parm *p = get(name);
108 if (!p && !short_name.empty())
109 p = get(short_name);
110 if (p)
111 return parse_number_throw<T>(p->value, name);
112 else
113 return default_value;
114 }
115
116 template <typename T>
117 T get_num_required(const std::string &name, const std::string &short_name) const
118 {
119 const Parm *p = get(name);
120 if (!p && !short_name.empty())
121 p = get(short_name);
122 if (!p)
123 throw url_parameter_error(name + " : not found");
124 return parse_number_throw<T>(p->value, name);
125 }
126
127 bool get_bool(const std::string &name, const std::string &short_name, const bool default_value) const
128 {
129 const Parm *p = get(name);
130 if (!p && !short_name.empty())
131 p = get(short_name);
132 if (p)
133 {
134 if (p->value == "0")
135 return false;
136 else if (p->value == "1")
137 return true;
138 else
139 throw url_parameter_error(name + ": parameter must be 0 or 1");
140 }
141 else
142 return default_value;
143 }
144
145 std::string get_string(const std::string &name, const std::string &short_name) const
146 {
147 const Parm *p = get(name);
148 if (!p && !short_name.empty())
149 p = get(short_name);
150 if (p)
151 return p->value;
152 else
153 return "";
154 }
155
156 std::string get_string_required(const std::string &name, const std::string &short_name) const
157 {
158 const Parm *p = get(name);
159 if (!p && !short_name.empty())
160 p = get(short_name);
161 if (!p)
162 throw url_parameter_error(name + " : not found");
163 return p->value;
164 }
165
166 std::string to_string() const
167 {
168 std::ostringstream out;
169 for (size_t i = 0; i < size(); ++i)
170 out << '[' << i << "] " << (*this)[i].to_string() << "\n";
171 return out.str();
172 }
173
174 std::string request(const bool remove_leading_slash) const
175 {
176 std::string ret = request_;
177 if (remove_leading_slash)
178 {
179 if (ret.length() > 0 && ret[0] == '/')
180 ret = ret.substr(1);
181 else
182 throw HTTP::WebException(HTTP::Status::BadRequest, "URI missing leading slash");
183 }
184 if (ret.empty())
185 throw HTTP::WebException(HTTP::Status::BadRequest, "URI resource is empty");
186 return ret;
187 }
188
189 const std::string &request() const
190 {
191 return request_;
192 }
193
194 private:
195 std::string request_;
196};
197
198} // namespace openvpn::URL
199
200#endif
std::string get_string(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:145
std::string get_value(const std::string &key) const
Definition urlparm.hpp:86
const std::string & request() const
Definition urlparm.hpp:189
const std::string & get_value_required(const std::string &key) const
Definition urlparm.hpp:95
const Parm * get(const std::string &key) const
Definition urlparm.hpp:76
T get_num(const std::string &name, const std::string &short_name, const T default_value) const
Definition urlparm.hpp:105
ParmList(const std::string &uri)
Definition urlparm.hpp:50
std::string to_string() const
Definition urlparm.hpp:166
std::string get_string_required(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:156
T get_num_required(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:117
bool get_bool(const std::string &name, const std::string &short_name, const bool default_value) const
Definition urlparm.hpp:127
std::string request(const bool remove_leading_slash) const
Definition urlparm.hpp:174
#define OPENVPN_EXCEPTION(C)
Definition exception.hpp:99
std::string decode(const std::string &encoded)
Definition urlencode.hpp:45
std::vector< T > split(const T &str, const typename T::value_type sep, const int maxsplit=-1)
Definition string.hpp:440
std::string to_string() const
Definition urlparm.hpp:36
std::string name
Definition urlparm.hpp:43
Parm(const std::string &name_arg, const std::string &value_arg)
Definition urlparm.hpp:31
std::string value
Definition urlparm.hpp:44
server addresses push_back(address)
std::string ret
static std::stringstream out
Definition test_path.cpp:10