mastodonpp  0.4.0
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 &other)=delete
 Copy 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...
 
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...
 
void set_cainfo (string_view path)
 Set path to Certificate Authority (CA) bundle. More...
 
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
42  : _curl_buffer_error{}
43  , _stream_cancelled(false)
44 {
45  if (curlwrapper_instances == 0)
46  {
47  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
48  }
49  ++curlwrapper_instances;
50  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
51 
52  _connection = curl_easy_init();
53  setup_curl();
54 }

◆ CURLWrapper() [2/3]

mastodonpp::CURLWrapper::CURLWrapper ( const CURLWrapper other)
delete

Copy constructor.

◆ CURLWrapper() [3/3]

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

Move constructor.

◆ ~CURLWrapper()

mastodonpp::CURLWrapper::~CURLWrapper ( )
virtualnoexcept

Cleans up curl and connection.

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

Since
0.1.0
56 {
57  curl_easy_cleanup(_connection);
58 
59  --curlwrapper_instances;
60  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
61  if (curlwrapper_instances == 0)
62  {
63  curl_global_cleanup();
64  }
65 }

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
211  {
212  _stream_cancelled = true;
213  }

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

◆ get_buffer()

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

Returns a reference to the buffer libcurl writes into.

Since
0.1.0
197  {
198  return _curl_buffer_body;
199  }

◆ 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
106  {
107  return _connection;
108  }

◆ 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
69 {
70  _stream_cancelled = false;
71  _curl_buffer_headers.clear();
72  _curl_buffer_body.clear();
73 
74  CURLcode code;
75  switch (method)
76  {
77  case http_method::GET:
78  {
79  add_parameters_to_uri(uri, parameters);
80  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
81  curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
82 
83  break;
84  }
85  case http_method::POST:
86  {
87  if (parameters.empty())
88  {
89  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
90  curl_easy_setopt(_connection, CURLOPT_POST, 1L);
91  }
92  else
93  {
94  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
95  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
96  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
97  }
98 
99  break;
100  }
101  case http_method::PATCH:
102  {
103  if (!parameters.empty())
104  {
105  curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
106  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
107  curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
108  }
109 
110  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
111  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
112  if (code != CURLE_OK)
113  {
114  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
115  }
116 
117  break;
118  }
119  case http_method::PUT:
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, "PUT");
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::DELETE:
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, "DELETE");
148  if (code != CURLE_OK)
149  {
150  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
151  }
152 
153  break;
154  }
155  }
156  debuglog << "Making request to: " << uri << '\n';
157 
158  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
159  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
160  if (code != CURLE_OK)
161  {
162  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
163  }
164 
165  answer_type answer;
166  code = curl_easy_perform(_connection);
167  if (code == CURLE_OK
168  || (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
169  {
170  long http_status; // NOLINT(google-runtime-int)
171  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
172  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
173  answer.http_status = static_cast<uint16_t>(http_status);
174  debuglog << "HTTP status code: " << http_status << '\n';
175 
176  answer.headers = _curl_buffer_headers;
177  answer.body = _curl_buffer_body;
178  }
179  else
180  {
181  answer.curl_error_code = static_cast<uint8_t>(code);
182  answer.error_message = _curl_buffer_error;
183  debuglog << "libcurl error: " << code << '\n';
184  debuglog << _curl_buffer_error << '\n';
185  }
186 
187  return answer;
188 }

◆ 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
228 {
229  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
230  CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER,
231  access_token.data())};
232  if (code != CURLE_OK)
233  {
234  throw CURLException{code, "Could not set authorization token.",
235  _curl_buffer_error};
236  }
237 
238 #if (LIBCURL_VERSION_NUM < 0x073d00) // libcurl < 7.61.0.
239 #define CURLAUTH_BEARER CURLAUTH_ANY
240 #endif
241 
242  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
243  code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
244  if (code != CURLE_OK)
245  {
246  throw CURLException{code, "Could not set authorization token.",
247  _curl_buffer_error};
248  }
249 
250  debuglog << "Set authorization token.\n";
251 }

◆ set_cainfo()

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

Set path to Certificate Authority (CA) bundle.

Since
0.3.0
254 {
255  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
256  CURLcode code{curl_easy_setopt(_connection, CURLOPT_CAINFO, path.data())};
257  if (code != CURLE_OK)
258  {
259  throw CURLException{code, "Could not set CA info.", _curl_buffer_error};
260  }
261 }

◆ set_proxy()

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

Set the proxy to use.

See CURLOPT_PROXY(3).

Parameters
proxyExamples: "socks4a://127.0.0.1:9050", "http://[::1]:3128".
Since
0.1.0
217 {
218  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
219  CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy.data())};
220  if (code != CURLE_OK)
221  {
222  throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
223  }
224  debuglog << "Set proxy to: " << proxy << '\n';
225 }

◆ set_useragent()

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

Sets the User-Agent.

Since
0.3.0
264 {
265  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
266  CURLcode code{curl_easy_setopt(_connection, CURLOPT_USERAGENT,
267  useragent.data())};
268  if (code != CURLE_OK)
269  {
270  throw CURLException{code, "Failed to set User-Agent",
271  _curl_buffer_error};
272  }
273  debuglog << "Set User-Agent to: " << useragent << '\n';
274 }

◆ 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
194 {
195  if (!proxy.empty())
196  {
197  set_proxy(proxy);
198  }
199 
200  if (!access_token.empty())
201  {
202  set_access_token(access_token);
203  }
204 
205  if (!cainfo.empty())
206  {
207  set_cainfo(cainfo);
208  }
209 
210  if (!useragent.empty())
211  {
212  set_useragent(useragent);
213  }
214 }

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

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
void set_proxy(string_view proxy)
Set the proxy to use.
Definition: curl_wrapper.cpp:216
mastodonpp::CURLWrapper::set_cainfo
void set_cainfo(string_view path)
Set path to Certificate Authority (CA) bundle.
Definition: curl_wrapper.cpp:253
mastodonpp::CURLWrapper::set_useragent
void set_useragent(string_view useragent)
Sets the User-Agent.
Definition: curl_wrapper.cpp:263
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