17 #ifndef MASTODONPP_CURL_WRAPPER_HPP
18 #define MASTODONPP_CURL_WRAPPER_HPP
20 #include "curl/curl.h"
25 #include <string_view>
32 using std::string_view;
125 [[nodiscard]]
inline string escape_url(
const string_view url)
const
127 char *cbuf{curl_easy_escape(_connection, url.data(),
128 static_cast<int>(url.size()))};
148 char *cbuf{curl_easy_unescape(_connection, url.data(),
149 static_cast<int>(url.size()),
nullptr)};
163 string_view access_token,
164 string_view cainfo, string_view useragent);
197 return _curl_buffer_body;
211 _stream_cancelled =
true;
224 virtual void set_proxy(string_view proxy);
248 CURL *_connection{
nullptr};
249 char _curl_buffer_error[CURL_ERROR_SIZE]{
'\0'};
250 string _curl_buffer_headers;
251 string _curl_buffer_body;
252 bool _stream_cancelled{
false};
266 size_t writer_body(
char *data,
size_t size,
size_t nmemb);
276 static inline size_t writer_body_wrapper(
char *data,
size_t sz,
277 size_t nmemb,
void *f)
279 return static_cast<CURLWrapper *
>(f)->writer_body(data, sz, nmemb);
283 size_t writer_header(
char *data,
size_t size,
size_t nmemb);
286 static inline size_t writer_header_wrapper(
char *data,
size_t sz,
287 size_t nmemb,
void *f)
289 return static_cast<CURLWrapper *
>(f)->writer_header(data, sz, nmemb);
299 int progress(
void *clientp, curl_off_t dltotal, curl_off_t dlnow,
300 curl_off_t ultotal, curl_off_t ulnow)
const;
303 static inline int progress_wrapper(
void *f,
void *clientp,
304 curl_off_t dltotal, curl_off_t dlnow,
305 curl_off_t ultotal, curl_off_t ulnow)
307 return static_cast<CURLWrapper *
>(f)->progress(clientp, dltotal, dlnow,
328 static bool replace_parameter_in_uri(
string &uri,
339 static void add_parameters_to_uri(
string &uri,
351 static void add_mime_part(curl_mime *mime, string_view name,
368 curl_mime *parameters_to_curl_mime(
string &uri,
Handles the details of network connections.
Definition: curl_wrapper.hpp:58
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:203
string & get_buffer()
Returns a reference to the buffer libcurl writes into.
Definition: curl_wrapper.hpp:195
string escape_url(const string_view url) const
URL encodes the given string.
Definition: curl_wrapper.hpp:125
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:229
CURL * get_curl_easy_handle()
Returns pointer to the CURL easy handle.
Definition: curl_wrapper.hpp:108
virtual ~CURLWrapper() noexcept
Cleans up curl and connection.
Definition: curl_wrapper.cpp:68
CURLWrapper(CURLWrapper &&other) noexcept=delete
Move constructor.
CURLWrapper()
Initializes curl and sets up connection.
Definition: curl_wrapper.cpp:58
void cancel_stream()
Cancel the stream.
Definition: curl_wrapper.hpp:209
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:240
answer_type make_request(const http_method &method, string uri, const parametermap ¶meters)
Make a HTTP request.
Definition: curl_wrapper.cpp:80
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:276
string unescape_url(const string_view url) const
URL decodes the given string.
Definition: curl_wrapper.hpp:146
virtual void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:266
mutex _buffer_mutex
Mutex for get_buffer a.k.a. _curl_buffer_body.
Definition: curl_wrapper.hpp:175
C++ wrapper for the Mastodon API.
Definition: api.hpp:25
pair< string_view, variant< string_view, vector< string_view > >> parameterpair
A single parameter of a parametermap.
Definition: types.hpp:72
http_method
The HTTP method.
Definition: curl_wrapper.hpp:40
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
Return type for Requests.
Definition: types.hpp:80