summaryrefslogtreecommitdiff
path: root/src/utils/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/cpp')
-rw-r--r--src/utils/cpp/TARGETS21
-rw-r--r--src/utils/cpp/curl_context.cpp40
-rw-r--r--src/utils/cpp/curl_context.hpp36
-rw-r--r--src/utils/cpp/curl_easy_handle.cpp175
-rw-r--r--src/utils/cpp/curl_easy_handle.hpp86
5 files changed, 0 insertions, 358 deletions
diff --git a/src/utils/cpp/TARGETS b/src/utils/cpp/TARGETS
index 4bc79e8e..42d8ae9e 100644
--- a/src/utils/cpp/TARGETS
+++ b/src/utils/cpp/TARGETS
@@ -58,27 +58,6 @@
, "stage": ["src", "utils", "cpp"]
, "private-deps": [["src/buildtool/file_system", "file_system_manager"]]
}
-, "curl_context":
- { "type": ["@", "rules", "CC", "library"]
- , "name": ["curl_context"]
- , "hdrs": ["curl_context.hpp"]
- , "srcs": ["curl_context.cpp"]
- , "stage": ["src", "utils", "cpp"]
- , "private-deps": [["src/buildtool/logging", "logging"], ["", "libcurl"]]
- }
-, "curl_easy_handle":
- { "type": ["@", "rules", "CC", "library"]
- , "name": ["curl_easy_handle"]
- , "hdrs": ["curl_easy_handle.hpp"]
- , "srcs": ["curl_easy_handle.cpp"]
- , "deps": ["curl_context", ["@", "gsl-lite", "", "gsl-lite"]]
- , "stage": ["src", "utils", "cpp"]
- , "private-deps":
- [ ["src/buildtool/logging", "logging"]
- , ["src/buildtool/file_system", "file_system_manager"]
- , ["", "libcurl"]
- ]
- }
, "file_locking":
{ "type": ["@", "rules", "CC", "library"]
, "name": ["file_locking"]
diff --git a/src/utils/cpp/curl_context.cpp b/src/utils/cpp/curl_context.cpp
deleted file mode 100644
index 3369b2d3..00000000
--- a/src/utils/cpp/curl_context.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-// 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/utils/cpp/curl_context.hpp"
-
-#include "src/buildtool/logging/logger.hpp"
-
-#ifndef BOOTSTRAP_BUILD_TOOL
-extern "C" {
-#include <curl/curl.h>
-}
-#endif // BOOTSTRAP_BUILD_TOOL
-
-CurlContext::CurlContext() noexcept {
-#ifndef BOOTSTRAP_BUILD_TOOL
- // NOLINTNEXTLINE(hicpp-signed-bitwise)
- if (not(initialized_ = (curl_global_init(CURL_GLOBAL_DEFAULT) >= 0))) {
- Logger::Log(LogLevel::Error, "initializing libcurl failed");
- }
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-CurlContext::~CurlContext() noexcept {
-#ifndef BOOTSTRAP_BUILD_TOOL
- if (initialized_) {
- curl_global_cleanup();
- }
-#endif // BOOTSTRAP_BUILD_TOOL
-} \ No newline at end of file
diff --git a/src/utils/cpp/curl_context.hpp b/src/utils/cpp/curl_context.hpp
deleted file mode 100644
index e408b8bf..00000000
--- a/src/utils/cpp/curl_context.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-// 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.
-
-#ifndef INCLUDED_SRC_OTHER_TOOLS_CURL_CONTEXT_HPP
-#define INCLUDED_SRC_OTHER_TOOLS_CURL_CONTEXT_HPP
-
-/// \brief Maintainer of a libcurl state.
-/// Classes, static methods, and global functions dealing with curl operations
-/// should create a CurlContext before using libcurl.
-class CurlContext {
- public:
- // prohibit moves and copies
- CurlContext(CurlContext const&) = delete;
- CurlContext(CurlContext&& other) = delete;
- auto operator=(CurlContext const&) = delete;
- auto operator=(CurlContext&& other) = delete;
-
- CurlContext() noexcept;
- ~CurlContext() noexcept;
-
- private:
- bool initialized_{false};
-};
-
-#endif // INCLUDED_SRC_OTHER_TOOLS_CURL_CONTEXT_HPP \ No newline at end of file
diff --git a/src/utils/cpp/curl_easy_handle.cpp b/src/utils/cpp/curl_easy_handle.cpp
deleted file mode 100644
index 7fac31fc..00000000
--- a/src/utils/cpp/curl_easy_handle.cpp
+++ /dev/null
@@ -1,175 +0,0 @@
-// 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/utils/cpp/curl_easy_handle.hpp"
-
-#include <fstream>
-
-#include "src/buildtool/file_system/file_system_manager.hpp"
-#include "src/buildtool/logging/logger.hpp"
-
-#ifndef BOOTSTRAP_BUILD_TOOL
-extern "C" {
-#include "curl/curl.h"
-}
-#endif // BOOTSTRAP_BUILD_TOOL
-
-void curl_easy_closer(gsl::owner<CURL*> curl) {
-#ifndef BOOTSTRAP_BUILD_TOOL
- curl_easy_cleanup(curl);
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-auto CurlEasyHandle::Create() noexcept -> std::shared_ptr<CurlEasyHandle> {
-#ifdef BOOTSTRAP_BUILD_TOOL
- return nullptr;
-#else
- 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;
- }
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-auto CurlEasyHandle::EasyWriteToFile(gsl::owner<char*> data,
- size_t size,
- size_t nmemb,
- gsl::owner<void*> userptr)
- -> std::streamsize {
-#ifdef BOOTSTRAP_BUILD_TOOL
- return 0;
-#else
- 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;
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-auto CurlEasyHandle::EasyWriteToString(gsl::owner<char*> data,
- size_t size,
- size_t nmemb,
- gsl::owner<void*> userptr)
- -> std::streamsize {
-#ifdef BOOTSTRAP_BUILD_TOOL
- return 0;
-#else
- size_t actual_size = size * nmemb;
- (static_cast<std::string*>(userptr))->append(data, actual_size);
- return static_cast<std::streamsize>(actual_size);
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-auto CurlEasyHandle::DownloadToFile(
- std::string const& url,
- std::filesystem::path const& file_path) noexcept -> int {
-#ifdef BOOTSTRAP_BUILD_TOOL
- return 1;
-#else
- 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;
- }
-#endif // BOOTSTRAP_BUILD_TOOL
-}
-
-auto CurlEasyHandle::DownloadToString(std::string const& url) noexcept
- -> std::optional<std::string> {
-#ifdef BOOTSTRAP_BUILD_TOOL
- return std::nullopt;
-#else
- 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;
- }
-#endif // BOOTSTRAP_BUILD_TOOL
-}
diff --git a/src/utils/cpp/curl_easy_handle.hpp b/src/utils/cpp/curl_easy_handle.hpp
deleted file mode 100644
index 5f318887..00000000
--- a/src/utils/cpp/curl_easy_handle.hpp
+++ /dev/null
@@ -1,86 +0,0 @@
-// 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.
-
-#ifndef INCLUDED_SRC_OTHER_TOOLS_CURL_EASY_HANDLE_HPP
-#define INCLUDED_SRC_OTHER_TOOLS_CURL_EASY_HANDLE_HPP
-
-#include <filesystem>
-#include <functional>
-#include <memory>
-#include <optional>
-
-#include "gsl-lite/gsl-lite.hpp"
-#include "src/utils/cpp/curl_context.hpp"
-
-extern "C" {
-#if defined(BUILDING_LIBCURL) || defined(CURL_STRICTER)
-using CURL = struct Curl_easy;
-#else
-using CURL = void;
-#endif
-}
-
-void curl_easy_closer(gsl::owner<CURL*> curl);
-
-class CurlEasyHandle {
- public:
- CurlEasyHandle() noexcept = default;
- ~CurlEasyHandle() noexcept = default;
-
- // prohibit moves and copies
- CurlEasyHandle(CurlEasyHandle const&) = delete;
- CurlEasyHandle(CurlEasyHandle&& other) = delete;
- auto operator=(CurlEasyHandle const&) = delete;
- auto operator=(CurlEasyHandle&& other) = delete;
-
- /// \brief Create a CurlEasyHandle object
- [[nodiscard]] auto static Create() noexcept
- -> std::shared_ptr<CurlEasyHandle>;
-
- /// \brief Download file from URL into given file_path.
- /// Will perform cleanup (i.e., remove empty file) in case download fails.
- /// Returns 0 if successful.
- [[nodiscard]] auto DownloadToFile(
- std::string const& url,
- std::filesystem::path const& file_path) noexcept -> int;
-
- /// \brief Download file from URL into string as binary.
- /// Returns the content or nullopt if download failure.
- [[nodiscard]] auto DownloadToString(std::string const& url) noexcept
- -> std::optional<std::string>;
-
- private:
- // IMPORTANT: the CurlContext must to be initialized before any curl object!
- CurlContext curl_context_{};
- std::unique_ptr<CURL, decltype(&curl_easy_closer)> handle_{
- nullptr,
- curl_easy_closer};
-
- /// \brief Overwrites write_callback to redirrect to file instead of stdout.
- [[nodiscard]] auto static EasyWriteToFile(gsl::owner<char*> data,
- size_t size,
- size_t nmemb,
- gsl::owner<void*> userptr)
- -> std::streamsize;
-
- /// \brief Overwrites write_callback to redirect to string instead of
- /// stdout.
- [[nodiscard]] auto static EasyWriteToString(gsl::owner<char*> data,
- size_t size,
- size_t nmemb,
- gsl::owner<void*> userptr)
- -> std::streamsize;
-};
-
-#endif // INCLUDED_SRC_OTHER_TOOLS_CURL_EASY_HANDLE_HPP \ No newline at end of file