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{
30 {
31 }
32
33 Parm(const std::string &name_arg, const std::string &value_arg)
34 : name(name_arg), value(value_arg)
35 {
36 }
37
38 std::string to_string() const
39 {
40 std::ostringstream out;
41 out << name << '=' << value;
42 return out.str();
43 }
44
45 std::string name;
46 std::string value;
47};
48
49class ParmList : public std::vector<Parm>
50{
51 public:
52 ParmList(const std::string &uri)
53 {
54 try
55 {
56 const std::vector<std::string> req_parms = string::split(uri, '?', 1);
57 request_ = req_parms[0];
58 if (req_parms.size() == 2)
59 {
60 const std::vector<std::string> kv_list = string::split(req_parms[1], '&');
61 for (auto &kvstr : kv_list)
62 {
63 const std::vector<std::string> kv = string::split(kvstr, '=', 1);
64 Parm p;
65 p.name = decode(kv[0]);
66 if (kv.size() == 2)
67 p.value = decode(kv[1]);
68 push_back(std::move(p));
69 }
70 }
71 }
72 catch (const std::exception &e)
73 {
75 }
76 }
77
78 const Parm *get(const std::string &key) const
79 {
80 for (auto &p : *this)
81 {
82 if (key == p.name)
83 return &p;
84 }
85 return nullptr;
86 }
87
88 std::string get_value(const std::string &key) const
89 {
90 const Parm *p = get(key);
91 if (p)
92 return p->value;
93 else
94 return "";
95 }
96
97 const std::string &get_value_required(const std::string &key) const
98 {
99 const Parm *p = get(key);
100 if (p)
101 return p->value;
102 else
103 throw url_parameter_error(key + " : not found");
104 }
105
106 template <typename T>
107 T get_num(const std::string &name, const std::string &short_name, const T default_value) const
108 {
109 const Parm *p = get(name);
110 if (!p && !short_name.empty())
111 p = get(short_name);
112 if (p)
113 return parse_number_throw<T>(p->value, name);
114 else
115 return default_value;
116 }
117
118 template <typename T>
119 T get_num_required(const std::string &name, const std::string &short_name) const
120 {
121 const Parm *p = get(name);
122 if (!p && !short_name.empty())
123 p = get(short_name);
124 if (!p)
125 throw url_parameter_error(name + " : not found");
126 return parse_number_throw<T>(p->value, name);
127 }
128
129 bool get_bool(const std::string &name, const std::string &short_name, const bool default_value) const
130 {
131 const Parm *p = get(name);
132 if (!p && !short_name.empty())
133 p = get(short_name);
134 if (p)
135 {
136 if (p->value == "0")
137 return false;
138 else if (p->value == "1")
139 return true;
140 else
141 throw url_parameter_error(name + ": parameter must be 0 or 1");
142 }
143 else
144 return default_value;
145 }
146
147 std::string get_string(const std::string &name, const std::string &short_name) const
148 {
149 const Parm *p = get(name);
150 if (!p && !short_name.empty())
151 p = get(short_name);
152 if (p)
153 return p->value;
154 else
155 return "";
156 }
157
158 std::string get_string_required(const std::string &name, const std::string &short_name) const
159 {
160 const Parm *p = get(name);
161 if (!p && !short_name.empty())
162 p = get(short_name);
163 if (!p)
164 throw url_parameter_error(name + " : not found");
165 return p->value;
166 }
167
168 std::string to_string() const
169 {
170 std::ostringstream out;
171 for (size_t i = 0; i < size(); ++i)
172 out << '[' << i << "] " << (*this)[i].to_string() << std::endl;
173 return out.str();
174 }
175
176 std::string request(const bool remove_leading_slash) const
177 {
178 std::string ret = request_;
179 if (remove_leading_slash)
180 {
181 if (ret.length() > 0 && ret[0] == '/')
182 ret = ret.substr(1);
183 else
184 throw HTTP::WebException(HTTP::Status::BadRequest, "URI missing leading slash");
185 }
186 if (ret.empty())
187 throw HTTP::WebException(HTTP::Status::BadRequest, "URI resource is empty");
188 return ret;
189 }
190
191 const std::string &request() const
192 {
193 return request_;
194 }
195
196 private:
197 std::string request_;
198};
199
200} // namespace openvpn::URL
201
202#endif
std::string get_string(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:147
std::string get_value(const std::string &key) const
Definition urlparm.hpp:88
const std::string & request() const
Definition urlparm.hpp:191
const std::string & get_value_required(const std::string &key) const
Definition urlparm.hpp:97
const Parm * get(const std::string &key) const
Definition urlparm.hpp:78
T get_num(const std::string &name, const std::string &short_name, const T default_value) const
Definition urlparm.hpp:107
ParmList(const std::string &uri)
Definition urlparm.hpp:52
std::string to_string() const
Definition urlparm.hpp:168
std::string get_string_required(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:158
T get_num_required(const std::string &name, const std::string &short_name) const
Definition urlparm.hpp:119
bool get_bool(const std::string &name, const std::string &short_name, const bool default_value) const
Definition urlparm.hpp:129
std::string request(const bool remove_leading_slash) const
Definition urlparm.hpp:176
#define OPENVPN_EXCEPTION(C)
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:492
std::string to_string() const
Definition urlparm.hpp:38
std::string name
Definition urlparm.hpp:45
Parm(const std::string &name_arg, const std::string &value_arg)
Definition urlparm.hpp:33
std::string value
Definition urlparm.hpp:46
server addresses push_back({address, port})
std::string ret
static std::stringstream out
Definition test_path.cpp:10