File: urlparm.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (202 lines) | stat: -rw-r--r-- 5,404 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#ifndef OPENVPN_HTTP_URLPARM_H
#define OPENVPN_HTTP_URLPARM_H

#include <string>
#include <sstream>
#include <vector>

#include <openvpn/common/number.hpp>
#include <openvpn/http/urlencode.hpp>
#include <openvpn/http/webexcept.hpp>
#include <openvpn/common/string.hpp>

namespace openvpn::URL {
OPENVPN_EXCEPTION(url_parameter_error);

struct Parm
{
    Parm()
    {
    }

    Parm(const std::string &name_arg, const std::string &value_arg)
        : name(name_arg), value(value_arg)
    {
    }

    std::string to_string() const
    {
        std::ostringstream out;
        out << name << '=' << value;
        return out.str();
    }

    std::string name;
    std::string value;
};

class ParmList : public std::vector<Parm>
{
  public:
    ParmList(const std::string &uri)
    {
        try
        {
            const std::vector<std::string> req_parms = string::split(uri, '?', 1);
            request_ = req_parms[0];
            if (req_parms.size() == 2)
            {
                const std::vector<std::string> kv_list = string::split(req_parms[1], '&');
                for (auto &kvstr : kv_list)
                {
                    const std::vector<std::string> kv = string::split(kvstr, '=', 1);
                    Parm p;
                    p.name = decode(kv[0]);
                    if (kv.size() == 2)
                        p.value = decode(kv[1]);
                    push_back(std::move(p));
                }
            }
        }
        catch (const std::exception &e)
        {
            throw HTTP::WebException(HTTP::Status::BadRequest, e.what());
        }
    }

    const Parm *get(const std::string &key) const
    {
        for (auto &p : *this)
        {
            if (key == p.name)
                return &p;
        }
        return nullptr;
    }

    std::string get_value(const std::string &key) const
    {
        const Parm *p = get(key);
        if (p)
            return p->value;
        else
            return "";
    }

    const std::string &get_value_required(const std::string &key) const
    {
        const Parm *p = get(key);
        if (p)
            return p->value;
        else
            throw url_parameter_error(key + " : not found");
    }

    template <typename T>
    T get_num(const std::string &name, const std::string &short_name, const T default_value) const
    {
        const Parm *p = get(name);
        if (!p && !short_name.empty())
            p = get(short_name);
        if (p)
            return parse_number_throw<T>(p->value, name);
        else
            return default_value;
    }

    template <typename T>
    T get_num_required(const std::string &name, const std::string &short_name) const
    {
        const Parm *p = get(name);
        if (!p && !short_name.empty())
            p = get(short_name);
        if (!p)
            throw url_parameter_error(name + " : not found");
        return parse_number_throw<T>(p->value, name);
    }

    bool get_bool(const std::string &name, const std::string &short_name, const bool default_value) const
    {
        const Parm *p = get(name);
        if (!p && !short_name.empty())
            p = get(short_name);
        if (p)
        {
            if (p->value == "0")
                return false;
            else if (p->value == "1")
                return true;
            else
                throw url_parameter_error(name + ": parameter must be 0 or 1");
        }
        else
            return default_value;
    }

    std::string get_string(const std::string &name, const std::string &short_name) const
    {
        const Parm *p = get(name);
        if (!p && !short_name.empty())
            p = get(short_name);
        if (p)
            return p->value;
        else
            return "";
    }

    std::string get_string_required(const std::string &name, const std::string &short_name) const
    {
        const Parm *p = get(name);
        if (!p && !short_name.empty())
            p = get(short_name);
        if (!p)
            throw url_parameter_error(name + " : not found");
        return p->value;
    }

    std::string to_string() const
    {
        std::ostringstream out;
        for (size_t i = 0; i < size(); ++i)
            out << '[' << i << "] " << (*this)[i].to_string() << std::endl;
        return out.str();
    }

    std::string request(const bool remove_leading_slash) const
    {
        std::string ret = request_;
        if (remove_leading_slash)
        {
            if (ret.length() > 0 && ret[0] == '/')
                ret = ret.substr(1);
            else
                throw HTTP::WebException(HTTP::Status::BadRequest, "URI missing leading slash");
        }
        if (ret.empty())
            throw HTTP::WebException(HTTP::Status::BadRequest, "URI resource is empty");
        return ret;
    }

    const std::string &request() const
    {
        return request_;
    }

  private:
    std::string request_;
};

} // namespace openvpn::URL

#endif