mastodonpp  0.0.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

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...
 
void set_proxy (string_view proxy)
 Set the proxy to use. More...
 
void cancel_stream ()
 Cancel the stream. 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...
 

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
43  : _curl_buffer_error{}
44  , _stream_cancelled(false)
45 {
46  if (curlwrapper_instances == 0)
47  {
48  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
49  }
50  ++curlwrapper_instances;
51  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
52 
53  _connection = curl_easy_init();
54  setup_curl();
55 }

◆ 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
57 {
58  curl_easy_cleanup(_connection);
59 
60  --curlwrapper_instances;
61  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
62  if (curlwrapper_instances == 0)
63  {
64  curl_global_cleanup();
65  }
66 }

Member Function Documentation

◆ cancel_stream()

void mastodonpp::CURLWrapper::cancel_stream ( )

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
79 {
80  _stream_cancelled = true;
81 }

◆ get_buffer()

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

Returns a reference to the buffer libcurl writes into.

Since
0.1.0
187  {
188  return _curl_buffer_body;
189  }

◆ 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
128  {
129  return _connection;
130  }

◆ 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
85 {
86  _stream_cancelled = false;
87  _curl_buffer_headers.clear();
88  _curl_buffer_body.clear();
89 
90  CURLcode code;
91  switch (method)
92  {
93  case http_method::GET:
94  {
95  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
96  code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
97 
98  add_parameters_to_uri(uri, parameters);
99 
100  break;
101  }
102  case http_method::POST:
103  {
104  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
105  code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
106  break;
107  }
108  case http_method::PATCH:
109  {
110  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
111  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
112  break;
113  }
114  case http_method::PUT:
115  {
116  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
117  code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
118  break;
119  }
120  case http_method::DELETE:
121  {
122  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
123  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
124  break;
125  }
126  }
127  if (code != CURLE_OK)
128  {
129  throw CURLException{code, "Failed to set HTTP method",
130  _curl_buffer_error};
131  }
132  debuglog << "Making request to: " << uri << '\n';
133 
134  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
135  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
136  if (code != CURLE_OK)
137  {
138  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
139  }
140 
141  answer_type answer;
142  code = curl_easy_perform(_connection);
143  if (code == CURLE_OK
144  || (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
145  {
146  long http_status; // NOLINT(google-runtime-int)
147  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
148  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
149  answer.http_status = static_cast<uint16_t>(http_status);
150  debuglog << "HTTP status code: " << http_status << '\n';
151 
152  answer.headers = _curl_buffer_headers;
153  answer.body = _curl_buffer_body;
154  }
155  else
156  {
157  answer.curl_error_code = static_cast<uint8_t>(code);
158  answer.error_message = _curl_buffer_error;
159  debuglog << "libcurl error: " << code << '\n';
160  debuglog << _curl_buffer_error << '\n';
161  }
162 
163  return answer;
164 }

◆ 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_proxy()

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

Set the proxy to use.

See CURLOPT_PROXY(3).

Parameters
proxyExamples: "socks4a://127.0.0.1:9050", "http://[::1]:3128".
Since
0.1.0
69 {
70  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
71  CURLcode code = curl_easy_setopt(_connection, CURLOPT_PROXY, proxy);
72  if (code != CURLE_OK)
73  {
74  throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
75  }
76 }

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 in writer_body() and Connection::get_new_stream_contents 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: