mastodonpp  0.2.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 <utility>
29 #include <variant>
30 #include <vector>
31 
32 namespace mastodonpp
33 {
34 
35 using std::map;
36 using std::mutex;
37 using std::string;
38 using std::string_view;
39 using std::pair;
40 using std::variant;
41 using std::vector;
42 
48 enum class http_method
49 {
50  GET,
51  POST,
52  PATCH,
53  PUT,
54  DELETE
55 };
56 
76 using parametermap =
77  map<string_view, variant<string_view, vector<string_view>>>;
78 
84 using parameterpair =
85  pair<string_view, variant<string_view, vector<string_view>>>;
86 
97 {
98 public:
109  CURLWrapper();
110 
112  CURLWrapper(const CURLWrapper &other) = delete;
113 
115  CURLWrapper(CURLWrapper &&other) noexcept = delete;
116 
126  virtual ~CURLWrapper() noexcept;
127 
129  CURLWrapper& operator=(const CURLWrapper &other) = delete;
130 
132  CURLWrapper& operator=(CURLWrapper &&other) noexcept = delete;
133 
143  inline CURL *get_curl_easy_handle()
144  {
145  return _connection;
146  }
147 
158  void set_proxy(string_view proxy);
159 
160 protected:
170 
180  [[nodiscard]]
181  answer_type make_request(const http_method &method, string uri,
182  const parametermap &parameters);
183 
189  [[nodiscard]]
190  string &get_buffer()
191  {
192  return _curl_buffer_body;
193  }
194 
204  inline void cancel_stream()
205  {
206  _stream_cancelled = true;
207  }
208 
214  void set_access_token(const string_view access_token);
215 
216 private:
217  CURL *_connection;
218  char _curl_buffer_error[CURL_ERROR_SIZE];
219  string _curl_buffer_headers;
220  string _curl_buffer_body;
221  bool _stream_cancelled;
222 
228  size_t writer_body(char *data, size_t size, size_t nmemb);
229 
238  static inline size_t writer_body_wrapper(char *data, size_t sz,
239  size_t nmemb, void *f)
240  {
241  return static_cast<CURLWrapper*>(f)->writer_body(data, sz, nmemb);
242  }
243 
245  size_t writer_header(char *data, size_t size, size_t nmemb);
246 
248  static inline size_t writer_header_wrapper(char *data, size_t sz,
249  size_t nmemb, void *f)
250  {
251  return static_cast<CURLWrapper*>(f)->writer_header(data, sz, nmemb);
252  }
253 
261  int progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
262  curl_off_t ultotal, curl_off_t ulnow);
263 
265  static inline int progress_wrapper(void *f, void *clientp,
266  curl_off_t dltotal, curl_off_t dlnow,
267  curl_off_t ultotal, curl_off_t ulnow)
268  {
269  return static_cast<CURLWrapper*>(f)->progress(clientp, dltotal, dlnow,
270  ultotal, ulnow);
271  }
272 
278  void setup_curl();
279 
290  bool replace_parameter_in_uri(string &uri, const parameterpair &parameter);
291 
300  void add_parameters_to_uri(string &uri, const parametermap &parameters);
301 
311  void add_mime_part(curl_mime *mime,
312  string_view name, string_view data) const;
313 
328  curl_mime *parameters_to_curl_mime(string &uri,
329  const parametermap &parameters);
330 };
331 
332 } // namespace mastodonpp
333 
334 #endif // MASTODONPP_CURL_WRAPPER_HPP
mastodonpp::http_method
http_method
The HTTP method.
Definition: curl_wrapper.hpp:48
mastodonpp::CURLWrapper::set_access_token
void set_access_token(const string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:200
mastodonpp::CURLWrapper::CURLWrapper
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:41
mastodonpp::CURLWrapper::get_curl_easy_handle
CURL * get_curl_easy_handle()
Returns pointer to the CURL easy handle.
Definition: curl_wrapper.hpp:143
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: curl_wrapper.hpp:77
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:169
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:190
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:67
mastodonpp::answer_type
Return type for Requests.
Definition: answer.hpp:40
mastodonpp::parameterpair
pair< string_view, variant< string_view, vector< string_view > >> parameterpair
A single parameter of a parametermap.
Definition: curl_wrapper.hpp:85
mastodonpp::CURLWrapper::operator=
CURLWrapper & operator=(const CURLWrapper &other)=delete
Copy assignment operator.
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:96
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:77
mastodonpp::CURLWrapper::~CURLWrapper
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:55
mastodonpp::CURLWrapper::cancel_stream
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:204