This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/http.cpp

261 lines
7.6 KiB
C++
Raw Normal View History

2018-01-09 22:12:11 +01:00
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
2019-03-30 22:14:58 +01:00
*
2018-01-09 22:12:11 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
2018-02-26 07:57:30 +01:00
#include <functional> // std::bind
#include <list>
#include <cstring> // std::strncmp
2018-04-10 10:17:30 +02:00
#include <exception>
2019-04-10 02:25:55 +02:00
#include <thread>
2018-02-09 16:01:24 +01:00
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
2018-02-10 12:01:55 +01:00
#include <curlpp/Infos.hpp>
2019-02-22 11:35:06 +01:00
#include "debug.hpp"
2018-01-09 22:12:11 +01:00
#include "mastodon-cpp.hpp"
using namespace Mastodon;
2018-02-09 16:01:24 +01:00
namespace curlopts = curlpp::options;
2018-01-09 22:12:11 +01:00
using std::cerr;
2018-01-10 18:19:19 +01:00
API::http::http(const API &api, const string &instance,
const string &access_token)
: parent(api)
, _instance(instance)
2018-01-09 22:12:11 +01:00
, _access_token(access_token)
, _cancel_stream(false)
2018-01-09 22:12:11 +01:00
{
2018-02-09 16:01:24 +01:00
curlpp::initialize();
2018-01-09 22:12:11 +01:00
}
2018-02-26 07:57:30 +01:00
API::http::~http()
{
curlpp::terminate();
}
return_call API::http::request(const http_method &meth, const string &path)
2018-01-09 22:12:11 +01:00
{
2019-02-22 12:03:28 +01:00
return request(meth, path, curlpp::Forms());
2018-01-09 22:12:11 +01:00
}
2019-04-10 02:25:55 +02:00
return_call API::http::request(const http_method &meth, const string &path,
const curlpp::Forms &formdata)
2019-04-10 02:25:55 +02:00
{
string answer;
return request_common(meth, path, formdata, answer);
}
uint8_t API::http::request_stream(const string &path, string &stream)
{
static return_call ret;
_streamthread = std::thread(
[&]
{
ret = request_common(http_method::GET_STREAM, path,
curlpp::Forms(), stream);
});
// FIXME: Build reliable error detection.
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!ret)
{
cancel_stream();
return ret.error_code;
}
else
{
return 0;
}
}
return_call API::http::request_common(const http_method &meth,
const string &path,
const curlpp::Forms &formdata,
string &answer)
2018-01-09 22:12:11 +01:00
{
2018-02-26 07:57:30 +01:00
using namespace std::placeholders; // _1, _2, _3
2018-01-17 23:51:59 +01:00
ttdebug << "Path is: " << path << '\n';
2019-03-30 22:14:58 +01:00
2018-01-09 22:12:11 +01:00
try
{
2018-02-09 16:01:24 +01:00
curlpp::Easy request;
2018-02-26 07:57:30 +01:00
std::list<string> headers;
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::Url>("https://" + _instance + path);
2018-02-28 22:37:30 +01:00
ttdebug << "User-Agent: " << parent.get_useragent() << "\n";
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::UserAgent>(parent.get_useragent());
2018-02-26 07:57:30 +01:00
2018-05-26 22:34:31 +02:00
{
2018-05-26 23:48:03 +02:00
string proxy;
string userpw;
parent.get_proxy(proxy, userpw);
if (!proxy.empty())
2018-05-26 22:34:31 +02:00
{
2018-05-26 23:48:03 +02:00
request.setOpt<curlopts::Proxy>(proxy);
if (!userpw.empty())
{
request.setOpt<curlopts::ProxyUserPwd>(userpw);
}
2018-05-26 22:34:31 +02:00
}
}
2018-02-26 07:57:30 +01:00
if (!_access_token.empty())
2018-01-09 22:12:11 +01:00
{
2018-02-26 07:57:30 +01:00
headers.push_back("Authorization: Bearer " + _access_token);
}
if (meth != http_method::GET_STREAM)
2018-02-26 07:57:30 +01:00
{
headers.push_back("Connection: close");
// Get headers from server
2018-02-26 07:57:30 +01:00
request.setOpt<curlpp::options::Header>(true);
}
request.setOpt<curlopts::HttpHeader>(headers);
2018-02-10 12:01:55 +01:00
request.setOpt<curlopts::FollowLocation>(true);
2018-05-17 17:59:44 +02:00
request.setOpt<curlopts::WriteFunction>
(std::bind(&http::callback_write, this, _1, _2, _3, &answer));
request.setOpt<curlopts::ProgressFunction>
(std::bind(&http::callback_progress, this, _1, _2, _3, _4));
request.setOpt<curlopts::NoProgress>(0);
2018-02-09 16:01:24 +01:00
if (!formdata.empty())
2018-01-26 00:24:00 +01:00
{
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::HttpPost>(formdata);
2018-01-26 00:24:00 +01:00
}
2018-02-09 16:01:24 +01:00
switch (meth)
{
case http_method::GET:
case http_method::GET_STREAM:
break;
case http_method::PATCH:
request.setOpt<curlopts::CustomRequest>("PATCH");
break;
case http_method::POST:
request.setOpt<curlopts::CustomRequest>("POST");
break;
case http_method::PUT:
request.setOpt<curlopts::CustomRequest>("PUT");
break;
case http_method::DELETE:
request.setOpt<curlopts::CustomRequest>("DELETE");
break;
}
2019-03-30 22:14:58 +01:00
//request.setOpt<curlopts::Verbose>(true);
answer.clear();
2018-02-10 12:01:55 +01:00
request.perform();
2019-02-22 12:03:28 +01:00
uint16_t http_code = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << http_code << '\n';
// Work around "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK"
size_t pos = answer.find("\r\n\r\n", 25);
2018-02-26 07:57:30 +01:00
_headers = answer.substr(0, pos);
// Only return body
answer = answer.substr(pos + 4);
2018-02-25 23:20:02 +01:00
2019-02-22 12:03:28 +01:00
if (http_code == 200 || http_code == 302 || http_code == 307)
2018-02-10 12:01:55 +01:00
{ // OK or Found or Temporary Redirect
2019-02-22 12:03:28 +01:00
return { 0, "", http_code, answer };
2018-02-10 12:01:55 +01:00
}
2019-02-22 12:03:28 +01:00
else if (http_code == 301 || http_code == 308)
{ // Moved Permanently or Permanent Redirect
2018-02-17 20:01:51 +01:00
// return new URL
answer = curlpp::infos::EffectiveUrl::get(request);
2019-02-22 12:03:28 +01:00
return { 78, "Remote address changed", http_code, answer };
2018-02-10 12:01:55 +01:00
}
2019-02-22 12:03:28 +01:00
else if (http_code == 0)
2018-10-08 01:24:12 +02:00
{
2019-02-22 12:03:28 +01:00
return { 255, "Unknown error", http_code, answer };
2018-10-08 01:24:12 +02:00
}
2018-02-10 12:01:55 +01:00
else
{
2019-02-22 12:03:28 +01:00
return { 111, "Connection refused", http_code, answer };
2018-02-10 12:01:55 +01:00
}
2018-01-09 22:12:11 +01:00
}
2018-02-09 16:01:24 +01:00
catch (curlpp::RuntimeError &e)
{
2018-10-08 01:24:12 +02:00
const string what = e.what();
// This error is thrown if http.cancel_stream() is used.
2018-10-08 01:24:12 +02:00
if ((what.compare(0, 16, "Callback aborted") == 0) ||
(what.compare(0, 19, "Failed writing body") == 0))
2018-02-26 07:57:30 +01:00
{
ttdebug << "Request was cancelled by user\n";
2019-02-22 12:03:28 +01:00
return { 0, "Request was cancelled by user", 0, "" };
2018-02-26 07:57:30 +01:00
}
2018-10-08 01:24:12 +02:00
else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0)
{
ttdebug << "Timeout\n";
2019-02-22 12:03:28 +01:00
return { 110, "Connection timed out", 0, "" };
2018-10-08 01:24:12 +02:00
}
2018-04-10 10:44:46 +02:00
if (parent.exceptions())
2018-04-09 17:55:11 +02:00
{
2018-04-10 10:44:46 +02:00
std::rethrow_exception(std::current_exception());
2018-04-09 17:55:11 +02:00
}
2018-02-26 07:57:30 +01:00
else
{
2018-04-10 10:44:46 +02:00
ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl;
2019-02-28 15:47:25 +01:00
return { 192, e.what(), 0, "" };
2018-02-26 07:57:30 +01:00
}
2018-02-09 16:01:24 +01:00
}
catch (curlpp::LogicError &e)
2018-01-09 22:12:11 +01:00
{
2018-04-10 10:17:30 +02:00
if (parent.exceptions())
{
std::rethrow_exception(std::current_exception());
}
2018-04-10 10:44:46 +02:00
ttdebug << "curlpp::LogicError: " << e.what() << std::endl;
2019-02-28 15:47:25 +01:00
return { 193, e.what(), 0, "" };
2018-01-09 22:12:11 +01:00
}
}
2018-02-25 23:20:02 +01:00
2018-12-04 11:26:28 +01:00
void API::http::get_headers(string &headers) const
2018-02-25 23:20:02 +01:00
{
headers = _headers;
}
2018-02-26 07:57:30 +01:00
2018-12-04 11:26:28 +01:00
size_t API::http::callback_write(char* data, size_t size, size_t nmemb,
string *str)
{
std::lock_guard<std::mutex> lock(_mutex);
str->append(data, size * nmemb);
// ttdebug << "Received " << size * nmemb << " Bytes\n";
return size * nmemb;
2018-12-04 11:26:28 +01:00
}
double API::http::callback_progress(double /* dltotal */, double /* dlnow */,
double /* ultotal */, double /* ulnow */)
{
if (_cancel_stream)
2018-02-26 07:57:30 +01:00
{
// This throws the runtime error: Callback aborted
return 1;
2018-02-26 07:57:30 +01:00
}
return 0;
2018-12-04 11:26:28 +01:00
}
2018-02-26 07:57:30 +01:00
2018-12-04 11:26:28 +01:00
void API::http::cancel_stream()
{
_cancel_stream = true;
2019-04-10 02:25:55 +02:00
_streamthread.join();
}
2018-05-17 17:59:44 +02:00
std::mutex &API::http::get_mutex()
{
return _mutex;
}