Add version, fix a few things.
This commit is contained in:
parent
642fc26f46
commit
dd193cf0e0
|
@ -6,7 +6,8 @@ if(${CMAKE_VERSION} VERSION_LESS 3.12)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
project(curl_wrapper
|
project(curl_wrapper
|
||||||
DESCRIPTION "Simple libcurl wrapper."
|
VERSION 0.1.0
|
||||||
|
DESCRIPTION "Light libcurl wrapper."
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
|
|
||||||
option(WITH_CURL_WRAPPER_TESTS "Compile tests for curl_wrapper." NO)
|
option(WITH_CURL_WRAPPER_TESTS "Compile tests for curl_wrapper." NO)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Simple libcurl wrapper for when you need to GET a website with minimum effort.
|
Light libcurl wrapper for when you need to GET a website with minimum effort.
|
||||||
|
|
||||||
This is _not_ supposed to be a package on its own, but a thing you drop into
|
This is _not_ supposed to be a package on its own, but a thing you drop into
|
||||||
your project.
|
your project.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
file(GLOB sources *.cpp)
|
file(GLOB sources "*.cpp")
|
||||||
file(GLOB headers *.hpp)
|
file(GLOB headers "*.hpp")
|
||||||
add_library(${PROJECT_NAME} STATIC ${sources} ${headers})
|
add_library(${PROJECT_NAME} STATIC ${sources} ${headers})
|
||||||
unset(sources)
|
unset(sources)
|
||||||
unset(headers)
|
unset(headers)
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
namespace curl_wrapper
|
namespace curl_wrapper
|
||||||
{
|
{
|
||||||
|
|
||||||
static std::atomic<std::uint64_t> curlwrapper_instances{0};
|
inline static std::atomic<std::uint64_t> curlwrapper_instances{0};
|
||||||
|
|
||||||
void CURLWrapper::init()
|
void CURLWrapper::init()
|
||||||
{
|
{
|
||||||
|
@ -66,17 +66,6 @@ void CURLWrapper::init()
|
||||||
curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 5L);
|
curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 5L);
|
||||||
}
|
}
|
||||||
|
|
||||||
CURLWrapper::CURLWrapper()
|
|
||||||
: _connection{nullptr}
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
CURLWrapper::CURLWrapper(const CURLWrapper &)
|
|
||||||
: _connection{nullptr}
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
CURLWrapper::~CURLWrapper() noexcept
|
CURLWrapper::~CURLWrapper() noexcept
|
||||||
{
|
{
|
||||||
curl_easy_cleanup(_connection);
|
curl_easy_cleanup(_connection);
|
||||||
|
|
|
@ -32,9 +32,9 @@ using std::string;
|
||||||
using std::string_view;
|
using std::string_view;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Light wrapper atound libcurl.
|
* @brief Light wrapper around libcurl.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
class CURLWrapper
|
class CURLWrapper
|
||||||
{
|
{
|
||||||
|
@ -49,16 +49,24 @@ public:
|
||||||
*
|
*
|
||||||
* May throw CURLException or std::runtime_error.
|
* May throw CURLException or std::runtime_error.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
CURLWrapper();
|
CURLWrapper()
|
||||||
|
: _connection{nullptr}
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Copy constructor. Does the same as the Constructor.
|
* @brief Copy constructor. Does the same as the Constructor.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
CURLWrapper(const CURLWrapper &);
|
CURLWrapper(const CURLWrapper &)
|
||||||
|
: _connection{nullptr}
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
//! Move constructor
|
//! Move constructor
|
||||||
CURLWrapper(CURLWrapper &&other) noexcept = delete;
|
CURLWrapper(CURLWrapper &&other) noexcept = delete;
|
||||||
|
@ -71,7 +79,7 @@ public:
|
||||||
* [curl_global_cleanup(3)]
|
* [curl_global_cleanup(3)]
|
||||||
* (https://curl.haxx.se/libcurl/c/curl_global_cleanup.html).
|
* (https://curl.haxx.se/libcurl/c/curl_global_cleanup.html).
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
virtual ~CURLWrapper() noexcept;
|
virtual ~CURLWrapper() noexcept;
|
||||||
|
|
||||||
|
@ -88,7 +96,7 @@ public:
|
||||||
* information consult [curl_easy_setopt(3)]
|
* information consult [curl_easy_setopt(3)]
|
||||||
* (https://curl.haxx.se/libcurl/c/curl_easy_setopt.html).
|
* (https://curl.haxx.se/libcurl/c/curl_easy_setopt.html).
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] inline CURL *get_curl_easy_handle() const
|
[[nodiscard]] inline CURL *get_curl_easy_handle() const
|
||||||
{
|
{
|
||||||
|
@ -105,7 +113,7 @@ public:
|
||||||
*
|
*
|
||||||
* @return The escaped string or {} if it failed.
|
* @return The escaped string or {} if it failed.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] inline string escape_url(const string_view url) const
|
[[nodiscard]] inline string escape_url(const string_view url) const
|
||||||
{
|
{
|
||||||
|
@ -126,7 +134,7 @@ public:
|
||||||
*
|
*
|
||||||
* @return The unescaped string or {} if it failed.
|
* @return The unescaped string or {} if it failed.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] inline string unescape_url(const string_view url) const
|
[[nodiscard]] inline string unescape_url(const string_view url) const
|
||||||
{
|
{
|
||||||
|
@ -142,7 +150,7 @@ public:
|
||||||
*
|
*
|
||||||
* May throw CURLException.
|
* May throw CURLException.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
void set_useragent(string_view useragent);
|
void set_useragent(string_view useragent);
|
||||||
|
|
||||||
|
@ -156,7 +164,7 @@ public:
|
||||||
*
|
*
|
||||||
* @return The status code, headers and body of the page.
|
* @return The status code, headers and body of the page.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] answer make_http_request(http_method method, string_view uri);
|
[[nodiscard]] answer make_http_request(http_method method, string_view uri);
|
||||||
|
|
||||||
|
@ -169,14 +177,14 @@ private:
|
||||||
/*!
|
/*!
|
||||||
* @brief Initializes curl and sets up connection.
|
* @brief Initializes curl and sets up connection.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief libcurl write callback function.
|
* @brief libcurl write callback function.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
size_t writer_body(char *data, size_t size, size_t nmemb);
|
size_t writer_body(char *data, size_t size, size_t nmemb);
|
||||||
|
|
||||||
|
@ -186,7 +194,7 @@ private:
|
||||||
*
|
*
|
||||||
* <https://curl.haxx.se/docs/faq.html#Using_C_non_static_functions_f>
|
* <https://curl.haxx.se/docs/faq.html#Using_C_non_static_functions_f>
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
static inline size_t writer_body_wrapper(char *data, size_t sz,
|
static inline size_t writer_body_wrapper(char *data, size_t sz,
|
||||||
size_t nmemb, void *f)
|
size_t nmemb, void *f)
|
||||||
|
@ -207,7 +215,7 @@ private:
|
||||||
/*!
|
/*!
|
||||||
* @brief Throw runtime error if command doesn't return CURLE_OK.
|
* @brief Throw runtime error if command doesn't return CURLE_OK.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
void check(CURLcode code);
|
void check(CURLcode code);
|
||||||
};
|
};
|
||||||
|
@ -215,9 +223,7 @@ private:
|
||||||
/*!
|
/*!
|
||||||
* @brief Exception for libcurl errors.
|
* @brief Exception for libcurl errors.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*
|
|
||||||
* @headerfile exceptions.hpp mastodonpp/exceptions.hpp
|
|
||||||
*/
|
*/
|
||||||
class CURLException : public std::exception
|
class CURLException : public std::exception
|
||||||
{
|
{
|
||||||
|
@ -225,7 +231,7 @@ public:
|
||||||
/*!
|
/*!
|
||||||
* @brief Constructor with error code.
|
* @brief Constructor with error code.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
explicit CURLException(const CURLcode code)
|
explicit CURLException(const CURLcode code)
|
||||||
: error_code{code}
|
: error_code{code}
|
||||||
|
@ -234,15 +240,20 @@ public:
|
||||||
/*!
|
/*!
|
||||||
* @brief Constructor with error code and error buffer.
|
* @brief Constructor with error code and error buffer.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
explicit CURLException(const CURLcode code, string_view error_buffer)
|
explicit CURLException(const CURLcode code, string_view error_buffer)
|
||||||
: error_code{code}
|
: error_code{code}
|
||||||
, _error_message{error_buffer}
|
, _error_message{error_buffer}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
const CURLcode error_code;
|
const CURLcode error_code; //!< Error code from libcurl.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Error message.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
|
*/
|
||||||
[[nodiscard]] const char *what() noexcept;
|
[[nodiscard]] const char *what() noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -32,7 +32,7 @@ using std::string_view;
|
||||||
/*!
|
/*!
|
||||||
* @brief The HTTP method.
|
* @brief The HTTP method.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
enum class http_method
|
enum class http_method
|
||||||
{
|
{
|
||||||
|
@ -49,18 +49,18 @@ enum class http_method
|
||||||
*
|
*
|
||||||
* Currently only HTTP is considered.
|
* Currently only HTTP is considered.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
struct answer
|
struct answer
|
||||||
{
|
{
|
||||||
std::uint16_t status{0};
|
std::uint16_t status{0}; //!< Status code.
|
||||||
string headers;
|
string headers; //!< The headers of the response from the server.
|
||||||
string body;
|
string body; //!< The response from the server.
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns true if #status is 200.
|
* @brief Returns true if #status is 200.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] inline explicit operator bool() const
|
[[nodiscard]] inline explicit operator bool() const
|
||||||
{
|
{
|
||||||
|
@ -68,9 +68,9 @@ struct answer
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns #body as const std::string_view.
|
* @brief Returns std::string_view of the #body.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] inline explicit operator string_view() const
|
[[nodiscard]] inline explicit operator string_view() const
|
||||||
{
|
{
|
||||||
|
@ -80,7 +80,7 @@ struct answer
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns #body as std::ostream.
|
* @brief Returns #body as std::ostream.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
inline friend ostream &operator<<(ostream &out, const answer &answer)
|
inline friend ostream &operator<<(ostream &out, const answer &answer)
|
||||||
{
|
{
|
||||||
|
@ -96,7 +96,7 @@ struct answer
|
||||||
* @return A std::string_view to the value of the header field or {} if not
|
* @return A std::string_view to the value of the header field or {} if not
|
||||||
* found.
|
* found.
|
||||||
*
|
*
|
||||||
* @since INSERT_VERSION
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] string_view get_header(string_view field) const;
|
[[nodiscard]] string_view get_header(string_view field) const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|
||||||
file(GLOB sources_tests test_*.cpp)
|
file(GLOB sources_tests "test_*.cpp")
|
||||||
|
|
||||||
find_package(Catch2 CONFIG)
|
find_package(Catch2 CONFIG)
|
||||||
if(Catch2_FOUND) # Catch 2.x
|
if(Catch2_FOUND) # Catch 2.x
|
||||||
|
@ -19,8 +19,8 @@ else() # Catch 1.x
|
||||||
if(EXISTS "/usr/include/catch.hpp")
|
if(EXISTS "/usr/include/catch.hpp")
|
||||||
message(STATUS "Catch 1.x found.")
|
message(STATUS "Catch 1.x found.")
|
||||||
foreach(src ${sources_tests})
|
foreach(src ${sources_tests})
|
||||||
get_filename_component(bin ${src} NAME_WE)
|
get_filename_component(bin "${src}" NAME_WE)
|
||||||
add_executable(${bin} main.cpp ${src})
|
add_executable(${bin} "main.cpp" "${src}")
|
||||||
set_target_properties(${bin}
|
set_target_properties(${bin}
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
CXX_STANDARD 17
|
CXX_STANDARD 17
|
||||||
|
|
Loading…
Reference in New Issue