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 : #pragma once
37 :
38 : #include <vector>
39 : #include <string>
40 : #include <mutex>
41 : #include <shared_mutex>
42 :
43 : #include <nlohmann/json.hpp>
44 :
45 : #include <Map.hpp>
46 : #include <AdminServerProvision.hpp>
47 :
48 : #include <JsonSchema.hpp>
49 : #include <AdminSchemas.hpp>
50 :
51 :
52 : namespace h2agent
53 : {
54 : namespace model
55 : {
56 :
57 : // Map key will be string which has a hash function.
58 : // We will agregate method and uri in a single string for that.
59 : class AdminServerProvisionData : public Map<admin_server_provision_key_t, std::shared_ptr<AdminServerProvision>>
60 : {
61 : public:
62 : AdminServerProvisionData();
63 154 : ~AdminServerProvisionData() = default;
64 :
65 : using KeyType = admin_server_provision_key_t;
66 : using ValueType = std::shared_ptr<AdminServerProvision>;
67 :
68 : // Load result
69 : enum LoadResult { Success = 0, BadSchema, BadContent };
70 :
71 : /**
72 : * Json string representation for class information (json array)
73 : *
74 : * @param ordered Print json array elements following the insertion order (false by default)
75 : * @param getUnused Print json array elements which was not used (false by default)
76 : *
77 : * @return Json string representation ('[]' for empty array).
78 : */
79 : std::string asJsonString(bool ordered = false, bool getUnused = false) const;
80 :
81 : /**
82 : * Loads server provision operation data
83 : *
84 : * @param j json document from operation body request
85 : * @param regexMatchingConfigured provision load depends on matching configuration (priority regexp)
86 : * @param cr common resources references (general configuration, global variables, file manager, mock server events data)
87 : *
88 : * @return Load operation result
89 : */
90 : LoadResult load(const nlohmann::json &j, bool regexMatchingConfigured, const common_resources_t &cr);
91 :
92 : /** Clears internal data (map and ordered keys vector)
93 : *
94 : * @return True if something was removed, false if already empty
95 : */
96 : bool clear();
97 :
98 : /**
99 : * Finds provision item for traffic reception. Previously, mock dynamic data should be checked to
100 : * know if current state exists for the reception.
101 : *
102 : * @param inState Request input state if proceeed
103 : * @param method Request method received
104 : * @param uri Request URI path received
105 : *
106 : * @return Provision information or null if missing
107 : */
108 : std::shared_ptr<AdminServerProvision> find(const std::string &inState, const std::string &method, const std::string &uri) const;
109 :
110 : /**
111 : * Finds provision item for traffic reception. Previously, mock dynamic data should be checked to
112 : * know if current state exists for the reception.
113 : * The algorithm is RegexMatching, so ordered search is applied.
114 : *
115 : * @param inState Request input state if proceeed
116 : * @param method Request method received
117 : * @param uri Request URI path received
118 : *
119 : * @return Provision information or null if missing
120 : */
121 : std::shared_ptr<AdminServerProvision> findRegexMatching(const std::string &inState, const std::string &method, const std::string &uri) const;
122 :
123 : /**
124 : * Gets provision schema
125 : */
126 1 : const h2agent::jsonschema::JsonSchema& getSchema() const {
127 1 : return server_provision_schema_;
128 : }
129 :
130 : private:
131 :
132 : std::vector<admin_server_provision_key_t> ordered_keys_{}; // this is used to keep the insertion order which shall be used in RegexMatching algorithm
133 : h2agent::jsonschema::JsonSchema server_provision_schema_{};
134 :
135 : LoadResult loadSingle(const nlohmann::json &j, bool regexMatchingConfigured, const common_resources_t &cr);
136 :
137 : mutable mutex_t rw_mutex_{}; // specific mutex (apart from Map's one) to protect own ordered_keys_ version of keys.
138 : };
139 :
140 : }
141 : }
142 :
|