diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/connection.cpp | 10 | ||||
-rw-r--r-- | src/curl_wrapper.cpp | 40 |
3 files changed, 51 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cdb651b..b9ede8d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ include(GNUInstallDirs) -find_package(CURL REQUIRED) +find_package(CURL 7.32 REQUIRED) # Write version in header. configure_file ("version.hpp.in" diff --git a/src/connection.cpp b/src/connection.cpp index fad710a..3aaa236 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -48,4 +48,14 @@ void Connection::set_proxy(const string_view proxy) _instance.set_proxy(proxy); } +string Connection::get_new_stream_contents() +{ + buffer_mutex.lock(); + auto &buffer{get_buffer()}; + auto buffer_copy{buffer}; + buffer.clear(); + buffer_mutex.unlock(); + return buffer_copy; +} + } // namespace mastodonpp diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index b72e9e9..b100d21 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -40,6 +40,7 @@ static atomic<uint16_t> curlwrapper_instances{0}; CURLWrapper::CURLWrapper() : _curl_buffer_error{} + , _stream_cancelled(false) { if (curlwrapper_instances == 0) { @@ -73,9 +74,16 @@ void CURLWrapper::set_proxy(const string_view proxy) } } +void CURLWrapper::cancel_stream() +{ + _stream_cancelled = true; +} + answer_type CURLWrapper::make_request(const http_method &method, string uri, const parametermap ¶meters) { + _stream_cancelled = false; + CURLcode code; switch (method) { @@ -158,7 +166,9 @@ size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb) return 0; } + buffer_mutex.lock(); _curl_buffer_body.append(data, size * nmemb); + buffer_mutex.unlock(); return size * nmemb; } @@ -175,6 +185,16 @@ size_t CURLWrapper::writer_header(char *data, size_t size, size_t nmemb) return size * nmemb; } +int CURLWrapper::progress(void *, curl_off_t , curl_off_t , + curl_off_t , curl_off_t ) +{ + if (_stream_cancelled) + { + return 1; + } + return 0; +} + void CURLWrapper::setup_curl() { if (_connection == nullptr) @@ -225,6 +245,26 @@ void CURLWrapper::setup_curl() } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + code = curl_easy_setopt(_connection, CURLOPT_XFERINFOFUNCTION, + progress_wrapper); + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set transfer info function", + _curl_buffer_error}; + } + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + curl_easy_setopt(_connection, CURLOPT_XFERINFODATA, this); + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set transfer info data", + _curl_buffer_error}; + } + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) code = curl_easy_setopt(_connection, CURLOPT_USERAGENT, (string("mastorss/") += version).c_str()); if (code != CURLE_OK) |