Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide libcurl internally instead of wrapping #168

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/powerloader/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <chrono>
#include <map>
#include <filesystem>
#include <optional>

#include <powerloader/export.hpp>
#include <powerloader/mirrorid.hpp>
Expand Down
106 changes: 3 additions & 103 deletions include/powerloader/curl.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#ifndef POWERLOADER_CURL_HPP
#define POWERLOADER_CURL_HPP

#include <filesystem>
#include <functional>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <optional>

#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <tl/expected.hpp>

Expand All @@ -18,15 +12,9 @@ extern "C"
#include <curl/curl.h>
}

#include <powerloader/export.hpp>
#include <powerloader/utils.hpp>
#include <powerloader/enums.hpp>

namespace powerloader
{
class Context;
class CURLHandle;
using proxy_map_type = std::map<std::string, std::string>;

enum class ssl_backend_t
{
Expand All @@ -46,22 +34,11 @@ namespace powerloader
rustls = CURLSSLBACKEND_RUSTLS,
};

class POWERLOADER_API curl_error : public std::runtime_error
{
public:
curl_error(const std::string& what = "download error", bool serious = false);
bool is_serious() const;

private:
bool m_serious;
};

// This is supposed to be used outside powerloader
struct POWERLOADER_API Response
{
std::map<std::string, std::string> headers;

curl_off_t average_speed = -1;
curl_off_t downloaded_size = -1;
long http_status = 0;
std::string effective_url;

Expand All @@ -75,89 +52,12 @@ namespace powerloader
// `h.perform() method)
std::optional<std::string> content;
nlohmann::json json() const;
};

// TODO: rename this, try to not expose it
POWERLOADER_API CURL* get_handle(const Context& ctx);

class POWERLOADER_API CURLHandle
{
public:
using end_callback_type = std::function<CbReturnCode(const Response&)>;
explicit CURLHandle(const Context& ctx);
CURLHandle(const Context& ctx, const std::string& url);
~CURLHandle();

CURLHandle& url(const std::string& url, const proxy_map_type& proxies);
CURLHandle& accept_encoding();
CURLHandle& user_agent(const std::string& user_agent);

Response perform();
void finalize_transfer();
// TODO: should be private?
void finalize_transfer(Response& response);

template <class T>
tl::expected<T, CURLcode> getinfo(CURLINFO option);

// TODO: why do we need to expose these three methods
CURL* handle();
operator CURL*(); // TODO: consider making this `explicit` or remove it
CURL* ptr() const;

CURLHandle& add_header(const std::string& header);
CURLHandle& add_headers(const std::vector<std::string>& headers);
CURLHandle& reset_headers();

template <class T>
CURLHandle& setopt(CURLoption opt, const T& val);

void set_default_callbacks();
CURLHandle& set_end_callback(end_callback_type func);

CURLHandle& upload(std::ifstream& stream);
CURLHandle& upload(std::istringstream& stream);

CURLHandle(CURLHandle&& rhs);
CURLHandle& operator=(CURLHandle&& rhs);

private:
void init_handle(const Context& ctx);

CURL* m_handle;
curl_slist* p_headers = nullptr;
char errorbuffer[CURL_ERROR_SIZE];

std::unique_ptr<Response> response;
end_callback_type end_callback;
curl_off_t average_speed = -1;
curl_off_t downloaded_size = -1;
};

// TODO: restrict the possible implementations in the cpp file
template <class T>
CURLHandle& CURLHandle::setopt(CURLoption opt, const T& val)
{
CURLcode ok;
if constexpr (std::is_same<T, std::string>())
{
ok = curl_easy_setopt(m_handle, opt, val.c_str());
}
else if constexpr (std::is_same<T, bool>())
{
ok = curl_easy_setopt(m_handle, opt, val ? 1L : 0L);
}
else
{
ok = curl_easy_setopt(m_handle, opt, val);
}
if (ok != CURLE_OK)
{
throw curl_error(
fmt::format("curl: curl_easy_setopt failed {}", curl_easy_strerror(ok)));
}
return *this;
}

std::optional<std::string> proxy_match(const proxy_map_type& ctx, const std::string& url);
}

#endif
6 changes: 2 additions & 4 deletions include/powerloader/download_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include <powerloader/export.hpp>
#include <powerloader/enums.hpp>
#include <powerloader/url.hpp>
#include <powerloader/curl.hpp>
#include <powerloader/mirror.hpp>
#include <powerloader/fileio.hpp>
#include <powerloader/errors.hpp>

namespace powerloader
{

struct zck_target;

struct POWERLOADER_API CacheControl
Expand Down Expand Up @@ -80,8 +78,8 @@ namespace powerloader
m_error = std::move(err);
}

