mastodon-cpp  0.30.0
Public Types | Public Member Functions | List of all members
Mastodon::API::http Class Reference

http class. Do not use this directly. More...

#include <mastodon-cpp.hpp>

Public Types

enum  method {
  GET, PATCH, POST, PUT,
  DELETE, GET_STREAM
}
 HTTP methods.
 

Public Member Functions

 http (const API &api, const string &instance, const string &access_token)
 
uint_fast16_t request (const method &meth, const string &path, string &answer)
 
uint_fast16_t request (const method &meth, const string &path, const curlpp::Forms &formdata, string &answer)
 HTTP Request. More...
 
void get_headers (string &headers) const
 Get all headers in a string.
 
void cancel_stream ()
 Cancels the stream. Use only with streams. More...
 
void abort_stream ()
 
std::mutex & get_mutex ()
 Gets the mutex guarding the string that is written to. More...
 

Detailed Description

http class. Do not use this directly.

Since
before 0.11.0

Member Function Documentation

◆ cancel_stream()

void API::http::cancel_stream ( )

Cancels the stream. Use only with streams.

Cancels the stream next time data comes in. Can take a few seconds. This works only with streams, because only streams have an own http object.

Since
0.12.2
231 {
232  _cancel_stream = true;
233 }

◆ get_mutex()

std::mutex & API::http::get_mutex ( )

Gets the mutex guarding the string that is written to.

The mutex guards the function that writes to the string you specified in get_stream().

Returns
A reference of the mutex.
Since
0.12.3
241 {
242  return _mutex;
243 }

◆ request()

uint_fast16_t API::http::request ( const method meth,
const string &  path,
const curlpp::Forms &  formdata,
string &  answer 
)

HTTP Request.

Parameters
methThe method defined in http::method
pathThe api call as string
formdataThe form data for PATCH and POST requests.
answerThe answer from the server
Returns
Error code. If the URL has permanently changed, 13 is returned and answer is set to the new URL.
Since
before 0.11.0
58 {
59  using namespace std::placeholders; // _1, _2, _3
60 
61  uint_fast16_t ret = 0;
62  ttdebug << "Path is: " << path << '\n';
63 
64  try
65  {
66  curlpp::Easy request;
67  std::list<string> headers;
68 
69  request.setOpt<curlopts::Url>("https://" + _instance + path);
70  ttdebug << "User-Agent: " << parent.get_useragent() << "\n";
71  request.setOpt<curlopts::UserAgent>(parent.get_useragent());
72 
73  {
74  string proxy;
75  string userpw;
76  parent.get_proxy(proxy, userpw);
77  if (!proxy.empty())
78  {
79  request.setOpt<curlopts::Proxy>(proxy);
80  if (!userpw.empty())
81  {
82  request.setOpt<curlopts::ProxyUserPwd>(userpw);
83  }
84  }
85  }
86 
87  if (!_access_token.empty())
88  {
89  headers.push_back("Authorization: Bearer " + _access_token);
90  }
91  if (meth != http::method::GET_STREAM)
92  {
93  headers.push_back("Connection: close");
94  // Get headers from server
95  request.setOpt<curlpp::options::Header>(true);
96  }
97 
98  request.setOpt<curlopts::HttpHeader>(headers);
99  request.setOpt<curlopts::FollowLocation>(true);
100  request.setOpt<curlopts::WriteFunction>
101  (std::bind(&http::callback_write, this, _1, _2, _3, &answer));
102  request.setOpt<curlopts::ProgressFunction>
103  (std::bind(&http::callback_progress, this, _1, _2, _3, _4));
104  request.setOpt<curlopts::NoProgress>(0);
105  if (!formdata.empty())
106  {
107  request.setOpt<curlopts::HttpPost>(formdata);
108  }
109 
110  switch (meth)
111  {
112  case http::method::GET:
113  break;
114  case http::method::PATCH:
115  request.setOpt<curlopts::CustomRequest>("PATCH");
116  break;
117  case http::method::POST:
118  request.setOpt<curlopts::CustomRequest>("POST");
119  break;
120  case http::method::PUT:
121  request.setOpt<curlopts::CustomRequest>("PUT");
122  break;
123  case http::method::DELETE:
124  request.setOpt<curlopts::CustomRequest>("DELETE");
125  break;
126  default:
127  break;
128  }
129 
130  //request.setOpt<curlopts::Verbose>(true);
131 
132  answer.clear();
133  request.perform();
134  ret = curlpp::infos::ResponseCode::get(request);
135  ttdebug << "Response code: " << ret << '\n';
136  // Work around "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK"
137  size_t pos = answer.find("\r\n\r\n", 25);
138  _headers = answer.substr(0, pos);
139  // Only return body
140  answer = answer.substr(pos + 4);
141 
142  if (ret == 200 || ret == 302 || ret == 307)
143  { // OK or Found or Temporary Redirect
144  return 0;
145  }
146  else if (ret == 301 || ret == 308)
147  { // Moved Permanently or Permanent Redirect
148  // return new URL
149  answer = curlpp::infos::EffectiveUrl::get(request);
150  return 13;
151  }
152  else if (ret == 0)
153  {
154  return 0xffff;
155  }
156  else
157  {
158  return ret;
159  }
160  }
161  catch (curlpp::RuntimeError &e)
162  {
163  const string what = e.what();
164  // This error is thrown if http.cancel_stream() is used.
165  if ((what.compare(0, 16, "Callback aborted") == 0) ||
166  (what.compare(0, 19, "Failed writing body") == 0))
167  {
168  ttdebug << "Request was cancelled by user\n";
169  return 14;
170  }
171  else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0)
172  {
173  ttdebug << "Timeout\n";
174  return 16;
175  }
176 
177  if (parent.exceptions())
178  {
179  std::rethrow_exception(std::current_exception());
180  }
181  else
182  {
183  ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl;
184  return 15;
185  }
186  }
187  catch (curlpp::LogicError &e)
188  {
189  if (parent.exceptions())
190  {
191  std::rethrow_exception(std::current_exception());
192  }
193 
194  ttdebug << "curlpp::LogicError: " << e.what() << std::endl;
195  return 15;
196  }
197 }

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