Aktualizr
C++ SOTA Client
httpclient.h
1 #ifndef HTTPCLIENT_H_
2 #define HTTPCLIENT_H_
3 
4 #include <curl/curl.h>
5 #include <gtest/gtest.h>
6 #include <memory>
7 #include "json/json.h"
8 
9 #include "httpinterface.h"
10 #include "logging/logging.h"
11 #include "utilities/utils.h"
12 
13 /**
14  * Helper class to manage curl_global_init/curl_global_cleanup calls
15  */
17  public:
18  CurlGlobalInitWrapper() { curl_global_init(CURL_GLOBAL_DEFAULT); }
19  ~CurlGlobalInitWrapper() { curl_global_cleanup(); }
20  CurlGlobalInitWrapper &operator=(const CurlGlobalInitWrapper &) = delete;
22  CurlGlobalInitWrapper &operator=(CurlGlobalInitWrapper &&) = delete;
24 };
25 
26 class HttpClient : public HttpInterface {
27  public:
28  HttpClient();
29  HttpClient(const HttpClient & /*curl_in*/);
30  ~HttpClient() override;
31  HttpResponse get(const std::string &url, int64_t maxsize) override;
32  HttpResponse post(const std::string &url, const Json::Value &data) override;
33  HttpResponse put(const std::string &url, const Json::Value &data) override;
34 
35  HttpResponse download(const std::string &url, curl_write_callback callback, void *userp) override;
36  void setCerts(const std::string &ca, CryptoSource ca_source, const std::string &cert, CryptoSource cert_source,
37  const std::string &pkey, CryptoSource pkey_source) override;
38  long http_code{}; // NOLINT
39 
40  private:
41  FRIEND_TEST(GetTest, download_speed_limit);
42  /**
43  * These are here to catch a common programming error where a Json::Value is
44  * implicitly constructed from a std::string. By having an private overload
45  * that takes string (and with no implementation), this will fail during
46  * compilation.
47  */
48  HttpResponse post(const std::string &url, std::string data);
49  HttpResponse put(const std::string &url, std::string data);
50 
51  static CurlGlobalInitWrapper manageCurlGlobalInit_;
52  CURL *curl;
53  curl_slist *headers;
54  HttpResponse perform(CURL *curl_handler, int retry_times, int64_t size_limit);
55  std::string user_agent;
56 
57  static CURLcode sslCtxFunction(CURL *handle, void *sslctx, void *parm);
58  std::unique_ptr<TemporaryFile> tls_ca_file;
59  std::unique_ptr<TemporaryFile> tls_cert_file;
60  std::unique_ptr<TemporaryFile> tls_pkey_file;
61  static const int RETRY_TIMES = 2;
62  static const long kSpeedLimitTimeInterval = 60L; // NOLINT
63  static const long kSpeedLimitBytesPerSec = 5000L; // NOLINT
64 
65  long speed_limit_time_interval_{kSpeedLimitTimeInterval}; // NOLINT
66  long speed_limit_bytes_per_sec_{kSpeedLimitBytesPerSec}; // NOLINT
67  void overrideSpeedLimitParams(long time_interval, long bytes_per_sec) { // NOLINT
68  speed_limit_time_interval_ = time_interval;
69  speed_limit_bytes_per_sec_ = bytes_per_sec;
70  }
71  bool pkcs11_key{false};
72  bool pkcs11_cert{false};
73 };
74 #endif
General data structures.
Definition: types.cc:6
Helper class to manage curl_global_init/curl_global_cleanup calls.
Definition: httpclient.h:16