mastodonpp  0.0.0
curl_wrapper.hpp
1 /* This file is part of mastodonpp.
2  * Copyright © 2020 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODONPP_CURL_WRAPPER_HPP
18 #define MASTODONPP_CURL_WRAPPER_HPP
19 
20 #include "answer.hpp"
21 
22 #include "curl/curl.h"
23 
24 #include <map>
25 #include <mutex>
26 #include <string>
27 #include <string_view>
28 #include <variant>
29 #include <vector>
30 
31 namespace mastodonpp
32 {
33 
34 using std::map;
35 using std::mutex;
36 using std::string;
37 using std::string_view;
38 using std::variant;
39 using std::vector;
40 
46 enum class http_method
47 {
48  GET,
49  POST,
50  PATCH,
51  PUT,
52  DELETE
53 };
54 
69 using parametermap = map<string_view, variant<string_view, vector<string_view>>>;
70 
81 {
82 public:
93  CURLWrapper();
94 
96  CURLWrapper(const CURLWrapper &other) = delete;
97 
99  CURLWrapper(CURLWrapper &&other) noexcept = delete;
100 
110  virtual ~CURLWrapper() noexcept;
111 
113  CURLWrapper& operator=(const CURLWrapper &other) = delete;
114 
116  CURLWrapper& operator=(CURLWrapper &&other) noexcept = delete;
117 
127  inline CURL *get_curl_easy_handle()
128  {
129  return _connection;
130  }
131 
142  void set_proxy(string_view proxy);
143 
153  void cancel_stream();
154 
155 protected:
166 
176  [[nodiscard]]
177  answer_type make_request(const http_method &method, string uri,
178  const parametermap &parameters);
179 
185  [[nodiscard]]
186  string &get_buffer()
187  {
188  return _curl_buffer_body;
189  }
190 
191 private:
192  CURL *_connection;
193  char _curl_buffer_error[CURL_ERROR_SIZE];
194  string _curl_buffer_headers;
195  string _curl_buffer_body;
196  bool _stream_cancelled;
197 
203  size_t writer_body(char *data, size_t size, size_t nmemb);
204 
213  static inline size_t writer_body_wrapper(char *data, size_t sz,
214  size_t nmemb, void *f)
215  {
216  return static_cast<CURLWrapper*>(f)->writer_body(data, sz, nmemb);
217  }
218 
220  size_t writer_header(char *data, size_t size, size_t nmemb);
221 
223  static inline size_t writer_header_wrapper(char *data, size_t sz,
224  size_t nmemb, void *f)
225  {
226  return static_cast<CURLWrapper*>(f)->writer_header(data, sz, nmemb);
227  }
228 
236  int progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
237  curl_off_t ultotal, curl_off_t ulnow);
238 
240  static inline int progress_wrapper(void *f, void *clientp,
241  curl_off_t dltotal, curl_off_t dlnow,
242  curl_off_t ultotal, curl_off_t ulnow)
243  {
244  return static_cast<CURLWrapper*>(f)->progress(clientp, dltotal, dlnow,
245  ultotal, ulnow);
246  }
247 
253  void setup_curl();
254 
263  void add_parameters_to_uri(string &uri, const parametermap &parameters);
264 };
265 
266 } // namespace mastodonpp
267 
268 #endif // MASTODONPP_CURL_WRAPPER_HPP
mastodonpp::http_method
http_method
The HTTP method.
Definition: curl_wrapper.hpp:46
mastodonpp::CURLWrapper::CURLWrapper
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:42
mastodonpp::CURLWrapper::get_curl_easy_handle
CURL * get_curl_easy_handle()
Returns pointer to the CURL easy handle.
Definition: curl_wrapper.hpp:127
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: curl_wrapper.hpp:69
mastodonpp
C++ wrapper for the Mastodon API.
Definition: answer.cpp:22
mastodonpp::CURLWrapper::buffer_mutex
mutex buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:165
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:186
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:68
mastodonpp::answer_type
Return type for Requests.
Definition: answer.hpp:40
mastodonpp::CURLWrapper::operator=
CURLWrapper & operator=(const CURLWrapper &other)=delete
Copy assignment operator.
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:80
mastodonpp::CURLWrapper::make_request
answer_type make_request(const http_method &method, string uri, const parametermap &parameters)
Make a HTTP request.
Definition: curl_wrapper.cpp:83
mastodonpp::CURLWrapper::~CURLWrapper
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:56
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.cpp:78