summaryrefslogtreecommitdiffstats
path: root/src/curl_wrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/curl_wrapper.cpp')
-rw-r--r--src/curl_wrapper.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp
new file mode 100644
index 0000000..ef724b9
--- /dev/null
+++ b/src/curl_wrapper.cpp
@@ -0,0 +1,90 @@
+/* This file is part of mastodonpp.
+ * Copyright © 2020 tastytea <tastytea@tastytea.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "curl_wrapper.hpp"
+#include "exceptions.hpp"
+
+namespace mastodonpp
+{
+
+bool curl_initialized{false};
+
+CURLWrapper::CURLWrapper()
+ : _curl_buffer_error{}
+{
+ if (!curl_initialized)
+ {
+ curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
+ curl_initialized = true;
+ }
+ _connection = curl_easy_init();
+ setup_curl();
+}
+CURLWrapper::~CURLWrapper() noexcept
+{
+ curl_easy_cleanup(_connection);
+
+ if (curl_initialized)
+ {
+ curl_global_cleanup();
+ curl_initialized = false;
+ }
+}
+
+int CURLWrapper::writer(char *data, size_t size, size_t nmemb, string *writerData)
+{
+ if(writerData == nullptr)
+ {
+ return 0;
+ }
+
+ writerData->append(data, size*nmemb);
+
+ return static_cast<int>(size * nmemb);
+}
+
+void CURLWrapper::setup_curl()
+{
+ if (_connection == nullptr)
+ {
+ throw CURLException{CURLE_FAILED_INIT, "Failed to initialize curl."};
+ }
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ CURLcode code{curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER,
+ _curl_buffer_error)};
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to set error buffer."};
+ }
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ code = curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer);
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to set writer", _curl_buffer_error};
+ }
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
+ code = curl_easy_setopt(_connection, CURLOPT_WRITEDATA, &_curl_buffer);
+ if (code != CURLE_OK)
+ {
+ throw CURLException{code, "Failed to set write data",
+ _curl_buffer_error};
+ }
+}
+
+} // namespace mastodonpp