Aktualizr
C++ SOTA Client
httpclient.h
1 #ifndef HTTPCLIENT_H_
2 #define HTTPCLIENT_H_
3 
4 #include <future>
5 #include <memory>
6 
7 #include <curl/curl.h>
8 #include "gtest/gtest_prod.h"
9 #include "json/json.h"
10 
11 #include "httpinterface.h"
12 #include "logging/logging.h"
13 #include "utilities/utils.h"
14 
15 /**
16  * Helper class to manage curl_global_init/curl_global_cleanup calls
17  */
19  public:
20  CurlGlobalInitWrapper() { curl_global_init(CURL_GLOBAL_DEFAULT); }
21  ~CurlGlobalInitWrapper() { curl_global_cleanup(); }
22  CurlGlobalInitWrapper &operator=(const CurlGlobalInitWrapper &) = delete;
24  CurlGlobalInitWrapper &operator=(CurlGlobalInitWrapper &&) = delete;
26 };
27 
28 class HttpClient : public HttpInterface {
29  public:
30  HttpClient(const std::vector<std::string> *extra_headers = nullptr);
31  HttpClient(const HttpClient & /*curl_in*/);
32  ~HttpClient() override;
33  HttpResponse get(const std::string &url, int64_t maxsize) override;
34  HttpResponse post(const std::string &url, const std::string &content_type, const std::string &data) override;
35  HttpResponse post(const std::string &url, const Json::Value &data) override;
36  HttpResponse put(const std::string &url, const std::string &content_type, const std::string &data) override;
37  HttpResponse put(const std::string &url, const Json::Value &data) override;
38 
39  HttpResponse download(const std::string &url, curl_write_callback write_cb, curl_xferinfo_callback progress_cb,
40  void *userp, curl_off_t from) override;
41  std::future<HttpResponse> downloadAsync(const std::string &url, curl_write_callback write_cb,
42  curl_xferinfo_callback progress_cb, void *userp, curl_off_t from,
43  CurlHandler *easyp) override;
44  void setCerts(const std::string &ca, CryptoSource ca_source, const std::string &cert, CryptoSource cert_source,
45  const std::string &pkey, CryptoSource pkey_source) override;
46  bool updateHeader(const std::string &name, const std::string &value);
47 
48  private:
49  FRIEND_TEST(GetTest, download_speed_limit);
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  static curl_slist *curl_slist_dup(curl_slist *sl);
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(google-runtime-int)
63  static const long kSpeedLimitBytesPerSec = 5000L; // NOLINT(google-runtime-int)
64 
65  long speed_limit_time_interval_{kSpeedLimitTimeInterval}; // NOLINT(google-runtime-int)
66  long speed_limit_bytes_per_sec_{kSpeedLimitBytesPerSec}; // NOLINT(google-runtime-int)
67  void overrideSpeedLimitParams(long time_interval, long bytes_per_sec) { // NOLINT(google-runtime-int)
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
HttpInterface
Definition: httpinterface.h:38
data
General data structures.
Definition: types.h:217
HttpResponse
Definition: httpinterface.h:17
CurlGlobalInitWrapper
Helper class to manage curl_global_init/curl_global_cleanup calls.
Definition: httpclient.h:18
HttpClient
Definition: httpclient.h:28