Aktualizr
C++ SOTA Client
httpinterface.h
1 #ifndef HTTPINTERFACE_H_
2 #define HTTPINTERFACE_H_
3 
4 #include <future>
5 #include <string>
6 #include <utility>
7 
8 #include <curl/curl.h>
9 #include "json/json.h"
10 
11 #include "libaktualizr/types.h"
12 #include "logging/logging.h"
13 #include "utilities/utils.h"
14 
15 using CurlHandler = std::shared_ptr<CURL>;
16 
17 struct HttpResponse {
18  HttpResponse() = default;
19  HttpResponse(std::string body_in, const long http_status_code_in, // NOLINT(google-runtime-int)
20  CURLcode curl_code_in, std::string error_message_in)
21  : body(std::move(body_in)),
22  http_status_code(http_status_code_in),
23  curl_code(curl_code_in),
24  error_message(std::move(error_message_in)) {}
25  std::string body;
26  long http_status_code{0}; // NOLINT(google-runtime-int)
27  CURLcode curl_code{CURLE_OK};
28  std::string error_message;
29  bool isOk() const { return (curl_code == CURLE_OK && http_status_code >= 200 && http_status_code < 400); }
30  bool wasInterrupted() const { return curl_code == CURLE_ABORTED_BY_CALLBACK; };
31  std::string getStatusStr() const {
32  return std::to_string(curl_code) + " " + error_message + " HTTP " + std::to_string(http_status_code);
33  }
34 
35  Json::Value getJson() const { return Utils::parseJSON(body); }
36 };
37 
39  public:
40  HttpInterface() = default;
41  virtual ~HttpInterface() = default;
42  virtual HttpResponse get(const std::string &url, int64_t maxsize) = 0;
43  virtual HttpResponse post(const std::string &url, const std::string &content_type, const std::string &data) = 0;
44  virtual HttpResponse post(const std::string &url, const Json::Value &data) = 0;
45  virtual HttpResponse put(const std::string &url, const std::string &content_type, const std::string &data) = 0;
46  virtual HttpResponse put(const std::string &url, const Json::Value &data) = 0;
47 
48  virtual HttpResponse download(const std::string &url, curl_write_callback write_cb,
49  curl_xferinfo_callback progress_cb, void *userp, curl_off_t from) = 0;
50  virtual std::future<HttpResponse> downloadAsync(const std::string &url, curl_write_callback write_cb,
51  curl_xferinfo_callback progress_cb, void *userp, curl_off_t from,
52  CurlHandler *easyp) = 0;
53  virtual void setCerts(const std::string &ca, CryptoSource ca_source, const std::string &cert,
54  CryptoSource cert_source, const std::string &pkey, CryptoSource pkey_source) = 0;
55  static constexpr int64_t kNoLimit = 0; // no limit the size of downloaded data
56  static constexpr int64_t kPostRespLimit = 64 * 1024;
57  static constexpr int64_t kPutRespLimit = 64 * 1024;
58 };
59 
60 #endif // HTTPINTERFACE_H_
types.h
HttpInterface
Definition: httpinterface.h:38
data
General data structures.
Definition: types.h:217
HttpResponse
Definition: httpinterface.h:17