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_sync.cpp

207 lines
6.1 KiB
C++
Raw Normal View History

2018-01-09 22:12:11 +01:00
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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 <string>
#include <cstdint>
#include <iostream>
2018-02-26 07:57:30 +01:00
#include <functional> // std::bind
#include <list>
#include <cstring> // std::strncmp
2018-02-09 16:01:24 +01:00
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
2018-02-10 12:01:55 +01:00
#include <curlpp/Infos.hpp>
#include "macros.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::string;
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)
2018-02-26 07:57:30 +01:00
, _abort_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();
}
2018-01-09 22:12:11 +01:00
const std::uint16_t API::http::request_sync(const method &meth,
const string &path,
string &answer)
{
2018-02-09 16:01:24 +01:00
return request_sync(meth, path, curlpp::Forms(), answer);
2018-01-09 22:12:11 +01:00
}
const std::uint16_t API::http::request_sync(const method &meth,
const string &path,
2018-02-09 16:01:24 +01:00
const curlpp::Forms &formdata,
2018-01-09 22:12:11 +01:00
string &answer)
{
2018-02-26 07:57:30 +01:00
using namespace std::placeholders; // _1, _2, _3
std::uint16_t ret = 0;
2018-01-17 23:51:59 +01:00
ttdebug << "Path is: " << path << '\n';
2018-02-09 16:01:24 +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
headers.push_back("Connection: close");
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);
}
request.setOpt<curlopts::HttpHeader>(headers);
2018-02-25 23:20:02 +01:00
// Get headers from server
2018-02-26 07:57:30 +01:00
if (meth != http::method::GET_STREAM)
{
request.setOpt<curlpp::options::Header>(true);
}
2018-02-10 12:01:55 +01:00
request.setOpt<curlopts::FollowLocation>(true);
2018-02-26 07:57:30 +01:00
request.setOpt<curlpp::options::WriteFunction>
(std::bind(&http::callback, this, _1, _2, _3, &answer));
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:
break;
case http::method::PATCH:
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::CustomRequest>("PATCH");
break;
case http::method::POST:
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::CustomRequest>("POST");
break;
case http::method::PUT:
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::CustomRequest>("PUT");
case http::method::DELETE:
2018-02-09 16:01:24 +01:00
request.setOpt<curlopts::CustomRequest>("DELETE");
default:
break;
}
2018-02-09 16:01:24 +01:00
//request.setOpt<curlopts::Verbose>(true);
answer.clear();
2018-02-10 12:01:55 +01:00
request.perform();
2018-02-26 07:57:30 +01:00
ret = curlpp::infos::ResponseCode::get(request);
2018-02-10 12:01:55 +01:00
ttdebug << "Response code: " << ret << '\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);
2018-02-25 23:20:02 +01:00
2018-02-10 12:01:55 +01:00
if (ret == 200 || ret == 302 || ret == 307)
{ // OK or Found or Temporary Redirect
2018-02-25 23:20:02 +01:00
// Only return body
2018-02-26 07:57:30 +01:00
answer = answer.substr(pos + 4);
return 0;
2018-02-10 12:01:55 +01:00
}
else if (ret == 301 || ret == 308)
{ // Moved Permanently or Permanent Redirect
2018-02-17 20:01:51 +01:00
// return new URL
answer = curlpp::infos::EffectiveUrl::get(request);
2018-02-28 22:37:30 +01:00
return 13;
2018-02-10 12:01:55 +01:00
}
else
{
return ret;
}
2018-01-09 22:12:11 +01:00
}
2018-02-09 16:01:24 +01:00
catch (curlpp::RuntimeError &e)
{
2018-02-26 07:57:30 +01:00
if (std::strncmp(e.what(),
"Failed writing body", 19) == 0)
{
ttdebug << "Request was aborted by user\n";
2018-02-28 22:37:30 +01:00
return 14;
2018-02-26 07:57:30 +01:00
}
else if (std::strncmp(e.what(),
"Failed to connect to", 20) == 0)
2018-02-26 07:57:30 +01:00
{
2018-02-28 22:37:30 +01:00
ret = 20;
2018-02-26 07:57:30 +01:00
}
else if (std::strncmp(e.what(),
2018-02-28 22:37:30 +01:00
"Couldn't resolve host", 21) == 0 ||
std::strncmp(e.what(),
"Could not resolve host", 22) == 0)
{
2018-02-28 22:37:30 +01:00
ret = 21;
}
2018-02-27 02:03:44 +01:00
else if (std::strncmp(e.what(),
"Network is unreachable", 22) == 0)
{
2018-02-28 22:37:30 +01:00
ret = 22;
2018-02-27 02:03:44 +01:00
}
2018-02-26 07:57:30 +01:00
else
{
cerr << "RUNTIME ERROR: " << e.what() << std::endl;
ret = 0xffff;
}
ttdebug << e.what() << std::endl;
2018-02-26 07:57:30 +01:00
return ret;
2018-02-09 16:01:24 +01:00
}
catch (curlpp::LogicError &e)
2018-01-09 22:12:11 +01:00
{
2018-02-09 16:01:24 +01:00
cerr << "LOGIC ERROR: " << e.what() << std::endl;
2018-01-09 22:12:11 +01:00
return 0xffff;
}
return ret;
2018-01-09 22:12:11 +01:00
}
2018-02-25 23:20:02 +01:00
const void API::http::get_headers(string &headers) const
{
headers = _headers;
}
2018-02-26 07:57:30 +01:00
const size_t API::http::callback(char* data, size_t size, size_t nmemb,
string *str)
{
if (_abort_stream)
{
// This throws the runtime error: Failed writing body
return 0;
}
str->append(data, size * nmemb);
2018-02-26 07:57:30 +01:00
// ttdebug << "Received " << size * nmemb << " Bytes\n";
return size * nmemb;
};
const void API::http::abort_stream()
{
_abort_stream = true;
}