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 <atomic>
39 : #include <memory>
40 : #include <regex>
41 :
42 : #include <nlohmann/json.hpp>
43 :
44 : #include <JsonSchema.hpp>
45 : #include <AdminSchemas.hpp>
46 :
47 :
48 : namespace h2agent
49 : {
50 : namespace model
51 : {
52 :
53 : class AdminServerMatchingData
54 : {
55 : public:
56 : AdminServerMatchingData();
57 307 : ~AdminServerMatchingData() = default;
58 :
59 : // Algorithm type
60 : enum AlgorithmType { FullMatching = 0, FullMatchingRegexReplace, RegexMatching };
61 : // UriPathQueryParameters filter type
62 : enum UriPathQueryParametersFilterType { Sort = 0, PassBy, Ignore };
63 : // UriPathQueryParameters separator type
64 : enum UriPathQueryParametersSeparatorType { Ampersand = 0, Semicolon };
65 :
66 : // Load result
67 : enum LoadResult { Success = 0, BadSchema, BadContent };
68 :
69 : // Immutable configuration snapshot (thread-safe by design)
70 : struct Config {
71 : AlgorithmType algorithm{FullMatching};
72 : std::regex rgx{};
73 : std::string fmt{};
74 : UriPathQueryParametersFilterType uri_path_query_parameters_filter{Sort};
75 : UriPathQueryParametersSeparatorType uri_path_query_parameters_separator{Ampersand};
76 : nlohmann::json json{};
77 : };
78 :
79 : /**
80 : * Returns an immutable configuration snapshot.
81 : * Callers hold the shared_ptr for the duration of use,
82 : * so concurrent load() calls cannot invalidate it.
83 : */
84 176 : std::shared_ptr<const Config> getConfig() const {
85 176 : return std::atomic_load(&config_);
86 : }
87 :
88 : // Convenience getters (safe for scalar/by-value access)
89 172 : AlgorithmType getAlgorithm() const { return getConfig()->algorithm; }
90 1 : UriPathQueryParametersFilterType getUriPathQueryParametersFilter() const { return getConfig()->uri_path_query_parameters_filter; }
91 1 : UriPathQueryParametersSeparatorType getUriPathQueryParametersSeparator() const { return getConfig()->uri_path_query_parameters_separator; }
92 1 : nlohmann::json getJson() const { return getConfig()->json; }
93 :
94 : /**
95 : * Loads server matching operation data
96 : *
97 : * @param j Json document from operation body request
98 : *
99 : * @return Load operation result
100 : */
101 : LoadResult load(const nlohmann::json &j);
102 :
103 : /**
104 : * Gets matching schema
105 : */
106 1 : const h2agent::jsonschema::JsonSchema& getSchema() const {
107 1 : return server_matching_schema_;
108 : }
109 :
110 : private:
111 : h2agent::jsonschema::JsonSchema server_matching_schema_{};
112 : std::shared_ptr<const Config> config_;
113 : };
114 :
115 : }
116 : }
117 :
|