Add set_useragent().
This commit is contained in:
parent
04526f37cc
commit
1b4ad05acb
@ -196,8 +196,10 @@ public:
|
|||||||
*
|
*
|
||||||
* @since 0.3.0
|
* @since 0.3.0
|
||||||
*/
|
*/
|
||||||
void setup_connection_properties(string_view proxy, string_view access_token,
|
void setup_connection_properties(string_view proxy,
|
||||||
string_view cainfo);
|
string_view access_token,
|
||||||
|
string_view cainfo,
|
||||||
|
string_view useragent);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*!
|
/*!
|
||||||
@ -275,6 +277,8 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void set_cainfo(string_view path);
|
void set_cainfo(string_view path);
|
||||||
|
|
||||||
|
void set_useragent(string_view useragent);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CURL *_connection;
|
CURL *_connection;
|
||||||
char _curl_buffer_error[CURL_ERROR_SIZE];
|
char _curl_buffer_error[CURL_ERROR_SIZE];
|
||||||
|
@ -73,7 +73,8 @@ public:
|
|||||||
*/
|
*/
|
||||||
inline void copy_connection_properties(CURLWrapper &curlwrapper)
|
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;
|
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.
|
* @brief Simplifies obtaining an OAuth 2.0 Bearer Access Token.
|
||||||
*
|
*
|
||||||
@ -304,6 +311,7 @@ private:
|
|||||||
string _proxy;
|
string _proxy;
|
||||||
vector<string> _post_formats;
|
vector<string> _post_formats;
|
||||||
string _cainfo;
|
string _cainfo;
|
||||||
|
string _useragent;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mastodonpp
|
} // namespace mastodonpp
|
||||||
|
@ -187,9 +187,10 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri,
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CURLWrapper::setup_connection_properties(string_view proxy,
|
void CURLWrapper::setup_connection_properties(const string_view proxy,
|
||||||
string_view access_token,
|
const string_view access_token,
|
||||||
string_view cainfo)
|
const string_view cainfo,
|
||||||
|
const string_view useragent)
|
||||||
{
|
{
|
||||||
if (!proxy.empty())
|
if (!proxy.empty())
|
||||||
{
|
{
|
||||||
@ -205,6 +206,11 @@ void CURLWrapper::setup_connection_properties(string_view proxy,
|
|||||||
{
|
{
|
||||||
set_cainfo(cainfo);
|
set_cainfo(cainfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!useragent.empty())
|
||||||
|
{
|
||||||
|
set_useragent(useragent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CURLWrapper::set_proxy(const string_view proxy)
|
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)
|
size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb)
|
||||||
{
|
{
|
||||||
if(data == nullptr)
|
if(data == nullptr)
|
||||||
@ -316,18 +335,11 @@ void CURLWrapper::setup_curl()
|
|||||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||||
curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L);
|
curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L);
|
||||||
|
|
||||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
set_useragent((string("mastodonpp/") += version));
|
||||||
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};
|
|
||||||
}
|
|
||||||
|
|
||||||
// The next 2 only fail if HTTP is not supported.
|
// The next 2 only fail if HTTP is not supported.
|
||||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
// 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)
|
if (code != CURLE_OK)
|
||||||
{
|
{
|
||||||
throw CURLException{code, "HTTP is not supported.", _curl_buffer_error};
|
throw CURLException{code, "HTTP is not supported.", _curl_buffer_error};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user