added http::callback_progress() to cancel transfers quicker and more reliably
This commit is contained in:
parent
6b47feb732
commit
d342edfd21
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (mastodon-cpp
|
project (mastodon-cpp
|
||||||
VERSION 0.13.0
|
VERSION 0.13.1
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,10 @@ There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/exampl
|
||||||
|
|
||||||
## Upgrading from below 0.10.0
|
## 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
|
`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
|
`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.
|
still supported but is deprecated and will be removed in version 1.0.0.
|
||||||
|
|
32
src/http.cpp
32
src/http.cpp
|
@ -84,7 +84,10 @@ const uint_fast16_t API::http::request(const method &meth,
|
||||||
request.setOpt<curlopts::HttpHeader>(headers);
|
request.setOpt<curlopts::HttpHeader>(headers);
|
||||||
request.setOpt<curlopts::FollowLocation>(true);
|
request.setOpt<curlopts::FollowLocation>(true);
|
||||||
request.setOpt<curlopts::WriteFunction>
|
request.setOpt<curlopts::WriteFunction>
|
||||||
(std::bind(&http::callback, this, _1, _2, _3, &answer));
|
(std::bind(&http::callback_write, this, _1, _2, _3, &answer));
|
||||||
|
request.setOpt<curlopts::ProgressFunction>
|
||||||
|
(std::bind(&http::callback_progress, this, _1, _2, _3, _4));
|
||||||
|
request.setOpt<curlopts::NoProgress>(0);
|
||||||
if (!formdata.empty())
|
if (!formdata.empty())
|
||||||
{
|
{
|
||||||
request.setOpt<curlopts::HttpPost>(formdata);
|
request.setOpt<curlopts::HttpPost>(formdata);
|
||||||
|
@ -138,7 +141,8 @@ const uint_fast16_t API::http::request(const method &meth,
|
||||||
catch (curlpp::RuntimeError &e)
|
catch (curlpp::RuntimeError &e)
|
||||||
{
|
{
|
||||||
// This error is thrown if http.cancel_stream() is used.
|
// 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";
|
ttdebug << "Request was cancelled by user\n";
|
||||||
return 14;
|
return 14;
|
||||||
|
@ -173,20 +177,32 @@ const void API::http::get_headers(string &headers) const
|
||||||
headers = _headers;
|
headers = _headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t API::http::callback(char* data, size_t size, size_t nmemb,
|
const size_t API::http::callback_write(char* data, size_t size, size_t nmemb,
|
||||||
string *str)
|
string *str)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
if (_cancel_stream)
|
|
||||||
{
|
|
||||||
// This throws the runtime error: Failed writing body
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
str->append(data, size * nmemb);
|
str->append(data, size * nmemb);
|
||||||
// ttdebug << "Received " << size * nmemb << " Bytes\n";
|
// ttdebug << "Received " << size * nmemb << " Bytes\n";
|
||||||
return size * nmemb;
|
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()
|
const void API::http::cancel_stream()
|
||||||
{
|
{
|
||||||
_cancel_stream = true;
|
_cancel_stream = true;
|
||||||
|
|
|
@ -154,8 +154,13 @@ public:
|
||||||
bool _cancel_stream;
|
bool _cancel_stream;
|
||||||
std::mutex _mutex;
|
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,
|
const size_t callback(char* data, size_t size, size_t nmemb,
|
||||||
string *oss);
|
string *oss);
|
||||||
|
double callback_progress(double /* dltotal */, double /* dlnow */,
|
||||||
|
double /* ultotal */, double /* ulnow */);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
Reference in New Issue