diff --git a/tinycurl.cpp b/tinycurl.cpp index 4c6e625..b6a0013 100644 --- a/tinycurl.cpp +++ b/tinycurl.cpp @@ -9,6 +9,7 @@ TinyCurl::TinyCurl(const std::string &url) { + curl_global_init(CURL_GLOBAL_ALL); m_handle = curl_easy_init(); if ( m_handle == NULL ) throw Exception("Unable to initialize curl handler"); @@ -17,23 +18,24 @@ TinyCurl::TinyCurl(const std::string &url) m_url = url; } -TinyCurl::~TinyCurl() +TinyCurl::~TinyCurl() noexcept { curl_easy_cleanup(m_handle); + curl_slist_free_all(opt_list); + curl_global_cleanup(); } -void TinyCurl::setHTTPHeaders(const headers_t &hdrs) const +void TinyCurl::setHTTPHeaders(const headers_t &hdrs) { if (! hdrs.empty()) { - struct curl_slist *chunk = NULL; for (headers_t::const_iterator i = hdrs.begin() ; i != hdrs.end() ; ++i) { std::string h(i->first); h += ": "; h += i->second; - chunk = curl_slist_append(chunk, h.c_str()); + opt_list = curl_slist_append(opt_list, h.c_str()); } - CURLcode res = curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, chunk); + CURLcode res = curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, opt_list); if ( res != CURLE_OK) throw Exception(res); } diff --git a/tinycurl.h b/tinycurl.h index 0f5a54e..82d1583 100644 --- a/tinycurl.h +++ b/tinycurl.h @@ -35,7 +35,7 @@ class TinyCurl }; TinyCurl(const std::string &url); - ~TinyCurl(); + ~TinyCurl() noexcept; typedef std::map headers_t; @@ -58,7 +58,7 @@ class TinyCurl TinyCurl(const TinyCurl &); void setOptions(); - void setHTTPHeaders(const headers_t &hdrs) const; + void setHTTPHeaders(const headers_t &hdrs); void curlPerform(); @@ -66,4 +66,5 @@ class TinyCurl std::string m_url; std::string m_data; std::string m_upload_data; + struct curl_slist *opt_list = NULL; };