mastodonpp  0.4.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 "types.hpp"
21 
22 #include "curl/curl.h"
23 
24 #include <mutex>
25 #include <string>
26 #include <string_view>
27 
28 namespace mastodonpp
29 {
30 
31 using std::mutex;
32 using std::string;
33 using std::string_view;
34 
40 enum class http_method
41 {
42  GET,
43  POST,
44  PATCH,
45  PUT,
46  DELETE
47 };
48 
59 {
60 public:
71  CURLWrapper();
72 
74  CURLWrapper(const CURLWrapper &other) = delete;
75 
77  CURLWrapper(CURLWrapper &&other) noexcept = delete;
78 
88  virtual ~CURLWrapper() noexcept;
89 
91  CURLWrapper& operator=(const CURLWrapper &other) = delete;
92 
94  CURLWrapper& operator=(CURLWrapper &&other) noexcept = delete;
95 
105  inline CURL *get_curl_easy_handle()
106  {
107  return _connection;
108  }
109 
122  [[nodiscard]]
123  inline string escape_url(const string_view url) const
124  {
125  char *cbuf{curl_easy_escape(_connection, url.data(),
126  static_cast<int>(url.size()))};
127  string sbuf{cbuf};
128  curl_free(cbuf);
129  return sbuf;
130  }
131 
144  [[nodiscard]]
145  inline string unescape_url(const string_view url) const
146  {
147  char *cbuf{curl_easy_unescape(_connection, url.data(),
148  static_cast<int>(url.size()), nullptr)};
149  string sbuf{cbuf};
150  curl_free(cbuf);
151  return sbuf;
152  }
153 
161  void setup_connection_properties(string_view proxy,
162  string_view access_token,
163  string_view cainfo,
164  string_view useragent);
165 
166 protected:
176 
186  [[nodiscard]]
187  answer_type make_request(const http_method &method, string uri,
188  const parametermap &parameters);
189 
195  [[nodiscard]]
196  string &get_buffer()
197  {
198  return _curl_buffer_body;
199  }
200 
210  inline void cancel_stream()
211  {
212  _stream_cancelled = true;
213  }
214 
225  void set_proxy(string_view proxy);
226 
232  void set_access_token(string_view access_token);
233 
234 
240  void set_cainfo(string_view path);
241 
247  void set_useragent(string_view useragent);
248 
249 private:
250  CURL *_connection;
251  char _curl_buffer_error[CURL_ERROR_SIZE];
252  string _curl_buffer_headers;
253  string _curl_buffer_body;
254  bool _stream_cancelled;
255 
261  size_t writer_body(char *data, size_t size, size_t nmemb);
262 
271  static inline size_t writer_body_wrapper(char *data, size_t sz,
272  size_t nmemb, void *f)
273  {
274  return static_cast<CURLWrapper*>(f)->writer_body(data, sz, nmemb);
275  }
276 
278  size_t writer_header(char *data, size_t size, size_t nmemb);
279 
281  static inline size_t writer_header_wrapper(char *data, size_t sz,
282  size_t nmemb, void *f)
283  {
284  return static_cast<CURLWrapper*>(f)->writer_header(data, sz, nmemb);
285  }
286 
294  int progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
295  curl_off_t ultotal, curl_off_t ulnow);
296 
298  static inline int progress_wrapper(void *f, void *clientp,
299  curl_off_t dltotal, curl_off_t dlnow,
300  curl_off_t ultotal, curl_off_t ulnow)
301  {
302  return static_cast<CURLWrapper*>(f)->progress(clientp, dltotal, dlnow,
303  ultotal, ulnow);
304  }
305 
311  void setup_curl();
312 
323  bool replace_parameter_in_uri(string &uri, const parameterpair &parameter);
324 
333  void add_parameters_to_uri(string &uri, const parametermap &parameters);
334 
344  void add_mime_part(curl_mime *mime,
345  string_view name, string_view data) const;
346 
361  curl_mime *parameters_to_curl_mime(string &uri,
362  const parametermap &parameters);
363 };
364 
365 } // namespace mastodonpp
366 
367 #endif // MASTODONPP_CURL_WRAPPER_HPP
mastodonpp::http_method
http_method
The HTTP method.
Definition: curl_wrapper.hpp:40
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:105
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
mastodonpp::CURLWrapper::unescape_url
string unescape_url(const string_view url) const
URL decodes the given string .
Definition: curl_wrapper.hpp:145
mastodonpp
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
mastodonpp::CURLWrapper::buffer_mutex
mutex buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:175
mastodonpp::CURLWrapper::get_buffer
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:196
mastodonpp::CURLWrapper::set_proxy
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:216
mastodonpp::answer_type
Return type for Requests.
Definition: types.hpp:82
mastodonpp::parameterpair
pair< string_view, variant< string_view, vector< string_view > >> parameterpair
A single parameter of a parametermap.
Definition: types.hpp:72
mastodonpp::CURLWrapper::set_cainfo
void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:253
mastodonpp::CURLWrapper::operator=
CURLWrapper & operator=(const CURLWrapper &other)=delete
Copy assignment operator.
mastodonpp::CURLWrapper::set_useragent
void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:263
mastodonpp::CURLWrapper
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
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:67
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:210
mastodonpp::CURLWrapper::escape_url
string escape_url(const string_view url) const
URL encodes the given string.
Definition: curl_wrapper.hpp:123
mastodonpp::CURLWrapper::set_access_token
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:227
mastodonpp::CURLWrapper::setup_connection_properties
void setup_connection_properties(string_view proxy, string_view access_token, string_view cainfo, string_view useragent)
Set some properties of the connection.
Definition: curl_wrapper.cpp:190