From 1b4ad05acb1c3605c3a6949a746228d31da5e9da Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 12 Jan 2020 17:35:11 +0100 Subject: [PATCH] Add set_useragent(). --- include/curl_wrapper.hpp | 8 ++++++-- include/instance.hpp | 10 +++++++++- src/curl_wrapper.cpp | 36 ++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 0759e01..30fa1ce 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -196,8 +196,10 @@ public: * * @since 0.3.0 */ - void setup_connection_properties(string_view proxy, string_view access_token, - string_view cainfo); + void setup_connection_properties(string_view proxy, + string_view access_token, + string_view cainfo, + string_view useragent); protected: /*! @@ -275,6 +277,8 @@ protected: */ void set_cainfo(string_view path); + void set_useragent(string_view useragent); + private: CURL *_connection; char _curl_buffer_error[CURL_ERROR_SIZE]; diff --git a/include/instance.hpp b/include/instance.hpp index 85756e9..198746c 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -73,7 +73,8 @@ public: */ inline void copy_connection_properties(CURLWrapper &curlwrapper) { - curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo); + curlwrapper.setup_connection_properties(_proxy, _access_token, _cainfo, + _useragent); } /*! @@ -213,6 +214,12 @@ public: return _cainfo; } + void set_useragent(const string_view useragent) + { + _useragent = useragent; + CURLWrapper::set_useragent(useragent); + } + /*! * @brief Simplifies obtaining an OAuth 2.0 Bearer Access Token. * @@ -304,6 +311,7 @@ private: string _proxy; vector _post_formats; string _cainfo; + string _useragent; }; } // namespace mastodonpp diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index d5d96be..0fb0540 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -187,9 +187,10 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri, return answer; } -void CURLWrapper::setup_connection_properties(string_view proxy, - string_view access_token, - string_view cainfo) +void CURLWrapper::setup_connection_properties(const string_view proxy, + const string_view access_token, + const string_view cainfo, + const string_view useragent) { if (!proxy.empty()) { @@ -205,6 +206,11 @@ void CURLWrapper::setup_connection_properties(string_view proxy, { set_cainfo(cainfo); } + + if (!useragent.empty()) + { + set_useragent(useragent); + } } void CURLWrapper::set_proxy(const string_view proxy) @@ -252,6 +258,19 @@ void CURLWrapper::set_cainfo(const string_view path) } } +void CURLWrapper::set_useragent(const string_view useragent) +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + CURLcode code{curl_easy_setopt(_connection, CURLOPT_USERAGENT, + useragent.data())}; + if (code != CURLE_OK) + { + throw CURLException{code, "Failed to set User-Agent", + _curl_buffer_error}; + } + debuglog << "Set User-Agent to: " << useragent << '\n'; +} + size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb) { if(data == nullptr) @@ -316,18 +335,11 @@ void CURLWrapper::setup_curl() // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - CURLcode code{curl_easy_setopt(_connection, CURLOPT_USERAGENT, - (string("mastodonpp/") += version).c_str())}; - if (code != CURLE_OK) - { - throw CURLException{code, "Failed to set User-Agent", - _curl_buffer_error}; - } + set_useragent((string("mastodonpp/") += version)); // The next 2 only fail if HTTP is not supported. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - code = curl_easy_setopt(_connection, CURLOPT_FOLLOWLOCATION, 1L); + CURLcode code{curl_easy_setopt(_connection, CURLOPT_FOLLOWLOCATION, 1L)}; if (code != CURLE_OK) { throw CURLException{code, "HTTP is not supported.", _curl_buffer_error};