Added proxy support

This commit is contained in:
tastytea 2018-05-26 22:34:31 +02:00
rodič e01ce6b96f
revize 86a614dd61
Podepsáno: tastytea
ID GPG klíče: 59346E0EA35C67E5
5 změnil soubory, kde provedl 82 přidání a 1 odebrání

Zobrazit soubor

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.14.0
VERSION 0.15.0
LANGUAGES CXX
)

Zobrazit soubor

@ -0,0 +1,40 @@
/* This file is part of mastodon-cpp.
* This example shows how to use a proxy.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using std::cout;
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
// SOCKS5 Proxy. Proxy resolves URL hostname.
masto.set_proxy("socks5h://[fd12::1a]:1080/");
// HTTPS proxy with username and password
masto.set_proxy("https://localhost:3128", "user5:supersecurepassword");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
std::cout << answer << '\n';
return 0;
}

Zobrazit soubor

@ -35,6 +35,8 @@ API::http::http(const API &api, const string &instance,
, _instance(instance)
, _access_token(access_token)
, _cancel_stream(false)
, _proxy("")
, _proxy_userpw("")
{
curlpp::initialize();
}
@ -70,6 +72,15 @@ const uint_fast16_t API::http::request(const method &meth,
ttdebug << "User-Agent: " << parent.get_useragent() << "\n";
request.setOpt<curlopts::UserAgent>(parent.get_useragent());
if (!_proxy.empty())
{
request.setOpt<curlopts::Proxy>(_proxy);
if (!_proxy_userpw.empty())
{
request.setOpt<curlopts::ProxyUserPwd>(_proxy_userpw);
}
}
if (!_access_token.empty())
{
headers.push_back("Authorization: Bearer " + _access_token);
@ -217,3 +228,9 @@ std::mutex &API::http::get_mutex()
{
return _mutex;
}
const void API::http::set_proxy(const string &proxy, const string &userpw)
{
_proxy = proxy;
_proxy_userpw = userpw;
}

Zobrazit soubor

@ -571,3 +571,8 @@ const string API::unescape_html(const string &html)
return output;
}
const void API::set_proxy(const string &proxy, const string &userpw)
{
_http.set_proxy(proxy, userpw);
}

Zobrazit soubor

@ -146,6 +146,16 @@ public:
*/
std::mutex &get_mutex();
/*!
* @brief Sets the proxy.
*
* @param proxy See `man 3 CURLOPT_PROXY`
* @param userpw See `man 3 CURLOPT_PROXYUSERPWD` (optional)
*
* @since 0.15.0
*/
const void set_proxy(const string &proxy, const string &userpw = "");
private:
const API &parent;
const string _instance;
@ -153,6 +163,8 @@ public:
string _headers;
bool _cancel_stream;
std::mutex _mutex;
string _proxy;
string _proxy_userpw;
const size_t callback_write(char* data, size_t size, size_t nmemb,
string *oss);
@ -395,6 +407,13 @@ public:
*/
static const string unescape_html(const string &html);
/*!
* @brief Calls Mastodon::API::http::set_proxy()
*
* @since 0.15.0
*/
const void set_proxy(const string &proxy, const string &userpw = "");
/*!
* @brief Make a GET request which doesn't require parameters.
*