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 <memory>
41 : #include <cstdint>
42 : #include <atomic>
43 : #include <chrono>
44 :
45 : #include <boost/asio.hpp>
46 :
47 : #include <JsonSchema.hpp>
48 :
49 : #include <ert/metrics/Metrics.hpp>
50 :
51 : #include <ert/http2comm/Http2Server.hpp>
52 :
53 : namespace h2agent
54 : {
55 : namespace model
56 : {
57 : class MockServerData;
58 : class MockClientData;
59 : //class Configuration;
60 : class GlobalVariable;
61 : //class FileManager;
62 : //class SocketManager;
63 : class AdminData;
64 : }
65 :
66 : namespace http2
67 : {
68 :
69 : class MyTrafficHttp2Server: public ert::http2comm::Http2Server
70 : {
71 : bool server_data_{};
72 : bool server_data_key_history_{};
73 : bool purge_execution_{};
74 :
75 : model::AdminData *admin_data_{};
76 : model::MockServerData *mock_server_events_data_{};
77 : model::MockClientData *mock_client_events_data_{};
78 :
79 : // metrics:
80 : ert::metrics::Metrics *metrics_{};
81 :
82 : ert::metrics::counter_t *provisioned_requests_successful_counter_{};
83 : ert::metrics::counter_t *provisioned_requests_failed_counter_{};
84 : ert::metrics::counter_t *purged_contexts_successful_counter_{};
85 : ert::metrics::counter_t *purged_contexts_failed_counter_{};
86 :
87 : std::atomic<int> max_busy_threads_{0};
88 : std::atomic<bool> receive_request_body_{true};
89 : std::atomic<bool> pre_reserve_request_body_{true};
90 :
91 : model::GlobalVariable* global_variable_ptr_{};
92 :
93 : public:
94 : MyTrafficHttp2Server(const std::string &name, size_t workerThreads, size_t maxWorkerThreads, boost::asio::io_context *timersIoContext, int maxQueueDispatcherSize);
95 60 : ~MyTrafficHttp2Server() {;}
96 :
97 : /**
98 : * Enable metrics
99 : *
100 : * @param metrics Optional metrics object to compute counters
101 : * @param source Source label for prometheus metrics. If missing, class name will be taken (even being redundant with
102 : * family name prefix as will ease metrics filtering anyway). A good source convention could be the process name and
103 : * the endpoint identification:
104 : *
105 : * - h2agent[_traffic_server]: optional endpoint category, as it would be deducted from family name
106 : * - h2agentB
107 : */
108 : void enableMyMetrics(ert::metrics::Metrics *metrics, const std::string &source = "");
109 :
110 : bool checkMethodIsAllowed(
111 : const nghttp2::asio_http2::server::request& req,
112 : std::vector<std::string>& allowedMethods);
113 :
114 : bool checkMethodIsImplemented(
115 : const nghttp2::asio_http2::server::request& req);
116 :
117 : bool checkHeaders(const nghttp2::asio_http2::server::request& req);
118 :
119 : bool receiveDataLen(const nghttp2::asio_http2::server::request& req); //virtual
120 : bool preReserveRequestBody(); //virtual
121 :
122 : void receive(const std::uint64_t &receptionId,
123 : const nghttp2::asio_http2::server::request& req,
124 : const std::string &requestBody,
125 : const std::chrono::microseconds &receptionTimestampUs,
126 : unsigned int& statusCode, nghttp2::asio_http2::header_map& headers,
127 : std::string& responseBody, unsigned int &responseDelayMs);
128 :
129 : void streamError(uint32_t errorCode, const std::string &serverName, const std::uint64_t &receptionId, const nghttp2::asio_http2::server::request &req);
130 : std::chrono::milliseconds responseDelayMs(const std::uint64_t &receptionId);
131 :
132 49 : void setAdminData(model::AdminData *p) {
133 49 : admin_data_ = p;
134 49 : }
135 40 : model::AdminData *getAdminData() const {
136 40 : return admin_data_;
137 : }
138 :
139 49 : void setMockServerData(model::MockServerData *p) {
140 49 : mock_server_events_data_ = p;
141 49 : }
142 24 : model::MockServerData *getMockServerData() const {
143 24 : return mock_server_events_data_;
144 : }
145 :
146 49 : void setMockClientData(model::MockClientData *p) {
147 49 : mock_client_events_data_ = p;
148 49 : }
149 : model::MockClientData *getMockClientData() const {
150 : return mock_client_events_data_;
151 : }
152 :
153 : std::string dataConfigurationAsJsonString() const;
154 : std::string configurationAsJsonString() const;
155 :
156 2 : void discardData(bool discard = true) {
157 2 : server_data_ = !discard;
158 2 : }
159 :
160 2 : void discardDataKeyHistory(bool discard = true) {
161 2 : server_data_key_history_ = !discard;
162 2 : }
163 :
164 2 : void disablePurge(bool disable = true) {
165 2 : purge_execution_ = !disable;
166 2 : }
167 :
168 2 : void setReceiveRequestBody(bool receive = true) {
169 2 : receive_request_body_.store(receive);
170 2 : }
171 :
172 2 : void setPreReserveRequestBody(bool preReserve = true) {
173 2 : pre_reserve_request_body_.store(preReserve);
174 2 : }
175 :
176 : // Used for re-schedule of delay timers through responseDelayMs()
177 49 : void setGlobalVariable(model::GlobalVariable *p) {
178 49 : global_variable_ptr_ = p;
179 49 : }
180 : model::GlobalVariable *getGlobalVariable() const {
181 : return global_variable_ptr_;
182 : }
183 : };
184 :
185 : }
186 : }
187 :
|