summaryrefslogtreecommitdiff
path: root/src/other_tools/utils/curl_easy_handle.cpp
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-02-15 14:47:15 +0100
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-02-17 16:27:50 +0100
commitebe7695ee5803dd3c2bb4f22f5e12d776c985d56 (patch)
tree9aca606caff43ed369d6afafa75a09ff5388c251 /src/other_tools/utils/curl_easy_handle.cpp
parentb6a4271feaa2ed9eaa553189183a74cb28c4b5fd (diff)
downloadjustbuild-ebe7695ee5803dd3c2bb4f22f5e12d776c985d56.tar.gz
structure cleanup: move libcurl utilities to other_tools...
...in order to not include unwanted dependencies in just proper. As the whole other_tools folder is meant to be excluded from bootstrapping, also remove the bootstrap guards.
Diffstat (limited to 'src/other_tools/utils/curl_easy_handle.cpp')
-rw-r--r--src/other_tools/utils/curl_easy_handle.cpp151
1 files changed, 151 insertions, 0 deletions
diff --git a/src/other_tools/utils/curl_easy_handle.cpp b/src/other_tools/utils/curl_easy_handle.cpp
new file mode 100644
index 00000000..29e7670e
--- /dev/null
+++ b/src/other_tools/utils/curl_easy_handle.cpp
@@ -0,0 +1,151 @@
+// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/other_tools/utils/curl_easy_handle.hpp"
+
+#include <fstream>
+
+#include "src/buildtool/file_system/file_system_manager.hpp"
+#include "src/buildtool/logging/logger.hpp"
+
+extern "C" {
+#include "curl/curl.h"
+}
+
+void curl_easy_closer(gsl::owner<CURL*> curl) {
+ curl_easy_cleanup(curl);
+}
+
+auto CurlEasyHandle::Create() noexcept -> std::shared_ptr<CurlEasyHandle> {
+ try {
+ auto curl = std::make_shared<CurlEasyHandle>();
+ auto* handle = curl_easy_init();
+ if (handle == nullptr) {
+ return nullptr;
+ }
+ curl->handle_.reset(handle);
+ return curl;
+ } catch (std::exception const& ex) {
+ Logger::Log(LogLevel::Error,
+ "create curl easy handle failed with:\n{}",
+ ex.what());
+ return nullptr;
+ }
+}
+
+auto CurlEasyHandle::EasyWriteToFile(gsl::owner<char*> data,
+ size_t size,
+ size_t nmemb,
+ gsl::owner<void*> userptr)
+ -> std::streamsize {
+ auto actual_size = static_cast<std::streamsize>(size * nmemb);
+ auto* file = static_cast<std::ofstream*>(userptr);
+ file->write(data, actual_size); // append chunk
+ return actual_size;
+}
+
+auto CurlEasyHandle::EasyWriteToString(gsl::owner<char*> data,
+ size_t size,
+ size_t nmemb,
+ gsl::owner<void*> userptr)
+ -> std::streamsize {
+ size_t actual_size = size * nmemb;
+ (static_cast<std::string*>(userptr))->append(data, actual_size);
+ return static_cast<std::streamsize>(actual_size);
+}
+
+auto CurlEasyHandle::DownloadToFile(
+ std::string const& url,
+ std::filesystem::path const& file_path) noexcept -> int {
+ try {
+ // set URL
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_URL, url.c_str());
+
+ // ensure redirects are allowed, otherwise it might simply read empty
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_FOLLOWLOCATION, 1);
+
+ // set callback for writing to file
+ std::ofstream file(file_path.c_str(), std::ios::binary);
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_WRITEFUNCTION, EasyWriteToFile);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(
+ handle_.get(), CURLOPT_WRITEDATA, static_cast<void*>(&file));
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_VERBOSE, 1);
+
+ // perform download
+ auto res = curl_easy_perform(handle_.get());
+
+ // close file
+ file.close();
+
+ // check result
+ if (res != CURLE_OK) {
+ // cleanup failed downloaded file, if created
+ [[maybe_unused]] auto tmp_res =
+ FileSystemManager::RemoveFile(file_path);
+ }
+ return res;
+ } catch (std::exception const& ex) {
+ Logger::Log(LogLevel::Error,
+ "curl download to file failed with:\n{}",
+ ex.what());
+ return 1;
+ }
+}
+
+auto CurlEasyHandle::DownloadToString(std::string const& url) noexcept
+ -> std::optional<std::string> {
+ try {
+ // set URL
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_URL, url.c_str());
+
+ // ensure redirects are allowed, otherwise it might simply read empty
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_FOLLOWLOCATION, 1);
+
+ // set callback for writing to string
+ std::string content{};
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(
+ handle_.get(), CURLOPT_WRITEFUNCTION, EasyWriteToString);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(
+ handle_.get(), CURLOPT_WRITEDATA, static_cast<void*>(&content));
+
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
+ curl_easy_setopt(handle_.get(), CURLOPT_VERBOSE, 1);
+
+ // perform download
+ auto res = curl_easy_perform(handle_.get());
+
+ // check result
+ if (res != CURLE_OK) {
+ return std::nullopt;
+ }
+ return content;
+ } catch (std::exception const& ex) {
+ Logger::Log(LogLevel::Error,
+ "curl download to string failed with:\n{}",
+ ex.what());
+ return std::nullopt;
+ }
+}