From d342edfd21653381e6f9d0b8a03058e0bbaab17a Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 20 May 2018 12:29:04 +0200 Subject: [PATCH] added http::callback_progress() to cancel transfers quicker and more reliably --- CMakeLists.txt | 2 +- README.md | 4 ++++ src/http.cpp | 34 +++++++++++++++++++++++++--------- src/mastodon-cpp.hpp | 5 +++++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d26b6e..98bf921 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.13.0 + VERSION 0.13.1 LANGUAGES CXX ) diff --git a/README.md b/README.md index 936eec1..24f4eda 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/exampl ## Upgrading from below 0.10.0 +You have to recompile all applications linking against this library. + +## Upgrading from below 0.10.0 + `Mastodon::API::get`, `::get_stream`, `::post`, `::put` and `::del` don't take `std::string` as parameter to API-calls anymore, only `parametermap`s. The old behaviour is still supported but is deprecated and will be removed in version 1.0.0. diff --git a/src/http.cpp b/src/http.cpp index 6095cc6..1e62404 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -84,7 +84,10 @@ const uint_fast16_t API::http::request(const method &meth, request.setOpt(headers); request.setOpt(true); request.setOpt - (std::bind(&http::callback, this, _1, _2, _3, &answer)); + (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); @@ -138,7 +141,8 @@ const uint_fast16_t API::http::request(const method &meth, catch (curlpp::RuntimeError &e) { // This error is thrown if http.cancel_stream() is used. - if (std::strncmp(e.what(), "Failed writing body", 19) == 0) + if ((std::strncmp(e.what(), "Callback aborted", 16) == 0) || + (std::strncmp(e.what(), "Failed writing body", 19) == 0)) { ttdebug << "Request was cancelled by user\n"; return 14; @@ -173,20 +177,32 @@ const void API::http::get_headers(string &headers) const headers = _headers; } -const size_t API::http::callback(char* data, size_t size, size_t nmemb, - string *str) +const size_t API::http::callback_write(char* data, size_t size, size_t nmemb, + string *str) { std::lock_guard lock(_mutex); - if (_cancel_stream) - { - // This throws the runtime error: Failed writing body - return 0; - } str->append(data, size * nmemb); // ttdebug << "Received " << size * nmemb << " Bytes\n"; return size * nmemb; }; +const size_t API::http::callback(char* data, size_t size, size_t nmemb, + string *str) +{ + return callback_write(data, size, nmemb, str); +}; + +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; +}; + const void API::http::cancel_stream() { _cancel_stream = true; diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 299f39c..0a73f45 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -154,8 +154,13 @@ public: bool _cancel_stream; std::mutex _mutex; + const size_t callback_write(char* data, size_t size, size_t nmemb, + string *oss); + [[deprecated("Will vanish in 1.0.0. Use callback_write() instead.")]] const size_t callback(char* data, size_t size, size_t nmemb, string *oss); + double callback_progress(double /* dltotal */, double /* dlnow */, + double /* ultotal */, double /* ulnow */); }; /*!