mastodonpp  0.0.0
Public 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...
 
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
32  : _curl_buffer_error{}
33 {
34  if (curlwrapper_instances == 0)
35  {
36  curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
37  }
38  ++curlwrapper_instances;
39  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
40 
41  _connection = curl_easy_init();
42  setup_curl();
43 }

◆ 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
45 {
46  curl_easy_cleanup(_connection);
47 
48  --curlwrapper_instances;
49  debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
50  if (curlwrapper_instances == 0)
51  {
52  curl_global_cleanup();
53  }
54 }

Member Function Documentation

◆ make_request()

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

Make a request.

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

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