LCOV - code coverage report
Current view: top level - http2 - MyAdminHttp2Server.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 59.6 % 52 31
Test Date: 2025-02-14 17:40:40 Functions: 63.2 % 19 12

            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 <chrono>
      42              : 
      43              : #include <ert/metrics/Metrics.hpp>
      44              : 
      45              : #include <ert/http2comm/Http2Server.hpp>
      46              : #include <common.hpp>
      47              : 
      48              : namespace h2agent
      49              : {
      50              : namespace model
      51              : {
      52              : class Configuration;
      53              : class GlobalVariable;
      54              : class FileManager;
      55              : class SocketManager;
      56              : class AdminData;
      57              : class MockServerData;
      58              : class MockClientData;
      59              : }
      60              : 
      61              : namespace http2
      62              : {
      63              : 
      64              : bool statusCodeOK(int statusCode);
      65              : 
      66              : class MyTrafficHttp2Server;
      67              : 
      68              : class MyAdminHttp2Server: public ert::http2comm::Http2Server
      69              : {
      70              :     model::AdminData *admin_data_{};
      71              :     model::common_resources_t common_resources_{}; // general configuration, global variables, file manager and mock server events data
      72              : 
      73              :     h2agent::http2::MyTrafficHttp2Server *http2_server_{}; // used to set server-data configuration (discard contexts and/or history)
      74              : 
      75              :     // Client data storage:
      76              :     bool client_data_{};
      77              :     bool client_data_key_history_{};
      78              :     bool purge_execution_{};
      79              : 
      80              :     std::string getPathSuffix(const std::string &uriPath) const; // important: leading slash is omitted on extraction
      81              :     std::string buildJsonResponse(bool responseResult, const std::string &responseBody) const;
      82              : 
      83              :     void receiveNOOP(unsigned int& statusCode, nghttp2::asio_http2::header_map& headers, std::string &responseBody) const;
      84              :     void receivePOST(const std::string &pathSuffix, const std::string& requestBody, unsigned int& statusCode, nghttp2::asio_http2::header_map& headers, std::string &responseBody) const;
      85              :     void receiveGET(const std::string &uri, const std::string &pathSuffix, const std::string &queryParams, unsigned int& statusCode, nghttp2::asio_http2::header_map& headers, std::string &responseBody) const;
      86              :     void receiveDELETE(const std::string &pathSuffix, const std::string &queryParams, unsigned int& statusCode) const;
      87              :     void receivePUT(const std::string &pathSuffix, const std::string &queryParams, unsigned int& statusCode);
      88              : 
      89              :     void triggerClientOperation(const std::string &clientProvisionId, const std::string &queryParams, unsigned int& statusCode) const;
      90              : 
      91              : public:
      92              :     MyAdminHttp2Server(const std::string &name, size_t workerThreads);
      93              :     ~MyAdminHttp2Server();
      94              : 
      95              :     bool checkMethodIsAllowed(
      96              :         const nghttp2::asio_http2::server::request& req,
      97              :         std::vector<std::string>& allowedMethods);
      98              : 
      99              :     bool checkMethodIsImplemented(
     100              :         const nghttp2::asio_http2::server::request& req);
     101              : 
     102              :     bool checkHeaders(const nghttp2::asio_http2::server::request& req);
     103              : 
     104              :     void receive(const std::uint64_t &receptionId,
     105              :                  const nghttp2::asio_http2::server::request& req,
     106              :                  const std::string &requestBody,
     107              :                  const std::chrono::microseconds &receptionTimestampUs,
     108              :                  unsigned int& statusCode, nghttp2::asio_http2::header_map& headers,
     109              :                  std::string& responseBody, unsigned int &responseDelayMs);
     110              : 
     111              :     //bool receiveDataLen(const nghttp2::asio_http2::server::request& req); // virtual: default implementation (true) is required (request bodies are present in many operations).
     112              :     //bool preReserveRequestBody(); //virtual: default implementation (true) is acceptable for us (really, it does not matter).
     113              : 
     114           49 :     void setConfiguration(model::Configuration *p) {
     115           49 :         common_resources_.ConfigurationPtr = p;
     116           49 :     }
     117            1 :     model::Configuration *getConfiguration() const {
     118            1 :         return common_resources_.ConfigurationPtr;
     119              :     }
     120              : 
     121           49 :     void setGlobalVariable(model::GlobalVariable *p) {
     122           49 :         common_resources_.GlobalVariablePtr = p;
     123           49 :     }
     124           12 :     model::GlobalVariable *getGlobalVariable() const {
     125           12 :         return common_resources_.GlobalVariablePtr;
     126              :     }
     127              : 
     128           49 :     void setFileManager(model::FileManager *p) {
     129           49 :         common_resources_.FileManagerPtr = p;
     130           49 :     }
     131            1 :     model::FileManager *getFileManager() const {
     132            1 :         return common_resources_.FileManagerPtr;
     133              :     }
     134              : 
     135           49 :     void setSocketManager(model::SocketManager *p) {
     136           49 :         common_resources_.SocketManagerPtr = p;
     137           49 :     }
     138            0 :     model::SocketManager *getSocketManager() const {
     139            0 :         return common_resources_.SocketManagerPtr;
     140              :     }
     141              : 
     142            0 :     void setMetricsData(ert::metrics::Metrics *metrics, const ert::metrics::bucket_boundaries_t &responseDelaySecondsHistogramBucketBoundaries, const ert::metrics::bucket_boundaries_t &messageSizeBytesHistogramBucketBoundaries, const std::string &applicationName) {
     143            0 :         common_resources_.MetricsPtr = metrics;
     144            0 :         common_resources_.ResponseDelaySecondsHistogramBucketBoundaries = responseDelaySecondsHistogramBucketBoundaries;
     145            0 :         common_resources_.MessageSizeBytesHistogramBucketBoundaries = messageSizeBytesHistogramBucketBoundaries;
     146            0 :         common_resources_.ApplicationName = applicationName;
     147            0 :     }
     148              : 
     149          121 :     model::AdminData *getAdminData() const {
     150          121 :         return admin_data_;
     151              :     }
     152              : 
     153           49 :     void setMockServerData(model::MockServerData *p) {
     154           49 :         common_resources_.MockServerDataPtr = p;
     155           49 :     }
     156            0 :     model::MockServerData *getMockServerData() const {
     157            0 :         return common_resources_.MockServerDataPtr;
     158              :     }
     159              : 
     160           49 :     void setMockClientData(model::MockClientData *p) {
     161           49 :         common_resources_.MockClientDataPtr = p;
     162           49 :     }
     163            0 :     model::MockClientData *getMockClientData() const {
     164            0 :         return common_resources_.MockClientDataPtr;
     165              :     }
     166              : 
     167           49 :     void setHttp2Server(h2agent::http2::MyTrafficHttp2Server* ptr) {
     168           49 :         http2_server_ = ptr;
     169           49 :     }
     170              : 
     171            1 :     h2agent::http2::MyTrafficHttp2Server *getHttp2Server(void) const {
     172            1 :         return http2_server_;
     173              :     }
     174              : 
     175              :     int serverMatching(const nlohmann::json &configurationObject, std::string& log) const;
     176              :     int serverProvision(const nlohmann::json &configurationObject, std::string& log) const;
     177              :     int clientEndpoint(const nlohmann::json &configurationObject, std::string& log) const;
     178              :     int clientProvision(const nlohmann::json &configurationObject, std::string& log) const;
     179              :     int globalVariable(const nlohmann::json &configurationObject, std::string& log) const;
     180              :     int schema(const nlohmann::json &configurationObject, std::string& log) const;
     181              : 
     182              :     // Client data storage:
     183            0 :     void discardClientData(bool discard = true) {
     184            0 :         client_data_ = !discard;
     185            0 :     }
     186              : 
     187            0 :     void discardClientDataKeyHistory(bool discard = true) {
     188            0 :         client_data_key_history_ = !discard;
     189            0 :     }
     190              : 
     191            0 :     void disableClientPurge(bool disable = true) {
     192            0 :         purge_execution_ = !disable;
     193            0 :     }
     194              : 
     195              :     std::string clientDataConfigurationAsJsonString() const;
     196              : };
     197              : 
     198              : }
     199              : }
     200              : 
        

Generated by: LCOV version 2.0-1