mastodon-cpp  0.15.1
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)
 
const uint_fast16_t request (const method &meth, const string &path, string &answer)
 
const uint_fast16_t request (const method &meth, const string &path, const curlpp::Forms &formdata, string &answer)
 HTTP Request. More...
 
const void get_headers (string &headers) const
 Get all headers in a string.
 
const void cancel_stream ()
 Cancels the stream. Use only with streams. More...
 
const 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.

Member Function Documentation

◆ cancel_stream()

const 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
221 {
222  _cancel_stream = true;
223 }

◆ 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.
231 {
232  return _mutex;
233 }

◆ request()

const 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.
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  case http::method::DELETE:
123  request.setOpt<curlopts::CustomRequest>("DELETE");
124  default:
125  break;
126  }
127 
128  //request.setOpt<curlopts::Verbose>(true);
129 
130  answer.clear();
131  request.perform();
132  ret = curlpp::infos::ResponseCode::get(request);
133  ttdebug << "Response code: " << ret << '\n';
134  // Work around "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK"
135  size_t pos = answer.find("\r\n\r\n", 25);
136  _headers = answer.substr(0, pos);
137  // Only return body
138  answer = answer.substr(pos + 4);
139 
140  if (ret == 200 || ret == 302 || ret == 307)
141  { // OK or Found or Temporary Redirect
142  return 0;
143  }
144  else if (ret == 301 || ret == 308)
145  { // Moved Permanently or Permanent Redirect
146  // return new URL
147  answer = curlpp::infos::EffectiveUrl::get(request);
148  return 13;
149  }
150  else
151  {
152  return ret;
153  }
154  }
155  catch (curlpp::RuntimeError &e)
156  {
157  // This error is thrown if http.cancel_stream() is used.
158  if ((std::strncmp(e.what(), "Callback aborted", 16) == 0) ||
159  (std::strncmp(e.what(), "Failed writing body", 19) == 0))
160  {
161  ttdebug << "Request was cancelled by user\n";
162  return 14;
163  }
164 
165  if (parent.exceptions())
166  {
167  std::rethrow_exception(std::current_exception());
168  }
169  else
170  {
171  ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl;
172  return 15;
173  }
174  }
175  catch (curlpp::LogicError &e)
176  {
177  if (parent.exceptions())
178  {
179  std::rethrow_exception(std::current_exception());
180  }
181 
182  ttdebug << "curlpp::LogicError: " << e.what() << std::endl;
183  return 15;
184  }
185 
186  return ret;
187 }

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