mastodonpp  0.5.4
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:
mastodonpp::Connection mastodonpp::Instance mastodonpp::Instance::ObtainToken

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
58  : _connection{}
59  , _curl_buffer_error{}
60  , _stream_cancelled{false}
61 {
62  init();
63 }

◆ CURLWrapper() [2/3]

mastodonpp::CURLWrapper::CURLWrapper ( const CURLWrapper )

Copy constructor. Does the same as the Constructor.

Since
0.5.2
66  : _connection{}
67  , _curl_buffer_error{}
68  , _stream_cancelled{false}
69 {
70  init();
71 }

◆ 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
74 {
75  curl_easy_cleanup(_connection);
76 
77  --curlwrapper_instances;
78  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
79  if (curlwrapper_instances == 0)
80  {
81  curl_global_cleanup();
82  }
83 }

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
215  {
216  _stream_cancelled = true;
217  }

◆ 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
128  {
129  char *cbuf{curl_easy_escape(_connection, url.data(),
130  static_cast<int>(url.size()))};
131  const string sbuf{cbuf};
132  curl_free(cbuf);
133  return sbuf;
134  }

◆ get_buffer()

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

Returns a reference to the buffer libcurl writes into.

Since
0.1.0
201  {
202  return _curl_buffer_body;
203  }

◆ 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
110  {
111  return _connection;
112  }

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

◆ 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
246 {
247  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
248  CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER,
249  access_token.data())};
250  if (code != CURLE_OK)
251  {
252  throw CURLException{code, "Could not set authorization token.",
253  _curl_buffer_error};
254  }
255 
256 #if (LIBCURL_VERSION_NUM < 0x073d00) // libcurl < 7.61.0.
257 #define CURLAUTH_BEARER CURLAUTH_ANY
258 #endif
259 
260  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
261  code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
262  if (code != CURLE_OK)
263  {
264  throw CURLException{code, "Could not set authorization token.",
265  _curl_buffer_error};
266  }
267 
268  debuglog << "Set authorization token.\n";
269 }

◆ 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.

272 {
273  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
274  CURLcode code{curl_easy_setopt(_connection, CURLOPT_CAINFO, path.data())};
275  if (code != CURLE_OK)
276  {
277  throw CURLException{code, "Could not set CA info.", _curl_buffer_error};
278  }
279 }

◆ 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.

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

◆ set_useragent()

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

Sets the User-Agent.

Since
0.3.0

Reimplemented in mastodonpp::Instance.

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

◆ 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
212 {
213  if (!proxy.empty())
214  {
215  set_proxy(proxy);
216  }
217 
218  if (!access_token.empty())
219  {
220  set_access_token(access_token);
221  }
222 
223  if (!cainfo.empty())
224  {
225  set_cainfo(cainfo);
226  }
227 
228  if (!useragent.empty())
229  {
230  set_useragent(useragent);
231  }
232 }

◆ 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
150  {
151  char *cbuf{curl_easy_unescape(_connection, url.data(),
152  static_cast<int>(url.size()), nullptr)};
153  const string sbuf{cbuf};
154  curl_free(cbuf);
155  return sbuf;
156  }

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:
mastodonpp::CURLWrapper::set_proxy
virtual void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:234
mastodonpp::CURLWrapper::set_cainfo
virtual void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:271
mastodonpp::CURLWrapper::set_useragent
virtual void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:281
mastodonpp::CURLWrapper::set_access_token
void set_access_token(string_view access_token)
Set OAuth 2.0 Bearer Access Token.
Definition: curl_wrapper.cpp:245