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

Wrap libcurl calls #164

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
25 changes: 13 additions & 12 deletions include/powerloader/curl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ namespace powerloader
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:
Expand All @@ -94,16 +91,8 @@ namespace powerloader

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;
bool handle_exists();

CURLHandle& add_header(const std::string& header);
CURLHandle& add_headers(const std::vector<std::string>& headers);
Expand All @@ -123,13 +112,25 @@ namespace powerloader

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

CURL* handle();

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

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

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

friend class CURLInterface;

friend void add_multipart_upload(CURLHandle& target,
const std::vector<std::string>& files,
const std::map<std::string, std::string>& extra_fields);
};

// TODO: restrict the possible implementations in the cpp file
Expand Down
9 changes: 1 addition & 8 deletions include/powerloader/download_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace powerloader
{

struct zck_target;

struct POWERLOADER_API CacheControl
Expand All @@ -23,12 +22,6 @@ namespace powerloader
std::string last_modified;
};

enum CompressionType
{
NONE,
ZSTD,
};

class POWERLOADER_API DownloadTarget
{
public:
Expand Down Expand Up @@ -80,7 +73,7 @@ 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
/// 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
{
Expand Down
119 changes: 119 additions & 0 deletions include/powerloader/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ namespace powerloader
// Want: S3, OCI
};

enum CompressionType
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the enums here so that we could have them in the same file. But maybe we don't want that...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If powerloader user dont need them, then maybe moving them into src/curl_internal.hpp might work better?

{
NONE,
ZSTD,
};

enum class MirrorState
{
WAITING,
AUTHENTICATING,
READY,
RETRY_DELAY,
AUTHENTICATION_FAILED,
FAILED
};

enum class DownloadState
{
// The target is waiting to be processed.
Expand Down Expand Up @@ -81,11 +97,114 @@ namespace powerloader
kMD5
};

/** Librepo return/error codes */
enum class ErrorCode
{
// everything is ok
PD_OK,
// bad function argument
PD_BADFUNCARG,
// bad argument of the option
PD_BADOPTARG,
// library doesn't know the option
PD_UNKNOWNOPT,
// cURL doesn't know the option. Too old curl version?
PD_CURLSETOPT,
// Result object is not clean
PD_ADYUSEDRESULT,
// Result doesn't contain all what is needed
PD_INCOMPLETERESULT,
// cannot duplicate curl handle
PD_CURLDUP,
// cURL error
PD_CURL,
// cURL multi handle error
PD_CURLM,
// HTTP or FTP returned status code which do not represent success
// (file doesn't exists, etc.)
PD_BADSTATUS,
// some error that should be temporary and next try could work
// (HTTP status codes 500, 502-504, operation timeout, ...)
PD_TEMPORARYERR,
// URL is not a local address
PD_NOTLOCAL,
// cannot create a directory in output dir (ady exists?)
PD_CANNOTCREATEDIR,
// input output error
PD_IO,
// bad mirrorlist/metalink file (metalink doesn't contain needed
// file, mirrorlist doesn't contain urls, ..)
PD_MIRRORS,
// bad checksum
PD_BADCHECKSUM,
// no usable URL found
PD_NOURL,
// cannot create tmp directory
PD_CANNOTCREATETMP,
// unknown type of checksum is needed for verification
PD_UNKNOWNCHECKSUM,
// bad URL specified
PD_BADURL,
// Download was interrupted by signal.
// Only if LRO_INTERRUPTIBLE option is enabled.
PD_INTERRUPTED,
// sigaction error
PD_SIGACTION,
// File ady exists and checksum is ok.*/
PD_ADYDOWNLOADED,
// The download wasn't or cannot be finished.
PD_UNFINISHED,
// select() call failed.
PD_SELECT,
// OpenSSL library related error.
PD_OPENSSL,
// Cannot allocate more memory
PD_MEMORY,
// Interrupted by user cb
PD_CBINTERRUPTED,
// File operation error (operation not permitted, filename too long, no memory available,
// bad file descriptor, ...)
PD_FILE,
// Zchunk error (error reading zchunk file, ...)
PD_ZCK,
// (xx) unknown error - sentinel of error codes enum
PD_UNKNOWNERROR,
};

enum ErrorLevel
{
INFO,
SERIOUS,
FATAL
};

struct Checksum
{
ChecksumType type;
std::string checksum;
};

/** AS A REFERENCE */
// 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;
}

#endif
83 changes: 1 addition & 82 deletions include/powerloader/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,10 @@
#define POWERLOADER_ERRORS_HPP

#include <spdlog/spdlog.h>
#include <powerloader/enums.hpp>

namespace powerloader
{
/** Librepo return/error codes
*/
enum class ErrorCode
{
// everything is ok
PD_OK,
// bad function argument
PD_BADFUNCARG,
// bad argument of the option
PD_BADOPTARG,
// library doesn't know the option
PD_UNKNOWNOPT,
// cURL doesn't know the option. Too old curl version?
PD_CURLSETOPT,
// Result object is not clean
PD_ADYUSEDRESULT,
// Result doesn't contain all what is needed
PD_INCOMPLETERESULT,
// cannot duplicate curl handle
PD_CURLDUP,
// cURL error
PD_CURL,
// cURL multi handle error
PD_CURLM,
// HTTP or FTP returned status code which do not represent success
// (file doesn't exists, etc.)
PD_BADSTATUS,
// some error that should be temporary and next try could work
// (HTTP status codes 500, 502-504, operation timeout, ...)
PD_TEMPORARYERR,
// URL is not a local address
PD_NOTLOCAL,
// cannot create a directory in output dir (ady exists?)
PD_CANNOTCREATEDIR,
// input output error
PD_IO,
// bad mirrorlist/metalink file (metalink doesn't contain needed
// file, mirrorlist doesn't contain urls, ..)
PD_MIRRORS,
// bad checksum
PD_BADCHECKSUM,
// no usable URL found
PD_NOURL,
// cannot create tmp directory
PD_CANNOTCREATETMP,
// unknown type of checksum is needed for verification
PD_UNKNOWNCHECKSUM,
// bad URL specified
PD_BADURL,
// Download was interrupted by signal.
// Only if LRO_INTERRUPTIBLE option is enabled.
PD_INTERRUPTED,
// sigaction error
PD_SIGACTION,
// File ady exists and checksum is ok.*/
PD_ADYDOWNLOADED,
// The download wasn't or cannot be finished.
PD_UNFINISHED,
// select() call failed.
PD_SELECT,
// OpenSSL library related error.
PD_OPENSSL,
// Cannot allocate more memory
PD_MEMORY,
// Interrupted by user cb
PD_CBINTERRUPTED,
// File operation error (operation not permitted, filename too long, no memory available,
// bad file descriptor, ...)
PD_FILE,
// Zchunk error (error reading zchunk file, ...)
PD_ZCK,
// (xx) unknown error - sentinel of error codes enum
PD_UNKNOWNERROR,
};

enum ErrorLevel
{
INFO,
SERIOUS,
FATAL
};

struct DownloaderError
{
ErrorLevel level;
Expand Down
10 changes: 0 additions & 10 deletions include/powerloader/mirror.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ namespace powerloader
class Target;
class Context;

enum class MirrorState
{
WAITING,
AUTHENTICATING,
READY,
RETRY_DELAY,
AUTHENTICATION_FAILED,
FAILED
};

struct MirrorStats
{
// Maximum number of allowed parallel connections to this mirror. -1 means no
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
Loading