Add set_useragent().

This commit is contained in:
tastytea 2020-01-12 17:35:11 +01:00
parent 04526f37cc
commit 1b4ad05acb
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 39 additions and 15 deletions

View File

@ -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];

View File

@ -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<string> _post_formats;
string _cainfo;
string _useragent;
};
} // namespace mastodonpp

View File

@ -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};