/* This file is part of mastodon-cpp. * Copyright © 2018 tastytea * * 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 . */ #include #include #include #include #include #include #include #include #include #include "macros.hpp" #include "mastodon-cpp.hpp" using namespace Mastodon; namespace curlopts = curlpp::options; using std::string; using std::cerr; API::http::http(const API &api, const string &instance, const string &access_token) : parent(api) , _instance(instance) , _access_token(access_token) { curlpp::initialize(); } const std::uint16_t API::http::request_sync(const method &meth, const string &path, string &answer) { return request_sync(meth, path, curlpp::Forms(), answer); } const std::uint16_t API::http::request_sync(const method &meth, const string &path, const curlpp::Forms &formdata, string &answer) { ttdebug << "Path is: " << path << '\n'; try { std::ostringstream oss; curlpp::Easy request; request.setOpt("https://" + _instance + path); request.setOpt(parent.get_useragent()); request.setOpt( { "Connection: close", "Authorization: Bearer " + _access_token }); // Get headers from server request.setOpt(true); request.setOpt(true); request.setOpt(&oss); if (!formdata.empty()) { request.setOpt(formdata); } switch (meth) { case http::method::GET: break; case http::method::PATCH: request.setOpt("PATCH"); break; case http::method::POST: request.setOpt("POST"); break; case http::method::PUT: request.setOpt("PUT"); case http::method::DELETE: request.setOpt("DELETE"); default: break; } request.perform(); std::uint16_t ret = curlpp::infos::ResponseCode::get(request); ttdebug << "Response code: " << ret << '\n'; size_t pos = oss.str().find("\r\n\r\n"); _headers = oss.str().substr(0, pos); if (ret == 200 || ret == 302 || ret == 307) { // OK or Found or Temporary Redirect // Only return body answer = oss.str().substr(pos + 4); } else if (ret == 301 || ret == 308) { // Moved Permanently or Permanent Redirect // return new URL answer = curlpp::infos::EffectiveUrl::get(request); return 3; } else { return ret; } } catch (curlpp::RuntimeError &e) { cerr << "RUNTIME ERROR: " << e.what() << std::endl; return 0xffff; } catch (curlpp::LogicError &e) { cerr << "LOGIC ERROR: " << e.what() << std::endl; return 0xffff; } return 0; } const void API::http::get_headers(string &headers) const { headers = _headers; }