From 6a5b61396b48f27ca9bc0ee12a2fe6717f67dfd2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 1 Sep 2015 11:41:36 +0100 Subject: Improved wrappers for setopt and getinfo --- libadhocutil/curlHandle.cpp | 42 +++++++++++++++++++++++++++++++++++++ libadhocutil/curlHandle.h | 20 ++++++++++++++++-- libadhocutil/unittests/testCurl.cpp | 26 ++++++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/libadhocutil/curlHandle.cpp b/libadhocutil/curlHandle.cpp index 7912af7..6192c70 100644 --- a/libadhocutil/curlHandle.cpp +++ b/libadhocutil/curlHandle.cpp @@ -1,5 +1,6 @@ #include "curlHandle.h" #include +#include static void cleanup() __attribute__((destructor)); static void cleanup() @@ -27,12 +28,53 @@ CurlHandle::~CurlHandle() curl_easy_cleanup(curl_handle); } +template <> void CurlHandle::setopt(CURLoption opt, const void * val) { curl_easy_setopt(curl_handle, opt, val); } +template <> +void +CurlHandle::setopt(CURLoption opt, int val) +{ + curl_easy_setopt(curl_handle, opt, val); +} + +template <> +void +CurlHandle::setopt(CURLoption opt, long val) +{ + curl_easy_setopt(curl_handle, opt, val); +} + +void +CurlHandle::getinfo(CURLINFO info, long & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + +void +CurlHandle::getinfo(CURLINFO info, int & ival) const +{ + long val; + curl_easy_getinfo(curl_handle, info, &val); + ival = boost::numeric_cast(val); +} + +void +CurlHandle::getinfo(CURLINFO info, double & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + +void +CurlHandle::getinfo(CURLINFO info, char * & val) const +{ + curl_easy_getinfo(curl_handle, info, &val); +} + void CurlHandle::appendHeader(const char * header) { diff --git a/libadhocutil/curlHandle.h b/libadhocutil/curlHandle.h index b5a5d63..0bdcd11 100644 --- a/libadhocutil/curlHandle.h +++ b/libadhocutil/curlHandle.h @@ -13,8 +13,12 @@ class DLL_PUBLIC CurlHandle : public virtual IntrusivePtrBase { CurlHandle(const CurlHandle &) = delete; void operator=(const CurlHandle &) = delete; - void setopt(CURLoption opt, const void * val); - void getinto(CURLoption opt, void * & val); + template + void setopt(CURLoption opt, const T val); + void getinfo(CURLINFO info, long & val) const; + void getinfo(CURLINFO info, int & val) const; + void getinfo(CURLINFO info, double & val) const; + void getinfo(CURLINFO info, char * & val) const; void appendHeader(const char *); void appendPost(const char *, const char *); void perform(); @@ -30,5 +34,17 @@ class DLL_PUBLIC CurlHandle : public virtual IntrusivePtrBase { }; typedef boost::intrusive_ptr CurlHandlePtr; +template <> +void CurlHandle::setopt(CURLoption opt, const void * val); +template <> +void CurlHandle::setopt(CURLoption opt, int val); +template <> +void CurlHandle::setopt(CURLoption opt, long val); +template +void CurlHandle::setopt(CURLoption opt, const T val) +{ + setopt(opt, (const void *)val); +} + #endif diff --git a/libadhocutil/unittests/testCurl.cpp b/libadhocutil/unittests/testCurl.cpp index 0aed488..5365f58 100644 --- a/libadhocutil/unittests/testCurl.cpp +++ b/libadhocutil/unittests/testCurl.cpp @@ -17,10 +17,34 @@ BOOST_AUTO_TEST_CASE( fetch_file ) { auto url = "file://" + rootDir.string() + "/testCurl.cpp"; CurlHandle ch(url); - ch.setopt(CURLOPT_WRITEFUNCTION, (void*)&discard); + ch.setopt(CURLOPT_WRITEFUNCTION, discard); ch.perform(); } +BOOST_AUTO_TEST_CASE( setAndGetOptions ) +{ + auto url = "file://" + rootDir.string() + "/testCurl.cpp"; + CurlHandle ch(url); + // function + ch.setopt(CURLOPT_WRITEFUNCTION, discard); + // object + ch.setopt(CURLOPT_WRITEDATA, this); + // int + ch.setopt(CURLOPT_LOCALPORT, 8000); + // long + ch.setopt(CURLOPT_LOCALPORT, 8000L); + ch.perform(); + // char * + char * eurl; + ch.getinfo(CURLINFO_EFFECTIVE_URL, eurl); + BOOST_REQUIRE_EQUAL(url, eurl); + // double + double totalTime; + ch.getinfo(CURLINFO_TOTAL_TIME, totalTime); + BOOST_REQUIRE(totalTime > 0); + BOOST_REQUIRE(totalTime < 50); +} + BOOST_AUTO_TEST_CASE( fetch_missing ) { auto url = "file://" + rootDir.string() + "/nothere"; -- cgit v1.2.3