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 <nlohmann/json.hpp>
39 :
40 : #include <Map.hpp>
41 : #include <JsonSchema.hpp>
42 :
43 :
44 : namespace h2agent
45 : {
46 : namespace model
47 : {
48 :
49 : class WaitManager;
50 : class SseManager;
51 :
52 : /**
53 : * Converts a json value to its string representation for variable substitution.
54 : * Strings are returned without quotes (backward compatible with former string storage).
55 : */
56 19 : inline std::string jsonToString(const nlohmann::json &j) {
57 19 : if (j.is_string()) return j.get<std::string>();
58 8 : if (j.is_null()) return "";
59 5 : return j.dump();
60 : }
61 :
62 : /**
63 : * This class stores the vault list.
64 : *
65 : * The vault is written on the TRAFFIC hot path (every 'vault.X' transformation
66 : * target, from both the client and server roles). Under high concurrency a
67 : * provision that touches many vault keys would otherwise serialize on a single
68 : * global mutex. It is therefore backed by an N-way SHARDED Map (see Map.hpp):
69 : * per-key writes lock only their shard, so independent keys do not contend.
70 : * Whole-map operations (getJson/asJsonString/clear) lock all shards, but those
71 : * are administrative paths (GET/DELETE /admin/v1/vault), not the hot path.
72 : */
73 : class Vault : public Map<std::string, nlohmann::json, VAULT_MUTEX_SHARDS>
74 : {
75 : h2agent::jsonschema::JsonSchema vault_schema_{};
76 : WaitManager *wait_manager_{};
77 : SseManager *sse_manager_{};
78 :
79 : public:
80 : Vault();
81 304 : ~Vault() = default;
82 :
83 5 : void setWaitManager(WaitManager *p) { wait_manager_ = p; }
84 0 : void setSseManager(SseManager *p) { sse_manager_ = p; }
85 :
86 : /**
87 : * Loads variable with a json value.
88 : *
89 : * @param variable variable name
90 : * @param value json value to store
91 : */
92 : void load(const std::string &variable, const nlohmann::json &value);
93 : void load(const std::string &variable, nlohmann::json &&value);
94 :
95 : /**
96 : * Loads a value at a specific path within an existing json variable.
97 : * Creates the variable as an empty object if it does not exist.
98 : *
99 : * @param variable variable name
100 : * @param path json pointer path (e.g. "/request/headers/auth")
101 : * @param value json value to set at path
102 : */
103 : void loadAtPath(const std::string &variable, const std::string &path, const nlohmann::json &value);
104 :
105 : /**
106 : * Loads server data global operation data
107 : *
108 : * @param j Json document from operation body request
109 : *
110 : * @return Load operation result
111 : */
112 : bool loadJson(const nlohmann::json &j);
113 :
114 : /**
115 : * Json string representation for class information (json object)
116 : *
117 : * @return Json string representation ('{}' for empty object).
118 : */
119 : std::string asJsonString() const;
120 :
121 : /**
122 : * Builds json document for class information
123 : *
124 : * @return Json object
125 : */
126 : nlohmann::json getJson() const;
127 :
128 : /**
129 : * Gets vault schema
130 : */
131 1 : const h2agent::jsonschema::JsonSchema& getSchema() const {
132 1 : return vault_schema_;
133 : }
134 : };
135 :
136 : }
137 : }
138 :
|