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-01-17 23:51:59 +01:00
|
|
|
#include <sstream>
|
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>
|
2018-01-13 15:49:46 +01:00
|
|
|
#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-09 16:01:24 +01:00
|
|
|
curlpp::initialize();
|
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-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
|
|
|
std::ostringstream oss;
|
|
|
|
curlpp::Easy request;
|
|
|
|
request.setOpt<curlopts::Url>("https://" + _instance + path);
|
|
|
|
request.setOpt<curlopts::UserAgent>(parent.get_useragent());
|
|
|
|
request.setOpt<curlopts::HttpHeader>(
|
2018-01-09 22:12:11 +01:00
|
|
|
{
|
2018-02-09 16:01:24 +01:00
|
|
|
"Connection: close",
|
|
|
|
"Authorization: Bearer " + _access_token
|
|
|
|
});
|
2018-02-10 12:01:55 +01:00
|
|
|
request.setOpt<curlopts::FollowLocation>(true);
|
|
|
|
request.setOpt<curlopts::WriteStream>(&oss);
|
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
|
|
|
|
2018-01-21 23:54:42 +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");
|
2018-01-21 23:54:42 +01:00
|
|
|
break;
|
|
|
|
case http::method::POST:
|
2018-02-09 16:01:24 +01:00
|
|
|
request.setOpt<curlopts::CustomRequest>("POST");
|
|
|
|
break;
|
2018-01-24 18:52:24 +01:00
|
|
|
case http::method::PUT:
|
2018-02-09 16:01:24 +01:00
|
|
|
request.setOpt<curlopts::CustomRequest>("PUT");
|
2018-01-24 18:52:24 +01:00
|
|
|
case http::method::DELETE:
|
2018-02-09 16:01:24 +01:00
|
|
|
request.setOpt<curlopts::CustomRequest>("DELETE");
|
2018-01-21 23:54:42 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-02-09 16:01:24 +01:00
|
|
|
|
2018-02-10 12:01:55 +01:00
|
|
|
request.perform();
|
|
|
|
std::uint16_t ret = curlpp::infos::ResponseCode::get(request);
|
|
|
|
ttdebug << "Response code: " << ret << '\n';
|
|
|
|
if (ret == 200 || ret == 302 || ret == 307)
|
|
|
|
{ // OK or Found or Temporary Redirect
|
|
|
|
answer = oss.str();
|
|
|
|
}
|
|
|
|
else if (ret == 301 || ret == 308)
|
2018-02-11 12:26:42 +01:00
|
|
|
{ // Moved Permanently or Permanent Redirect
|
2018-02-10 12:01:55 +01:00
|
|
|
// FIXME: The new URL should be passed back somehow
|
|
|
|
answer = oss.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-09 22:12:11 +01:00
|
|
|
}
|
2018-02-09 16:01:24 +01:00
|
|
|
catch (curlpp::RuntimeError &e)
|
|
|
|
{
|
|
|
|
cerr << "RUNTIME ERROR: " << e.what() << std::endl;
|
2018-02-11 12:26:42 +01:00
|
|
|
|
2018-02-09 16:01:24 +01:00
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
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 0;
|
|
|
|
}
|