Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 : #include <regex>
38 :
39 : #include <nlohmann/json.hpp>
40 :
41 : #include <ert/tracing/Logger.hpp>
42 :
43 : #include <AdminServerMatchingData.hpp>
44 :
45 : namespace h2agent
46 : {
47 : namespace model
48 : {
49 :
50 173 : AdminServerMatchingData::AdminServerMatchingData() {
51 173 : algorithm_ = FullMatching;
52 : //rgx_.clear();
53 173 : fmt_.clear();
54 173 : uri_path_query_parameters_filter_ = Sort;
55 173 : uri_path_query_parameters_separator_ = Ampersand;
56 173 : json_["algorithm"] = "FullMatching";
57 :
58 173 : server_matching_schema_.setJson(h2agent::adminSchemas::server_matching); // won't fail
59 173 : }
60 :
61 142 : AdminServerMatchingData::LoadResult AdminServerMatchingData::load(const nlohmann::json &j) {
62 :
63 142 : std::string error{};
64 142 : if (!server_matching_schema_.validate(j, error)) {
65 6 : return BadSchema;
66 : }
67 :
68 136 : write_guard_t guard(rw_mutex_);
69 :
70 : // Store whole document (useful for GET operation)
71 136 : json_ = j;
72 :
73 : // Mandatory
74 136 : auto algorithm_it = j.find("algorithm");
75 :
76 : // Optional
77 136 : bool hasRgx = false;
78 136 : bool hasFmt = false;
79 136 : auto rgx_it = j.find("rgx");
80 136 : if (rgx_it != j.end() && rgx_it->is_string()) {
81 6 : hasRgx = true;
82 : try {
83 7 : rgx_.assign(rgx_it->get<std::string>(), std::regex::optimize);
84 : }
85 1 : catch (std::regex_error &e) {
86 1 : ert::tracing::Logger::error(e.what(), ERT_FILE_LOCATION);
87 1 : return BadContent;
88 1 : }
89 : }
90 :
91 135 : auto fmt_it = j.find("fmt");
92 135 : if (fmt_it != j.end() && fmt_it->is_string()) {
93 4 : hasFmt = true;
94 4 : fmt_ = *fmt_it;
95 : }
96 :
97 135 : auto uriPathQueryParameters_it = j.find("uriPathQueryParameters");
98 135 : if (uriPathQueryParameters_it != j.end()) {
99 7 : auto filter_it = (*uriPathQueryParameters_it).find("filter"); // mandatory
100 7 : if (filter_it->is_string()) {
101 7 : if (*filter_it == "Sort") {
102 2 : uri_path_query_parameters_filter_ = Sort;
103 : }
104 5 : else if (*filter_it == "PassBy") {
105 2 : uri_path_query_parameters_filter_ = PassBy;
106 : }
107 3 : else if (*filter_it == "Ignore") {
108 3 : uri_path_query_parameters_filter_ = Ignore;
109 : }
110 : }
111 :
112 7 : auto separator_it = (*uriPathQueryParameters_it).find("separator"); // optional
113 7 : if (separator_it != (*uriPathQueryParameters_it).end() && separator_it->is_string()) {
114 2 : if (*separator_it == "Ampersand") {
115 1 : uri_path_query_parameters_separator_ = Ampersand;
116 : }
117 1 : else if (*separator_it == "Semicolon") {
118 1 : uri_path_query_parameters_separator_ = Semicolon;
119 : }
120 : }
121 : }
122 :
123 : // Checkings
124 135 : if (*algorithm_it == "FullMatching") {
125 122 : if (hasRgx || hasFmt) {
126 3 : ert::tracing::Logger::error("FullMatching does not allow rgx and/or fmt fields", ERT_FILE_LOCATION);
127 3 : return BadContent;
128 : }
129 119 : algorithm_ = FullMatching;
130 : }
131 13 : else if (*algorithm_it == "FullMatchingRegexReplace") {
132 3 : if (!hasRgx || !hasFmt) {
133 1 : ert::tracing::Logger::error("FullMatchingRegexReplace requires rgx and fmt fields", ERT_FILE_LOCATION);
134 1 : return BadContent;
135 : }
136 2 : algorithm_ = FullMatchingRegexReplace;
137 : }
138 10 : else if (*algorithm_it == "RegexMatching") {
139 10 : if (hasRgx || hasFmt) {
140 2 : ert::tracing::Logger::error("RegexMatching does not allow rgx and/or fmt fields", ERT_FILE_LOCATION);
141 2 : return BadContent;
142 : }
143 8 : algorithm_ = RegexMatching;
144 : }
145 :
146 :
147 129 : return Success;
148 142 : }
149 :
150 : }
151 : }
152 :
|