diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 118dfcb..cb5fc66 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -255,6 +255,14 @@ protected: */ void set_access_token(const string_view access_token); + + /*! + * @brief Set path to Certificate Authority (CA) bundle. + * + * @since 0.2.1 + */ + void set_cainfo(string_view path); + private: CURL *_connection; char _curl_buffer_error[CURL_ERROR_SIZE]; diff --git a/include/instance.hpp b/include/instance.hpp index 527483f..0667a83 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -166,6 +166,32 @@ public: */ vector get_post_formats(); + /*! + * @brief Set path to Certificate Authority (CA) bundle. + * + * Sets also the CA info for all Connection%s that are initialized with + * this Instance afterwards. + * + * @since 0.2.1 + */ + void set_cainfo(string_view path) + { + _cainfo = path; + CURLWrapper::set_cainfo(path); + } + + /*! + * @brief Returns the cainfo path that was previously set. + * + * This is used when initializing a Connection. + * + * @since 0.2.1 + */ + string_view get_cainfo() + { + return _cainfo; + } + private: const string _hostname; const string _baseuri; @@ -173,6 +199,7 @@ private: uint64_t _max_chars; string _proxy; vector _post_formats; + string _cainfo; }; } // namespace mastodonpp diff --git a/src/connection.cpp b/src/connection.cpp index 8512097..0eba695 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -35,6 +35,10 @@ Connection::Connection(Instance &instance) { CURLWrapper::set_access_token(_instance.get_access_token()); } + if (!_instance.get_cainfo().empty()) + { + CURLWrapper::set_cainfo(_instance.get_cainfo()); + } } string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index d224ef5..4cfb9b5 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -223,6 +223,15 @@ void CURLWrapper::set_access_token(const string_view access_token) debuglog << "Set authorization token.\n"; } +void CURLWrapper::set_cainfo(string_view path) +{ + CURLcode code{curl_easy_setopt(_connection, CURLOPT_CAINFO, path.data())}; + if (code != CURLE_OK) + { + throw CURLException{code, "Could not set CA info.", _curl_buffer_error}; + } +} + size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb) { if(data == nullptr)