/// Returns a DownloadError if there was a falure at download or none if no error was set so
/// far.
/// Returns a DownloadError if there was a failure at download or none if no error was set
/// so far.
std::optional<DownloaderError> get_error() const noexcept
{
return m_error;
Expand Down
1 change: 0 additions & 1 deletion include/powerloader/downloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extern "C"

#include <powerloader/export.hpp>
#include <powerloader/context.hpp>
#include <powerloader/curl.hpp>
#include <powerloader/download_target.hpp>
#include <powerloader/enums.hpp>
#include <powerloader/mirror.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/powerloader/mirror.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <powerloader/export.hpp>
#include <powerloader/context.hpp>
#include <powerloader/curl.hpp>
#include <powerloader/enums.hpp>
#include <powerloader/utils.hpp>
#include <powerloader/mirrorid.hpp>
Expand All @@ -16,6 +15,7 @@ namespace powerloader
{
class Target;
class Context;
class CURLHandle;

enum class MirrorState
{
Expand Down
1 change: 1 addition & 0 deletions include/powerloader/mirrors/oci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <optional>
#include <fmt/core.h>
#include <nlohmann/json.hpp>

#include <powerloader/export.hpp>
#include <powerloader/mirror.hpp>
Expand Down
1 change: 0 additions & 1 deletion include/powerloader/mirrors/s3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace powerloader
{

class Target;

struct POWERLOADER_API S3CanonicalRequest
Expand Down
26 changes: 0 additions & 26 deletions include/powerloader/url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@ extern "C"

#include <powerloader/export.hpp>

// typedef enum {
// CURLUE_OK,
// CURLUE_BAD_HANDLE, /* 1 */
// CURLUE_BAD_PARTPOINTER, /* 2 */
// CURLUE_MALFORMED_INPUT, /* 3 */
// CURLUE_BAD_PORT_NUMBER, /* 4 */
// CURLUE_UNSUPPORTED_SCHEME, /* 5 */
// CURLUE_URLDECODE, /* 6 */
// CURLUE_OUT_OF_MEMORY, /* 7 */
// CURLUE_USER_NOT_ALLOWED, /* 8 */
// CURLUE_UNKNOWN_PART, /* 9 */
// CURLUE_NO_SCHEME, /* 10 */
// CURLUE_NO_USER, /* 11 */
// CURLUE_NO_PASSWORD, /* 12 */
// CURLUE_NO_OPTIONS, /* 13 */
// CURLUE_NO_HOST, /* 14 */
// CURLUE_NO_PORT, /* 15 */
// CURLUE_NO_QUERY, /* 16 */
// CURLUE_NO_FRAGMENT /* 17 */
// } CURLUcode;

namespace powerloader
{
POWERLOADER_API bool has_scheme(const std::string& url);
Expand Down Expand Up @@ -135,11 +114,6 @@ namespace powerloader
}
} // namespace detail

inline std::string join_url()
{
return "";
}

template <class S, class... Args>
inline std::string join_url(const S& s, const Args&... args)
{
Expand Down
11 changes: 0 additions & 11 deletions src/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <sstream>
#include <spdlog/spdlog.h>

#include <powerloader/curl.hpp>
#include <powerloader/utils.hpp>
#include <powerloader/context.hpp>
#include <powerloader/url.hpp>
Expand Down Expand Up @@ -230,16 +229,6 @@ namespace powerloader
return m_handle;
}

CURLHandle::operator CURL*()
{
return handle();
}

CURL* CURLHandle::ptr() const
{
return m_handle;
}

CURLHandle& CURLHandle::add_header(const std::string& header)
{
p_headers = curl_slist_append(p_headers, header.c_str());
Expand Down
102 changes: 101 additions & 1 deletion src/curl_internal.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,110 @@
#ifndef POWERLOADER_SRC_CURL_INTERNAL_HPP
#define POWERLOADER_SRC_CURL_INTERNAL_HPP

#include <optional>
#include <sstream>
#include <string>
#include <vector>

#include <fmt/core.h>

#include <powerloader/export.hpp>
#include <powerloader/utils.hpp>
#include <powerloader/enums.hpp>
#include <powerloader/curl.hpp>

namespace powerloader
{
class Context;
using proxy_map_type = std::map<std::string, std::string>;

// If needed, this can be moved to curl.hpp
class POWERLOADER_API curl_error : public std::runtime_error
{
public:
curl_error(const std::string& what = "download error", bool serious = false);
bool is_serious() const;

private:
bool m_serious;
};

class POWERLOADER_API CURLHandle
{
public:
using end_callback_type = std::function<CbReturnCode(const Response&)>;
explicit CURLHandle(const Context& ctx);
CURLHandle(const Context& ctx, const std::string& url);
~CURLHandle();

CURLHandle& url(const std::string& url, const proxy_map_type& proxies);
CURLHandle& accept_encoding();
CURLHandle& user_agent(const std::string& user_agent);

Response perform();
void finalize_transfer();

template <class T>
tl::expected<T, CURLcode> getinfo(CURLINFO option);

// This is made public because it is used internally in quite some files
CURL* handle();

CURLHandle& add_header(const std::string& header);
CURLHandle& add_headers(const std::vector<std::string>& headers);
CURLHandle& reset_headers();

template <class T>
CURLHandle& setopt(CURLoption opt, const T& val);

void set_default_callbacks();
CURLHandle& set_end_callback(end_callback_type func);

CURLHandle& upload(std::ifstream& stream);
CURLHandle& upload(std::istringstream& stream);

CURLHandle(CURLHandle&& rhs);
CURLHandle& operator=(CURLHandle&& rhs);

private:
void init_handle(const Context& ctx);
void finalize_transfer(Response& response);

CURL* m_handle;
curl_slist* p_headers = nullptr;
char errorbuffer[CURL_ERROR_SIZE];

std::unique_ptr<Response> response;
end_callback_type end_callback;
};

// TODO: restrict the possible implementations in the cpp file
template <class T>
CURLHandle& CURLHandle::setopt(CURLoption opt, const T& val)
{
CURLcode ok;
if constexpr (std::is_same<T, std::string>())
{
ok = curl_easy_setopt(m_handle, opt, val.c_str());
}
else if constexpr (std::is_same<T, bool>())
{
ok = curl_easy_setopt(m_handle, opt, val ? 1L : 0L);
}
else
{
ok = curl_easy_setopt(m_handle, opt, val);
}
if (ok != CURLE_OK)
{
throw curl_error(
fmt::format("curl: curl_easy_setopt failed {}", curl_easy_strerror(ok)));
}
return *this;
}

std::optional<std::string> proxy_match(const proxy_map_type& ctx, const std::string& url);
}

namespace powerloader::details
{
// Scoped initialization and termination of CURL.
Expand Down
Loading