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

136 lines
4.2 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-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>
#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-25 23:20:02 +01:00
// Get headers from server
request.setOpt<curlpp::options::Header>(true);
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
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
2018-02-10 12:01:55 +01:00
request.perform();
std::uint16_t ret = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << ret << '\n';
2018-02-25 23:20:02 +01:00
size_t pos = oss.str().find("\r\n\r\n");
_headers = oss.str().substr(0, pos);
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
answer = oss.str().substr(pos + 4);
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);
return 3;
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)
{
cerr << "RUNTIME ERROR: " << e.what() << std::endl;
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;
}
2018-02-25 23:20:02 +01:00
const void API::http::get_headers(string &headers) const
{
headers = _headers;
}