/* This file is part of mastodon-cpp. * Copyright © 2018, 2019 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 // std::bind #include #include // std::strncmp #include #include #include #include #include #include "debug.hpp" #include "mastodon-cpp.hpp" using namespace Mastodon; namespace curlopts = curlpp::options; using std::cerr; API::http::http(const API &api, const string &instance, const string &access_token) : parent(api) , _instance(instance) , _access_token(access_token) , _cancel_stream(false) { curlpp::initialize(); } API::http::~http() { curlpp::terminate(); } return_call API::http::request(const http_method &meth, const string &path) { return request(meth, path, curlpp::Forms()); } return_call API::http::request(const http_method &meth, const string &path, const curlpp::Forms &formdata) { 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) { using namespace std::placeholders; // _1, _2, _3 ttdebug << "Path is: " << path << '\n'; try { curlpp::Easy request; std::list headers; request.setOpt("https://" + _instance + path); ttdebug << "User-Agent: " << parent.get_useragent() << "\n"; request.setOpt(parent.get_useragent()); { string proxy; string userpw; parent.get_proxy(proxy, userpw); if (!proxy.empty()) { request.setOpt(proxy); if (!userpw.empty()) { request.setOpt(userpw); } } } if (!_access_token.empty()) { headers.push_back("Authorization: Bearer " + _access_token); } if (meth != http_method::GET_STREAM) { headers.push_back("Connection: close"); // Get headers from server request.setOpt(true); } request.setOpt(headers); request.setOpt(true); request.setOpt (std::bind(&http::callback_write, this, _1, _2, _3, &answer)); request.setOpt (std::bind(&http::callback_progress, this, _1, _2, _3, _4)); request.setOpt(0); if (!formdata.empty()) { request.setOpt(formdata); } switch (meth) { case http_method::GET: case http_method::GET_STREAM: break; case http_method::PATCH: request.setOpt("PATCH"); break; case http_method::POST: request.setOpt("POST"); break; case http_method::PUT: request.setOpt("PUT"); break; case http_method::DELETE: request.setOpt("DELETE"); break; } //request.setOpt(true); answer.clear(); request.perform(); 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); _headers = answer.substr(0, pos); // Only return body answer = answer.substr(pos + 4); if (http_code == 200 || http_code == 302 || http_code == 307) { // OK or Found or Temporary Redirect return { 0, "", http_code, answer }; } else if (http_code == 301 || http_code == 308) { // Moved Permanently or Permanent Redirect // return new URL answer = curlpp::infos::EffectiveUrl::get(request); return { 78, "Remote address changed", http_code, answer }; } else if (http_code == 0) { return { 255, "Unknown error", http_code, answer }; } else { return { 111, "Connection refused", http_code, answer }; } } catch (curlpp::RuntimeError &e) { const string what = e.what(); // This error is thrown if http.cancel_stream() is used. if ((what.compare(0, 16, "Callback aborted") == 0) || (what.compare(0, 19, "Failed writing body") == 0)) { ttdebug << "Request was cancelled by user\n"; return { 0, "Request was cancelled by user", 0, "" }; } else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0) { ttdebug << "Timeout\n"; return { 110, "Connection timed out", 0, "" }; } if (parent.exceptions()) { std::rethrow_exception(std::current_exception()); } else { ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl; return { 192, e.what(), 0, "" }; } } catch (curlpp::LogicError &e) { if (parent.exceptions()) { std::rethrow_exception(std::current_exception()); } ttdebug << "curlpp::LogicError: " << e.what() << std::endl; return { 193, e.what(), 0, "" }; } } void API::http::get_headers(string &headers) const { headers = _headers; } size_t API::http::callback_write(char* data, size_t size, size_t nmemb, string *str) { std::lock_guard lock(_mutex); str->append(data, size * nmemb); // ttdebug << "Received " << size * nmemb << " Bytes\n"; return size * nmemb; } double API::http::callback_progress(double /* dltotal */, double /* dlnow */, double /* ultotal */, double /* ulnow */) { if (_cancel_stream) { // This throws the runtime error: Callback aborted return 1; } return 0; } void API::http::cancel_stream() { _cancel_stream = true; _streamthread.join(); } std::mutex &API::http::get_mutex() { return _mutex; }