mastodon-cpp  0.110.0
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 Member Functions

 http (const API &api, const string &instance, const string &access_token)
 Constructs new http object. More...
 
return_call request (const http_method &meth, const string &path)
 
return_call request (const http_method &meth, const string &path, HTMLForm &formdata)
 HTTP Request. More...
 
void request_stream (const string &path, string &stream)
 HTTP Request for streams. More...
 
void get_headers (string &headers) const
 Get all headers in a string.
 
void cancel_stream ()
 Cancels the stream. Use only with streams. More...
 
std::mutex & get_mutex ()
 Gets the mutex guarding the string that is written to. More...
 
void set_proxy (const string &hostport, const string &userpw="")
 Set proxy. Do not call this directly. More...
 

Detailed Description

http class. Do not use this directly.

Since
before 0.11.0

Constructor & Destructor Documentation

◆ http()

API::http::http ( const API api,
const string &  instance,
const string &  access_token 
)
explicit

Constructs new http object.

Parameters
APIParent object.
instanceInstance domain name
access_tokenAccess token
Since
before 0.11.0
51 : parent(api)
52 , _instance(instance)
53 , _access_token(access_token)
54 , _cancel_stream(false)
55 {
56  Poco::Net::initializeSSL();
57 
58  try
59  {
60  string env_proxy = Environment::get("http_proxy");
61  size_t pos;
62 
63  // Only keep text between // and /.
64  if ((pos = env_proxy.find("//")) != string::npos)
65  {
66  env_proxy = env_proxy.substr(pos + 2);
67  }
68  if ((pos = env_proxy.find('/')) != string::npos)
69  {
70  env_proxy = env_proxy.substr(0, pos);
71  }
72 
73  if ((pos = env_proxy.find('@')) != string::npos)
74  {
75  string hostport = env_proxy.substr(pos + 1);
76  string userpw = env_proxy.substr(0, pos);
77  set_proxy(hostport, userpw);
78  }
79  else
80  {
81  set_proxy(env_proxy);
82  }
83  }
84  catch (const std::exception &)
85  {
86  // No proxy found, no problem.
87  }
88 
89 }
void set_proxy(const string &hostport, const string &userpw="")
Set proxy. Do not call this directly.
Definition: http.cpp:96

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
353 {
354  _cancel_stream = true;
355  _streamthread.join();
356 }

◆ 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
359 {
360  return _mutex;
361 }

◆ request()

return_call API::http::request ( const http_method meth,
const string &  path,
HTMLForm &  formdata 
)

HTTP Request.

Parameters
methA method defined in http::method.
pathThe API call as string.
formdataThe form data for PATCH and POST requests.
Since
0.100.0
145 {
146  string answer;
147  return request_common(meth, path, formdata, answer);
148 }

◆ request_stream()

void API::http::request_stream ( const string &  path,
string &  stream 
)

HTTP Request for streams.

Parameters
pathThe API call as string.
streamThe stream of data that is returned.
Since
0.100.0
151 {
152  static return_call ret;
153  _streamthread = std::thread(
154  [&, path] // path is captured by value because it may be
155  { // deleted before we access it.
156  HTMLForm form;
157  ret = request_common(http_method::GET_STREAM, path,
158  form, stream);
159  ttdebug << "Remaining content of the stream: " << stream << '\n';
160  if (!ret)
161  {
162  // Embed the HTTP status code in stream on error.
163  stream += "event: ERROR\ndata: {\"error_code\":"
164  + std::to_string(ret.error_code) + ",\"http_error\":"
165  + std::to_string(ret.http_error_code) + "}\n";
166  }
167  });
168 }
Return type for API calls.
Definition: return_types.hpp:93
uint8_t error_code
Error code.
Definition: return_types.hpp:42
uint16_t http_error_code
HTTP error code.
Definition: return_types.hpp:56

◆ set_proxy()

void API::http::set_proxy ( const string &  hostport,
const string &  userpw = "" 
)

Set proxy. Do not call this directly.

Parameters
hostporthost[:port]
userpwuser[:password] (optional)
Since
0.110.0
97 {
98  try
99  {
100  HTTPSClientSession::ProxyConfig proxyconfig;
101  size_t pos = hostport.find(':');
102 
103  proxyconfig.host = hostport.substr(0, pos);
104  if (pos != string::npos)
105  {
106  proxyconfig.port = std::stoi(hostport.substr(pos + 1));
107  }
108 
109  if (!userpw.empty())
110  {
111  string username;
112 
113  pos = userpw.find(':');
114  Poco::URI::decode(userpw.substr(0, pos), username);
115  proxyconfig.username = username;
116 
117  if (pos != string::npos)
118  {
119  string password;
120  Poco::URI::decode(userpw.substr(pos + 1), password);
121  proxyconfig.password = password;
122  }
123  }
124 
125  HTTPSClientSession::setGlobalProxyConfig(proxyconfig);
126  ttdebug << "Set proxy to " << hostport << ".\n";
127  }
128  catch (const std::exception &e)
129  {
130  if (parent.exceptions())
131  {
132  std::rethrow_exception(std::current_exception());
133  }
134  }
135 }
bool exceptions(const bool &value)
Turn exceptions on or off. Defaults to off.
Definition: mastodon-cpp.cpp:283

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