17 #ifndef MASTODONPP_CURL_WRAPPER_HPP
18 #define MASTODONPP_CURL_WRAPPER_HPP
22 #include "curl/curl.h"
26 #include <string_view>
33 using std::string_view;
126 [[nodiscard]]
inline string escape_url(
const string_view url)
const
128 char *cbuf{curl_easy_escape(_connection, url.data(),
129 static_cast<int>(url.size()))};
149 char *cbuf{curl_easy_unescape(_connection, url.data(),
150 static_cast<int>(url.size()),
nullptr)};
164 string_view access_token,
166 string_view useragent);
200 return _curl_buffer_body;
214 _stream_cancelled =
true;
227 virtual void set_proxy(string_view proxy);
253 char _curl_buffer_error[CURL_ERROR_SIZE];
254 string _curl_buffer_headers;
255 string _curl_buffer_body;
256 bool _stream_cancelled;
270 size_t writer_body(
char *data,
size_t size,
size_t nmemb);
280 static inline size_t writer_body_wrapper(
char *data,
size_t sz,
281 size_t nmemb,
void *f)
283 return static_cast<CURLWrapper*
>(f)->writer_body(data, sz, nmemb);
287 size_t writer_header(
char *data,
size_t size,
size_t nmemb);
290 static inline size_t writer_header_wrapper(
char *data,
size_t sz,
291 size_t nmemb,
void *f)
293 return static_cast<CURLWrapper*
>(f)->writer_header(data, sz, nmemb);
303 int progress(
void *clientp, curl_off_t dltotal, curl_off_t dlnow,
304 curl_off_t ultotal, curl_off_t ulnow);
307 static inline int progress_wrapper(
void *f,
void *clientp,
308 curl_off_t dltotal, curl_off_t dlnow,
309 curl_off_t ultotal, curl_off_t ulnow)
311 return static_cast<CURLWrapper*
>(f)->progress(clientp, dltotal, dlnow,
332 static bool replace_parameter_in_uri(
string &uri,
343 static void add_parameters_to_uri(
string &uri,
355 static void add_mime_part(curl_mime *mime,
356 string_view name, string_view data);
372 curl_mime *parameters_to_curl_mime(
string &uri,
378 #endif // MASTODONPP_CURL_WRAPPER_HPP
http_method
The HTTP method.
Definition: curl_wrapper.hpp:40
mutex _buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:177
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:57
CURL * get_curl_easy_handle()
Returns pointer to the CURL easy handle.
Definition: curl_wrapper.hpp:109
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
string unescape_url(const string_view url) const
URL decodes the given string.
Definition: curl_wrapper.hpp:147
C++ wrapper for the Mastodon API.
Definition: api.cpp:19
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:198
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:234
Return type for Requests.
Definition: types.hpp:79
pair< string_view, variant< string_view, vector< string_view > >> parameterpair
A single parameter of a parametermap.
Definition: types.hpp:72
virtual void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:271
CURLWrapper & operator=(const CURLWrapper &other)=delete
Copy assignment operator.
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:281
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
answer_type make_request(const http_method &method, string uri, const parametermap ¶meters)
Make a HTTP request.
Definition: curl_wrapper.cpp:85
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:73
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:212
string escape_url(const string_view url) const
URL encodes the given string.
Definition: curl_wrapper.hpp:126
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:245
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:208