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 "utilities/types.h"
12 #include "utilities/utils.h"
13 
14 using CurlHandler = std::shared_ptr<CURL>;
15 
16 struct HttpResponse {
17  HttpResponse() = default;
18  HttpResponse(std::string body_in, const long http_status_code_in, // NOLINT(google-runtime-int)
19  CURLcode curl_code_in, std::string error_message_in)
20  : body(std::move(body_in)),
21  http_status_code(http_status_code_in),
22  curl_code(curl_code_in),
23  error_message(std::move(error_message_in)) {}
24  std::string body;
25  long http_status_code{0}; // NOLINT(google-runtime-int)
26  CURLcode curl_code{CURLE_OK};
27  std::string error_message;
28  bool isOk() { return (curl_code == CURLE_OK && http_status_code >= 200 && http_status_code < 400); }
29  std::string getStatusStr() {
30  return std::to_string(curl_code) + " " + error_message + " HTTP " + std::to_string(http_status_code);
31  }
32 
33  Json::Value getJson() { return Utils::parseJSON(body); }
34 };
35 
37  public:
38  HttpInterface() = default;
39  virtual ~HttpInterface() = default;
40  virtual HttpResponse get(const std::string &url, int64_t maxsize) = 0;
41  virtual HttpResponse post(const std::string &url, const Json::Value &data) = 0;
42  virtual HttpResponse put(const std::string &url, const Json::Value &data) = 0;
43 
44  virtual HttpResponse download(const std::string &url, curl_write_callback callback, void *userp, curl_off_t from) = 0;
45  virtual std::future<HttpResponse> downloadAsync(const std::string &url, curl_write_callback callback, void *userp,
46  curl_off_t from, CurlHandler *easyp) = 0;
47  virtual void setCerts(const std::string &ca, CryptoSource ca_source, const std::string &cert,
48  CryptoSource cert_source, const std::string &pkey, CryptoSource pkey_source) = 0;
49  static constexpr int64_t kNoLimit = 0; // no limit the size of downloaded data
50  static constexpr int64_t kPostRespLimit = 64 * 1024;
51  static constexpr int64_t kPutRespLimit = 64 * 1024;
52 };
53 
54 #endif // HTTPINTERFACE_H_
General data structures.
Definition: types.cc:44