From d1c34dde661d446429aa790ec195d8cf12298827 Mon Sep 17 00:00:00 2001 From: Giuseppe Persico Date: Sat, 2 Sep 2017 16:04:00 +0200 Subject: [PATCH] Added curl share options and fixed typos. --- build/.gitkeep | 0 include/curl_multi.h | 9 +++---- include/curl_share.h | 64 +++++++++++++++++++++++++++++++++++++++++--- src/curl_global.cpp | 9 +++---- src/curl_multi.cpp | 15 ++++------- 5 files changed, 72 insertions(+), 25 deletions(-) delete mode 100644 build/.gitkeep diff --git a/build/.gitkeep b/build/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/include/curl_multi.h b/include/curl_multi.h index 8760e6f..6f52fd3 100644 --- a/include/curl_multi.h +++ b/include/curl_multi.h @@ -217,11 +217,11 @@ namespace curl { /** * Move constructor which moves internal data to another object. */ - curl_multi(curl_multi&&); + curl_multi(curl_multi&&) NOEXCEPT; /** * Move assignment operator which moves internal data to another object. */ - curl_multi& operator=(curl_multi&&); + curl_multi& operator=(curl_multi&&) NOEXCEPT; /** * Destructor to deallocate all the resources using * libcurl. @@ -321,12 +321,11 @@ namespace curl { */ CURLM *get_curl() const; private: - struct milti_deleter - { + struct multi_deleter { void operator()(CURLM* ptr) const; }; - using multi_ptr = std::unique_ptr; + using multi_ptr = std::unique_ptr; int message_queued; int active_transfers; diff --git a/include/curl_share.h b/include/curl_share.h index 380e7ce..c0332b1 100644 --- a/include/curl_share.h +++ b/include/curl_share.h @@ -29,7 +29,63 @@ #include "curl_interface.h" #include "curl_pair.h" +#define CURLCPP_DEFINE_OPTION(opt, value_type)\ + template <> struct shoption_t {\ + using type = value_type;\ + } + namespace curl { + namespace detail { + template + struct shoption_t; + + template + using SHOption_type = typename shoption_t::type; + + /* + * The parameter must be a pointer to a function matching the following prototype: + * void lock_function(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr); + * data defines what data libcurl wants to lock, and you must make sure that only one lock is given + * at any time for each kind of data. access defines what access type libcurl wants, shared or single. + * userptr is the pointer you set with CURLSHOPT_USERDATA. + */ + CURLCPP_DEFINE_OPTION(CURLSHOPT_LOCKFUNC, + void(*)(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr)); + /* + * The parameter must be a pointer to a function matching the following prototype: + * void unlock_function(CURL *handle, curl_lock_data data, void *userptr); + * data defines what data libcurl wants to unlock, and you must make sure that only one lock is given + * at any time for each kind of data. + * userptr is the pointer you set with CURLSHOPT_USERDATA. + */ + CURLCPP_DEFINE_OPTION(CURLSHOPT_UNLOCKFUNC, void(*)(CURL *handle, curl_lock_data data, void *userptr)); + + /* + * The parameter specifies a type of data that should be shared. This may be set to one of the values described below. + * CURL_LOCK_DATA_COOKIE: Cookie data will be shared across the easy handles using this shared object. + * CURL_LOCK_DATA_DNS: Cached DNS hosts will be shared across the easy handles using this shared object. + * Note that when you use the multi interface, all easy handles added to the same multi handle will share + * DNS cache by default without this having to be used! + * CURL_LOCK_DATA_SSL_SESSION: SSL session IDs will be shared across the easy handles using this shared + * object. This will reduce the time spent in the SSL handshake when reconnecting to the same server. + * Note SSL session IDs are reused within the same easy handle by default. Note this symbol was added + * in 7.10.3 but was not implemented until 7.23.0. + */ + CURLCPP_DEFINE_OPTION(CURLSHOPT_SHARE, int); + + /* + * This option does the opposite of CURLSHOPT_SHARE. It specifies that the specified parameter will no longer + * be shared. Valid values are the same as those for CURLSHOPT_SHARE. + */ + CURLCPP_DEFINE_OPTION(CURLSHOPT_UNSHARE, int); + + /* + * The parameter allows you to specify a pointer to data that will be passed to the lock_function and + * unlock_function each time it is called. + */ + CURLCPP_DEFINE_OPTION(CURLSHOPT_USERDATA, void *); + } + /** * Definition of share interface. The purpose of this interface is to * enable data sharing between curl handlers. @@ -64,7 +120,7 @@ namespace curl { /** * Add method used to add options to the share handle. */ - template void add(const curl_pair &); + template void add(const detail::SHOption_type); /** * Allows users to specify a list of options for the current * easy handler. In this way, you can specify any iterable data @@ -87,10 +143,10 @@ namespace curl { } // Implementation of add method - template void curl_share::add(const curl_pair &pair) { - const CURLSHcode code = curl_share_setopt(this->curl,pair.first(),pair.second()); + template void curl_share::add(const detail::SHOption_type val) { + const auto code = curl_share_setopt(this->curl, Opt, val); if (code != CURLSHE_OK) { - throw curl_share_exception(code,__FUNCTION__); + throw curl_share_exception(code, __FUNCTION__); } } diff --git a/src/curl_global.cpp b/src/curl_global.cpp index 66b9faa..49d1d9c 100644 --- a/src/curl_global.cpp +++ b/src/curl_global.cpp @@ -2,23 +2,20 @@ using curl::curl_global; -curl_global::curl_global() -{ +curl_global::curl_global() { const CURLcode code = curl_global_init(CURL_GLOBAL_ALL); if (code != CURLE_OK) { throw curl_easy_exception(code, __FUNCTION__); } } -curl_global::curl_global(const long flag) -{ +curl_global::curl_global(const long flag) { const CURLcode code = curl_global_init(flag); if (code != CURLE_OK) { throw curl_easy_exception(code, __FUNCTION__); } } -curl_global::~curl_global() -{ +curl_global::~curl_global() { curl_global_cleanup(); } diff --git a/src/curl_multi.cpp b/src/curl_multi.cpp index e8fcf72..dc191eb 100644 --- a/src/curl_multi.cpp +++ b/src/curl_multi.cpp @@ -11,13 +11,11 @@ using curl::curl_easy; using std::vector; using std::unique_ptr; -void curl_multi::milti_deleter::operator()(CURLM* ptr) const -{ +void curl_multi::multi_deleter::operator()(CURLM* ptr) const { curl_multi_cleanup(ptr); } -curl_multi::curl_multi() : curl_multi(CURL_GLOBAL_ALL) { -} +curl_multi::curl_multi() : curl_multi(CURL_GLOBAL_ALL) {} curl_multi::curl_multi(const long flag) : curl_interface(flag), @@ -29,26 +27,23 @@ curl_multi::curl_multi(const long flag) this->message_queued = 0; } -curl_multi::curl_multi(curl_multi&& other) +curl_multi::curl_multi(curl_multi&& other) NOEXCEPT : curl_interface(std::forward(other)), curl(std::move(other.curl)), active_transfers(other.active_transfers), message_queued(other.message_queued) { } -curl_multi &curl_multi::operator=(curl_multi&& other) { +curl_multi &curl_multi::operator=(curl_multi&& other) NOEXCEPT { if (this != &other) { curl = std::move(other.curl); active_transfers = other.active_transfers; message_queued = other.message_queued; } - return *this; } -curl_multi::~curl_multi() NOEXCEPT -{ -} +curl_multi::~curl_multi() NOEXCEPT = default; // Implementation of add method for easy handlers. void curl_multi::add(const curl_easy &easy) {