Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/edhttp
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Implementation of the URI validator.
21 : *
22 : * The URI validator allows us to check the input as an URI address.
23 : */
24 :
25 : // self
26 : //
27 : #include "edhttp/validator_uri.h"
28 :
29 : #include "edhttp/uri.h"
30 :
31 :
32 : // cppthread
33 : //
34 : #include <cppthread/log.h>
35 :
36 :
37 : // last include
38 : //
39 : #include <snapdev/poison.h>
40 :
41 :
42 :
43 :
44 : namespace edhttp
45 : {
46 :
47 :
48 :
49 : namespace
50 : {
51 :
52 :
53 :
54 2 : class validator_uri_factory
55 : : public advgetopt::validator_factory
56 : {
57 : public:
58 2 : validator_uri_factory()
59 2 : {
60 2 : advgetopt::validator::register_validator(*this);
61 2 : }
62 :
63 4 : virtual std::string get_name() const override
64 : {
65 4 : return std::string("uri");
66 : }
67 :
68 9 : virtual std::shared_ptr<advgetopt::validator> create(advgetopt::string_list_t const & data) const override
69 : {
70 9 : return std::make_shared<validator_uri>(data);
71 : }
72 : };
73 :
74 2 : validator_uri_factory g_validator_uri_factory;
75 :
76 :
77 :
78 : } // no name namespace
79 :
80 :
81 :
82 :
83 :
84 9 : validator_uri::validator_uri(advgetopt::string_list_t const & param_list)
85 : {
86 18 : for(auto const & p : param_list)
87 : {
88 9 : if(p == "accept_ip" || p == "accept-ip")
89 : {
90 2 : f_accept_ip = true;
91 : }
92 7 : else if(p == "accept_path" || p == "accept-path")
93 : {
94 2 : f_accept_path = true;
95 : }
96 5 : else if(p == "refuse_ip" || p == "refuse-ip")
97 : {
98 2 : f_accept_ip = false;
99 : }
100 3 : else if(p == "refuse_path" || p == "refuse-path")
101 : {
102 2 : f_accept_path = false;
103 : }
104 : else
105 : {
106 2 : cppthread::log << cppthread::log_level_t::error
107 1 : << "validator_uri(): unknown parameter \""
108 1 : << p
109 1 : << "\"."
110 2 : << cppthread::end;
111 : }
112 : }
113 9 : }
114 :
115 :
116 : /** \brief Return the name of this validator.
117 : *
118 : * This function returns "uri".
119 : *
120 : * \return "uri".
121 : */
122 9 : std::string validator_uri::name() const
123 : {
124 9 : return std::string("uri");
125 : }
126 :
127 :
128 : /** \brief Check the value to make sure URIs are considered valid.
129 : *
130 : * This function is used to verify the value for a valid URI.
131 : *
132 : * \param[in] value The value to be validated.
133 : *
134 : * \return true on a match.
135 : */
136 81 : bool validator_uri::validate(std::string const & value) const
137 : {
138 162 : uri u;
139 162 : return u.set_uri(value, f_accept_path, f_accept_ip);
140 : }
141 :
142 :
143 :
144 6 : } // namespace edhttp
145 : // vim: ts=4 sw=4 et
|