refactor
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
#define REPERTORY_INCLUDE_COMM_CURL_CURL_COMM_HPP_
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "comm/curl/dns_cache.hpp"
|
||||
#include "comm/curl/curl_shared.hpp"
|
||||
#include "comm/curl/multi_request.hpp"
|
||||
#include "comm/i_http_comm.hpp"
|
||||
#include "events/event_system.hpp"
|
||||
@@ -211,7 +211,7 @@ public:
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
||||
}
|
||||
|
||||
dns_cache::set_cache(curl);
|
||||
curl_shared::set_cache(curl);
|
||||
|
||||
auto url = construct_url(curl, request.get_path(), cfg) + parameters;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
|
@@ -19,11 +19,11 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef REPERTORY_INCLUDE_COMM_CURL_DNS_CACHE_HPP_
|
||||
#define REPERTORY_INCLUDE_COMM_CURL_DNS_CACHE_HPP_
|
||||
#ifndef REPERTORY_INCLUDE_COMM_CURL_CURL_SHARED_HPP_
|
||||
#define REPERTORY_INCLUDE_COMM_CURL_CURL_SHARED_HPP_
|
||||
|
||||
namespace repertory {
|
||||
class dns_cache final {
|
||||
class curl_shared final {
|
||||
private:
|
||||
struct curl_sh_deleter final {
|
||||
void operator()(CURLSH *ptr) {
|
||||
@@ -36,13 +36,13 @@ private:
|
||||
using curl_sh_t = std::unique_ptr<CURLSH, curl_sh_deleter>;
|
||||
|
||||
public:
|
||||
dns_cache() = delete;
|
||||
dns_cache(const dns_cache &) = delete;
|
||||
dns_cache(dns_cache &&) = delete;
|
||||
~dns_cache() = delete;
|
||||
curl_shared() = delete;
|
||||
curl_shared(const curl_shared &) = delete;
|
||||
curl_shared(curl_shared &&) = delete;
|
||||
~curl_shared() = delete;
|
||||
|
||||
auto operator=(const dns_cache &) -> dns_cache & = delete;
|
||||
auto operator=(dns_cache &&) -> dns_cache & = delete;
|
||||
auto operator=(const curl_shared &) -> curl_shared & = delete;
|
||||
auto operator=(curl_shared &&) -> curl_shared & = delete;
|
||||
|
||||
private:
|
||||
static curl_sh_t cache_;
|
||||
@@ -58,7 +58,7 @@ private:
|
||||
public:
|
||||
static void cleanup();
|
||||
|
||||
static void init();
|
||||
[[nodiscard]] static auto init() -> bool;
|
||||
|
||||
static void set_cache(CURL *curl);
|
||||
};
|
@@ -19,29 +19,40 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "comm/curl/dns_cache.hpp"
|
||||
#include "comm/curl/curl_shared.hpp"
|
||||
|
||||
namespace repertory {
|
||||
dns_cache::curl_sh_t dns_cache::cache_;
|
||||
curl_shared::curl_sh_t curl_shared::cache_;
|
||||
|
||||
std::recursive_mutex dns_cache::mtx_;
|
||||
std::recursive_mutex curl_shared::mtx_;
|
||||
|
||||
void dns_cache::cleanup() { cache_.reset(nullptr); }
|
||||
void curl_shared::cleanup() {
|
||||
cache_.reset(nullptr);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
auto curl_shared::init() -> bool {
|
||||
auto res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if (res != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void dns_cache::init() {
|
||||
auto *cache = curl_share_init();
|
||||
if (cache == nullptr) {
|
||||
return;
|
||||
curl_global_cleanup();
|
||||
return false;
|
||||
}
|
||||
cache_.reset(cache);
|
||||
|
||||
curl_share_setopt(cache, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
|
||||
curl_share_setopt(cache, CURLSHOPT_LOCKFUNC, lock_callback);
|
||||
curl_share_setopt(cache, CURLSHOPT_UNLOCKFUNC, unlock_callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
void dns_cache::lock_callback(CURL * /* curl */, curl_lock_data data,
|
||||
curl_lock_access /* access */, void * /* ptr */) {
|
||||
void curl_shared::lock_callback(CURL * /* curl */, curl_lock_data data,
|
||||
curl_lock_access /* access */,
|
||||
void * /* ptr */) {
|
||||
if (data != CURL_LOCK_DATA_DNS) {
|
||||
return;
|
||||
}
|
||||
@@ -49,13 +60,13 @@ void dns_cache::lock_callback(CURL * /* curl */, curl_lock_data data,
|
||||
mtx_.lock();
|
||||
}
|
||||
|
||||
void dns_cache::set_cache(CURL *curl) {
|
||||
void curl_shared::set_cache(CURL *curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_SHARE, cache_.get());
|
||||
}
|
||||
|
||||
void dns_cache::unlock_callback(CURL * /* curl */, curl_lock_data data,
|
||||
curl_lock_access /* access */,
|
||||
void * /* ptr */) {
|
||||
void curl_shared::unlock_callback(CURL * /* curl */, curl_lock_data data,
|
||||
curl_lock_access /* access */,
|
||||
void * /* ptr */) {
|
||||
if (data != CURL_LOCK_DATA_DNS) {
|
||||
return;
|
||||
}
|
@@ -19,10 +19,6 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
#include <curl/curl.h>
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
#if defined(PROJECT_ENABLE_OPENSSL)
|
||||
#include <openssl/ssl.h>
|
||||
#endif // defined(PROJECT_ENABLE_OPENSSL)
|
||||
@@ -49,7 +45,7 @@
|
||||
#endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW)
|
||||
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
#include "comm/curl/dns_cache.hpp"
|
||||
#include "comm/curl/curl_shared.hpp"
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
namespace repertory {
|
||||
@@ -87,13 +83,8 @@ auto project_initialize() -> bool {
|
||||
#endif // defined(PROJECT_ENABLE_OPENSSL)
|
||||
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
{
|
||||
auto res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if (res != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dns_cache::init();
|
||||
if (not curl_shared::init()) {
|
||||
return false;
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
@@ -102,8 +93,7 @@ auto project_initialize() -> bool {
|
||||
auto res = sqlite3_initialize();
|
||||
if (res != SQLITE_OK) {
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
dns_cache::cleanup();
|
||||
curl_global_cleanup();
|
||||
curl_shared::cleanup();
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
return false;
|
||||
}
|
||||
@@ -115,8 +105,7 @@ auto project_initialize() -> bool {
|
||||
|
||||
void project_cleanup() {
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
dns_cache::cleanup();
|
||||
curl_global_cleanup();
|
||||
curl_shared::cleanup();
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
|
||||
#if defined(PROJECT_ENABLE_SQLITE)
|
||||
|
Reference in New Issue
Block a user