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()
|
||||
|
||||
project(curl_wrapper
|
||||
DESCRIPTION "Simple libcurl wrapper."
|
||||
VERSION 0.1.0
|
||||
DESCRIPTION "Light libcurl wrapper."
|
||||
LANGUAGES CXX)
|
||||
|
||||
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
|
||||
your project.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
file(GLOB sources *.cpp)
|
||||
file(GLOB headers *.hpp)
|
||||
file(GLOB sources "*.cpp")
|
||||
file(GLOB headers "*.hpp")
|
||||
add_library(${PROJECT_NAME} STATIC ${sources} ${headers})
|
||||
unset(sources)
|
||||
unset(headers)
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
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()
|
||||
{
|
||||
|
@ -66,17 +66,6 @@ void CURLWrapper::init()
|
|||
curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 5L);
|
||||
}
|
||||
|
||||
CURLWrapper::CURLWrapper()
|
||||
: _connection{nullptr}
|
||||
{
|
||||
init();
|
||||
}
|
||||
CURLWrapper::CURLWrapper(const CURLWrapper &)
|
||||
: _connection{nullptr}
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CURLWrapper::~CURLWrapper() noexcept
|
||||
{
|
||||
curl_easy_cleanup(_connection);
|
||||
|
|
|
@ -32,9 +32,9 @@ using std::string;
|
|||
using std::string_view;
|
||||
|
||||
/*!
|
||||
* @brief Light wrapper atound libcurl.
|
||||
* @brief Light wrapper around libcurl.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class CURLWrapper
|
||||
{
|
||||
|
@ -49,16 +49,24 @@ public:
|
|||
*
|
||||
* 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.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
CURLWrapper(const CURLWrapper &);
|
||||
CURLWrapper(const CURLWrapper &)
|
||||
: _connection{nullptr}
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
//! Move constructor
|
||||
CURLWrapper(CURLWrapper &&other) noexcept = delete;
|
||||
|
@ -71,7 +79,7 @@ public:
|
|||
* [curl_global_cleanup(3)]
|
||||
* (https://curl.haxx.se/libcurl/c/curl_global_cleanup.html).
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
virtual ~CURLWrapper() noexcept;
|
||||
|
||||
|
@ -88,7 +96,7 @@ public:
|
|||
* information consult [curl_easy_setopt(3)]
|
||||
* (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
|
||||
{
|
||||
|
@ -105,7 +113,7 @@ public:
|
|||
*
|
||||
* @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
|
||||
{
|
||||
|
@ -126,7 +134,7 @@ public:
|
|||
*
|
||||
* @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
|
||||
{
|
||||
|
@ -142,7 +150,7 @@ public:
|
|||
*
|
||||
* May throw CURLException.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
void set_useragent(string_view useragent);
|
||||
|
||||
|
@ -156,7 +164,7 @@ public:
|
|||
*
|
||||
* @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);
|
||||
|
||||
|
@ -169,14 +177,14 @@ private:
|
|||
/*!
|
||||
* @brief Initializes curl and sets up connection.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
void init();
|
||||
|
||||
/*!
|
||||
* @brief libcurl write callback function.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
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>
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
static inline size_t writer_body_wrapper(char *data, size_t sz,
|
||||
size_t nmemb, void *f)
|
||||
|
@ -207,7 +215,7 @@ private:
|
|||
/*!
|
||||
* @brief Throw runtime error if command doesn't return CURLE_OK.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
void check(CURLcode code);
|
||||
};
|
||||
|
@ -215,9 +223,7 @@ private:
|
|||
/*!
|
||||
* @brief Exception for libcurl errors.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
*
|
||||
* @headerfile exceptions.hpp mastodonpp/exceptions.hpp
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class CURLException : public std::exception
|
||||
{
|
||||
|
@ -225,7 +231,7 @@ public:
|
|||
/*!
|
||||
* @brief Constructor with error code.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
explicit CURLException(const CURLcode code)
|
||||
: error_code{code}
|
||||
|
@ -234,15 +240,20 @@ public:
|
|||
/*!
|
||||
* @brief Constructor with error code and error buffer.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
explicit CURLException(const CURLcode code, string_view error_buffer)
|
||||
: error_code{code}
|
||||
, _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;
|
||||
|
||||
private:
|
||||
|
|
|
@ -32,7 +32,7 @@ using std::string_view;
|
|||
/*!
|
||||
* @brief The HTTP method.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
enum class http_method
|
||||
{
|
||||
|
@ -49,18 +49,18 @@ enum class http_method
|
|||
*
|
||||
* Currently only HTTP is considered.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
struct answer
|
||||
{
|
||||
std::uint16_t status{0};
|
||||
string headers;
|
||||
string body;
|
||||
std::uint16_t status{0}; //!< Status code.
|
||||
string headers; //!< The headers of the response from the server.
|
||||
string body; //!< The response from the server.
|
||||
|
||||
/*!
|
||||
* @brief Returns true if #status is 200.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
[[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
|
||||
{
|
||||
|
@ -80,7 +80,7 @@ struct answer
|
|||
/*!
|
||||
* @brief Returns #body as std::ostream.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
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
|
||||
* found.
|
||||
*
|
||||
* @since INSERT_VERSION
|
||||
* @since 0.1.0
|
||||
*/
|
||||
[[nodiscard]] string_view get_header(string_view field) const;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
include(CTest)
|
||||
|
||||
file(GLOB sources_tests test_*.cpp)
|
||||
file(GLOB sources_tests "test_*.cpp")
|
||||
|
||||
find_package(Catch2 CONFIG)
|
||||
if(Catch2_FOUND) # Catch 2.x
|
||||
|
@ -19,8 +19,8 @@ else() # Catch 1.x
|
|||
if(EXISTS "/usr/include/catch.hpp")
|
||||
message(STATUS "Catch 1.x found.")
|
||||
foreach(src ${sources_tests})
|
||||
get_filename_component(bin ${src} NAME_WE)
|
||||
add_executable(${bin} main.cpp ${src})
|
||||
get_filename_component(bin "${src}" NAME_WE)
|
||||
add_executable(${bin} "main.cpp" "${src}")
|
||||
set_target_properties(${bin}
|
||||
PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
|
|
Loading…
Reference in New Issue