mastodonpp  0.0.0
Public Member Functions | Protected Member Functions | 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)=default
 Copy constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=default
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=default
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=default
 Move assignment operator. More...
 
CURL * get_curl_easy_handle ()
 Returns pointer to the CURL easy handle. More...
 

Protected Member Functions

answer_type make_request (const http_method &method, const string_view &uri)
 Make a request. 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
36  : _curl_buffer_error{}
37 {
38  if (curlwrapper_instances == 0)
39  {
40  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
41  }
42  ++curlwrapper_instances;
43  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
44 
45  _connection = curl_easy_init();
46  setup_curl();
47 }

◆ CURLWrapper() [2/3]

mastodonpp::CURLWrapper::CURLWrapper ( const CURLWrapper other)
default

Copy constructor.

◆ CURLWrapper() [3/3]

mastodonpp::CURLWrapper::CURLWrapper ( CURLWrapper &&  other)
defaultnoexcept

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
49 {
50  curl_easy_cleanup(_connection);
51 
52  --curlwrapper_instances;
53  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
54  if (curlwrapper_instances == 0)
55  {
56  curl_global_cleanup();
57  }
58 }

Member Function Documentation

◆ 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
126  {
127  return _connection;
128  }

◆ make_request()

answer_type mastodonpp::CURLWrapper::make_request ( const http_method method,
const string_view &  uri 
)
protected

Make a request.

Parameters
methodThe HTTP method.
uriThe full URI.
Since
0.1.0
62 {
63  debuglog << "Making request to: " << uri << '\n';
64 
65  CURLcode code;
66  switch (method)
67  {
68  case http_method::GET:
69  {
70  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
71  code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
72  break;
73  }
74  case http_method::POST:
75  {
76  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
77  code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
78  break;
79  }
80  case http_method::PATCH:
81  {
82  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
83  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
84  break;
85  }
86  case http_method::PUT:
87  {
88  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
89  code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
90  break;
91  }
92  case http_method::DELETE:
93  {
94  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
95  code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
96  break;
97  }
98  }
99  if (code != CURLE_OK)
100  {
101  throw CURLException{code, "Failed to set HTTP method",
102  _curl_buffer_error};
103  }
104 
105  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
106  code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
107  if (code != CURLE_OK)
108  {
109  throw CURLException{code, "Failed to set URI", _curl_buffer_error};
110  }
111 
112  answer_type answer;
113  code = curl_easy_perform(_connection);
114  if (code == CURLE_OK)
115  {
116  long http_status; // NOLINT(google-runtime-int)
117  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
118  curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
119  answer.http_status = static_cast<uint16_t>(http_status);
120  debuglog << "HTTP status code: " << http_status << '\n';
121 
122  answer.headers = _curl_buffer_headers;
123  answer.body = _curl_buffer_body;
124  }
125  else
126  {
127  answer.curl_error_code = static_cast<uint8_t>(code);
128  answer.error_message = _curl_buffer_error;
129  debuglog << "libcurl error: " << code << '\n';
130  debuglog << _curl_buffer_error << '\n';
131  }
132 
133  return answer;
134 }

◆ operator=() [1/2]

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

Copy assignment operator.

◆ operator=() [2/2]

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

Move assignment operator.


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