mastodonpp  0.5.7
C++ wrapper for the Mastodon and Pleroma APIs.
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
mastodonpp::CURLWrapper Class Reference

Handles the details of network connections. More...

#include <mastodonpp/curl_wrapper.hpp>

Inheritance diagram for mastodonpp::CURLWrapper:
Inheritance graph
[legend]
Collaboration diagram for mastodonpp::CURLWrapper:
Collaboration graph
[legend]

Public Member Functions

 CURLWrapper ()
 Initializes curl and sets up connection. More...
 
 CURLWrapper (const CURLWrapper &)
 Copy constructor. Does the same as the Constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=delete
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=delete
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=delete
 Move assignment operator. More...
 
CURL * get_curl_easy_handle ()
 Returns pointer to the CURL easy handle. More...
 
string escape_url (const string_view url) const
 URL encodes the given string. More...
 
string unescape_url (const string_view url) const
 URL decodes the given string. More...
 
void setup_connection_properties (string_view proxy, string_view access_token, string_view cainfo, string_view useragent)
 Set some properties of the connection. More...
 

Protected Member Functions

answer_type make_request (const http_method &method, string uri, const parametermap &parameters)
 Make a HTTP request. More...
 
string & get_buffer ()
 Returns a reference to the buffer libcurl writes into. More...
 
void cancel_stream ()
 Cancel the stream. More...
 
virtual void set_proxy (string_view proxy)
 Set the proxy to use. More...
 
void set_access_token (string_view access_token)
 Set OAuth 2.0 Bearer Access Token. More...
 
virtual void set_cainfo (string_view path)
 Set path to Certificate Authority (CA) bundle. More...
 
virtual void set_useragent (string_view useragent)
 Sets the User-Agent. More...
 

Protected Attributes

mutex _buffer_mutex
 Mutex for get_buffer a.k.a. _curl_buffer_body. More...
 

Detailed Description

Handles the details of network connections.

You don't need to use this.

Since
0.1.0

Constructor & Destructor Documentation

◆ CURLWrapper() [1/3]

mastodonpp::CURLWrapper::CURLWrapper ( )

Initializes curl and sets up connection.

The first time an instance of CURLWrapper is created, it calls curl_global_init, which is not thread-safe. For more information consult curl_global_init(3).

Since
0.1.0
59 {
60  init();
61 }

◆ CURLWrapper() [2/3]

mastodonpp::CURLWrapper::CURLWrapper ( const CURLWrapper )

Copy constructor. Does the same as the Constructor.

Since
0.5.2
64 {
65  init();
66 }

◆ CURLWrapper() [3/3]

mastodonpp::CURLWrapper::CURLWrapper ( CURLWrapper &&  other)
deletenoexcept

Move constructor.

◆ ~CURLWrapper()

mastodonpp::CURLWrapper::~CURLWrapper ( )
virtualnoexcept

Cleans up curl and connection.

May call curl_global_cleanup, which is not thread-safe. For more information consult curl_global_cleanup(3).

Since
0.1.0
69 {
70  curl_easy_cleanup(_connection);
71 
72  --curlwrapper_instances;
73  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
74  if (curlwrapper_instances == 0)
75  {
76  curl_global_cleanup();
77  }
78 }

Member Function Documentation

◆ cancel_stream()

void mastodonpp::CURLWrapper::cancel_stream ( )
inlineprotected

Cancel the stream.

The stream will be cancelled, usually whithin a second. The curl_error_code of the answer will be set to 42 (CURLE_ABORTED_BY_CALLBACK).

Since
0.1.0
210  {
211  _stream_cancelled = true;
212  }

◆ escape_url()

string mastodonpp::CURLWrapper::escape_url ( const string_view  url) const
inline

URL encodes the given string.

For more information consult curl_easy_escape(3).

Parameters
urlString to escape.
Returns
The escaped string or {} if it failed.
Since
0.3.0
126  {
127  char *cbuf{curl_easy_escape(_connection, url.data(),
128  static_cast<int>(url.size()))};
129  string sbuf{cbuf};
130  curl_free(cbuf);
131  return sbuf;
132  }

◆ get_buffer()

string& mastodonpp::CURLWrapper::get_buffer ( )
inlineprotected

Returns a reference to the buffer libcurl writes into.

Since
0.1.0
196  {
197  return _curl_buffer_body;
198  }

◆ get_curl_easy_handle()

CURL* mastodonpp::CURLWrapper::get_curl_easy_handle ( )
inline

Returns pointer to the CURL easy handle.

You can use this handle to set or modify curl options. For more information consult curl_easy_setopt(3).

Since
0.1.0
109  {
110  return _connection;
111  }

◆ make_request()

answer_type mastodonpp::CURLWrapper::make_request ( const http_method method,
string  uri,
const parametermap parameters 
)
protected

Make a HTTP request.

Parameters
methodThe HTTP method.
uriThe full URI.
parametersA map of parameters.
Since
0.1.0
82 {
83  _stream_cancelled = false;
84  _curl_buffer_headers.clear();
85  _curl_buffer_body.clear();
86 
87  CURLcode code{CURLE_OK};
88  switch (method)
89  {
90  case http_method::GET:
91  {
92  add_parameters_to_uri(uri, parameters);
93  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
94  curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
95 
96  break;
97  }
98  case http_method::POST:
99  {
100  if (parameters.empty())
101  {
102  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
103  curl_easy_setopt(_connection, CURLOPT_POST, 1L);
104  }
105  else
106  {
107  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
108  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
109  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
110  }
111 
112  break;
113  }
114  case http_method::PATCH:
115  {
116  if (!parameters.empty())
117  {
118  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
119  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
120  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
121  }
122 
123  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
124  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
125  if (code != CURLE_OK)
126  {
127  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
128  }
129 
130  break;
131  }
132  case http_method::PUT:
133  {
134  if (!parameters.empty())
135  {
136  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
137  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
138  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
139  }
140 
141  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
142  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PUT");
143  if (code != CURLE_OK)
144  {
145  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
146  }
147 
148  break;
149  }
150  case http_method::DELETE:
151  {
152  if (!parameters.empty())
153  {
154  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
155  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
156  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
157  }
158 
159  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
160  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
161  if (code != CURLE_OK)
162  {
163  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
164  }
165 
166  break;
167  }
168  }
169  debuglog << "Making request to: " << uri << '\n';
170 
171  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
172  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
173  if (code != CURLE_OK)
174  {
175  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
176  }
177 
178  answer_type answer;
179  code = curl_easy_perform(_connection);
180  if (code == CURLE_OK
181  || (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
182  {
183  long http_status{0}; // NOLINT(google-runtime-int)
184  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
185  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
186  answer.http_status = static_cast<uint16_t>(http_status);
187  debuglog << "HTTP status code: " << http_status << '\n';
188 
189  answer.headers = _curl_buffer_headers;
190  answer.body = _curl_buffer_body;
191  }
192  else
193  {
194  answer.curl_error_code = static_cast<uint8_t>(code);
195  answer.error_message = _curl_buffer_error;
196  debuglog << "libcurl error: " << code << '\n';
197  debuglog << _curl_buffer_error << '\n';
198  }
199 
200  return answer;
201 }

◆ operator=() [1/2]

CURLWrapper& mastodonpp::CURLWrapper::operator= ( const CURLWrapper other)
delete

Copy assignment operator.

◆ operator=() [2/2]

CURLWrapper& mastodonpp::CURLWrapper::operator= ( CURLWrapper &&  other)
deletenoexcept

Move assignment operator.

◆ set_access_token()

void mastodonpp::CURLWrapper::set_access_token ( string_view  access_token)
protected

Set OAuth 2.0 Bearer Access Token.

Since
0.1.0
241 {
242  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
243  CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER,
244  access_token.data())};
245  if (code != CURLE_OK)
246  {
247  throw CURLException{code, "Could not set authorization token.",
248  _curl_buffer_error};
249  }
250 
251 #if (LIBCURL_VERSION_NUM < 0x073d00) // libcurl < 7.61.0.
252 # define CURLAUTH_BEARER CURLAUTH_ANY
253 #endif
254 
255  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
256  code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
257  if (code != CURLE_OK)
258  {
259  throw CURLException{code, "Could not set authorization token.",
260  _curl_buffer_error};
261  }
262 
263  debuglog << "Set authorization token.\n";
264 }

◆ set_cainfo()

void mastodonpp::CURLWrapper::set_cainfo ( string_view  path)
protectedvirtual

Set path to Certificate Authority (CA) bundle.

Since
0.3.0

Reimplemented in mastodonpp::Instance.

267 {
268  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
269  CURLcode code{curl_easy_setopt(_connection, CURLOPT_CAINFO, path.data())};
270  if (code != CURLE_OK)
271  {
272  throw CURLException{code, "Could not set CA info.", _curl_buffer_error};
273  }
274 }

◆ set_proxy()

void mastodonpp::CURLWrapper::set_proxy ( string_view  proxy)
protectedvirtual

Set the proxy to use.

See CURLOPT_PROXY(3).

Parameters
proxyExamples: "socks4a://127.0.0.1:9050", "http://[::1]:3128".
Since
0.1.0

Reimplemented in mastodonpp::Instance.

230 {
231  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
232  CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy.data())};
233  if (code != CURLE_OK)
234  {
235  throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
236  }
237  debuglog << "Set proxy to: " << proxy << '\n';
238 }

◆ set_useragent()

void mastodonpp::CURLWrapper::set_useragent ( string_view  useragent)
protectedvirtual

Sets the User-Agent.

Since
0.3.0

Reimplemented in mastodonpp::Instance.

277 {
278  CURLcode code{
279  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
280  curl_easy_setopt(_connection, CURLOPT_USERAGENT, useragent.data())};
281  if (code != CURLE_OK)
282  {
283  throw CURLException{code, "Failed to set User-Agent",
284  _curl_buffer_error};
285  }
286  debuglog << "Set User-Agent to: " << useragent << '\n';
287 }

◆ setup_connection_properties()

void mastodonpp::CURLWrapper::setup_connection_properties ( string_view  proxy,
string_view  access_token,
string_view  cainfo,
string_view  useragent 
)

Set some properties of the connection.

Meant for internal use. See Instance::copy_connection_properties().

Since
0.3.0
207 {
208  if (!proxy.empty())
209  {
210  set_proxy(proxy);
211  }
212 
213  if (!access_token.empty())
214  {
215  set_access_token(access_token);
216  }
217 
218  if (!cainfo.empty())
219  {
220  set_cainfo(cainfo);
221  }
222 
223  if (!useragent.empty())
224  {
225  set_useragent(useragent);
226  }
227 }
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:229
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:240
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:276
virtual void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:266

◆ unescape_url()

string mastodonpp::CURLWrapper::unescape_url ( const string_view  url) const
inline

URL decodes the given string.

For more information consult curl_easy_unescape(3).

Parameters
urlString to unescape.
Returns
The unescaped string or {} if it failed.
Since
0.3.0
147  {
148  char *cbuf{curl_easy_unescape(_connection, url.data(),
149  static_cast<int>(url.size()), nullptr)};
150  string sbuf{cbuf};
151  curl_free(cbuf);
152  return sbuf;
153  }

Member Data Documentation

◆ _buffer_mutex

mutex mastodonpp::CURLWrapper::_buffer_mutex
protected

Mutex for get_buffer a.k.a. _curl_buffer_body.

This mutex is locked before anything is read or written from/to _curl_buffer_body.

Since
0.1.0

The documentation for this class was generated from the following files: