Handles the details of network connections.
More...
#include <mastodonpp/curl_wrapper.hpp>
Handles the details of network connections.
You don't need to use this.
- Since
- 0.1.0
◆ 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)
46 if (curlwrapper_instances == 0)
48 curl_global_init(CURL_GLOBAL_ALL);
50 ++curlwrapper_instances;
51 debuglog <<
"CURLWrapper instances: " << curlwrapper_instances <<
" (+1)\n";
53 _connection = curl_easy_init();
◆ CURLWrapper() [2/3]
mastodonpp::CURLWrapper::CURLWrapper |
( |
const CURLWrapper & |
other | ) |
|
|
delete |
◆ CURLWrapper() [3/3]
mastodonpp::CURLWrapper::CURLWrapper |
( |
CURLWrapper && |
other | ) |
|
|
deletenoexcept |
◆ ~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
58 curl_easy_cleanup(_connection);
60 --curlwrapper_instances;
61 debuglog <<
"CURLWrapper instances: " << curlwrapper_instances <<
" (-1)\n";
62 if (curlwrapper_instances == 0)
64 curl_global_cleanup();
◆ 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
80 _stream_cancelled =
true;
◆ get_buffer()
string& mastodonpp::CURLWrapper::get_buffer |
( |
| ) |
|
|
inlineprotected |
Returns a reference to the buffer libcurl writes into.
- Since
- 0.1.0
188 return _curl_buffer_body;
◆ 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
◆ make_request()
Make a HTTP request.
- Parameters
-
method | The HTTP method. |
uri | The full URI. |
parameters | A map of parameters. |
- Since
- 0.1.0
86 _stream_cancelled =
false;
87 _curl_buffer_headers.clear();
88 _curl_buffer_body.clear();
93 case http_method::GET:
96 code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
98 add_parameters_to_uri(uri, parameters);
102 case http_method::POST:
105 code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
108 case http_method::PATCH:
111 code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST,
"PATCH");
114 case http_method::PUT:
117 code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
120 case http_method::DELETE:
123 code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST,
"DELETE");
127 if (code != CURLE_OK)
129 throw CURLException{code,
"Failed to set HTTP method",
132 debuglog <<
"Making request to: " << uri <<
'\n';
135 code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
136 if (code != CURLE_OK)
138 throw CURLException{code,
"Failed to set URI", _curl_buffer_error};
142 code = curl_easy_perform(_connection);
144 || (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
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';
152 answer.headers = _curl_buffer_headers;
153 answer.body = _curl_buffer_body;
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';
◆ operator=() [1/2]
Copy assignment operator.
◆ operator=() [2/2]
Move assignment operator.
◆ set_proxy()
void mastodonpp::CURLWrapper::set_proxy |
( |
string_view |
proxy | ) |
|
Set the proxy to use.
See CURLOPT_PROXY(3).
- Parameters
-
proxy | Examples: "socks4a://127.0.0.1:9050", "http://[::1]:3128". |
- Since
- 0.1.0
71 CURLcode code = curl_easy_setopt(_connection, CURLOPT_PROXY, proxy);
74 throw CURLException{code,
"Failed to set proxy", _curl_buffer_error};
◆ buffer_mutex
mutex mastodonpp::CURLWrapper::buffer_mutex |
|
protected |
The documentation for this class was generated from the following files: