Aktualizr
C++ SOTA Client
httpinterface.h
1 #ifndef HTTPINTERFACE_H_
2 #define HTTPINTERFACE_H_
3 
4 #include <string>
5 #include <utility>
6 
7 #include <curl/curl.h>
8 #include "json/json.h"
9 
10 #include "utilities/types.h"
11 #include "utilities/utils.h"
12 
13 struct HttpResponse {
14  HttpResponse(std::string body_in, const long http_status_code_in, CURLcode curl_code_in, // NOLINT
15  std::string error_message_in)
16  : body(std::move(body_in)),
17  http_status_code(http_status_code_in),
18  curl_code(curl_code_in),
19  error_message(std::move(error_message_in)) {}
20  std::string body;
21  long http_status_code; // NOLINT
22  CURLcode curl_code;
23  std::string error_message;
24  bool isOk() { return (curl_code == CURLE_OK && http_status_code >= 200 && http_status_code < 205); }
25  Json::Value getJson() { return Utils::parseJSON(body); }
26 };
27 
29  public:
30  HttpInterface() = default;
31  virtual ~HttpInterface() = default;
32  virtual HttpResponse get(const std::string &url, int64_t maxsize) = 0;
33  virtual HttpResponse post(const std::string &url, const Json::Value &data) = 0;
34  virtual HttpResponse put(const std::string &url, const Json::Value &data) = 0;
35 
36  virtual HttpResponse download(const std::string &url, curl_write_callback callback, void *userp) = 0;
37  virtual void setCerts(const std::string &ca, CryptoSource ca_source, const std::string &cert,
38  CryptoSource cert_source, const std::string &pkey, CryptoSource pkey_source) = 0;
39  static constexpr int64_t kNoLimit = 0; // no limit the size of downloaded data
40  static constexpr int64_t kPostRespLimit = 64 * 1024;
41  static constexpr int64_t kPutRespLimit = 64 * 1024;
42 };
43 
44 #endif // HTTPINTERFACE_H_
General data structures.
Definition: types.cc:6