28 #ifndef curlite_hpp_120129ad_36b2_4224_b790_d1658ce03bb4 29 #define curlite_hpp_120129ad_36b2_4224_b790_d1658ce03bb4 31 #include <curl/curl.h> 40 #if LIBCURL_VERSION_MAJOR < 7 || LIBCURL_VERSION_MINOR < 32 41 #error "This version of curlite is incompatible with your cURL version" 46 template <
class FunctionPtr>
47 struct Handler {
typedef std::function<typename std::remove_pointer<FunctionPtr>::type>
type; };
72 :
std::runtime_error( message )
93 std::unique_ptr<Pimpl>
_impl;
96 void operator = (
Easy const &other );
98 bool handleError( CURLcode code );
115 operator bool()
const;
133 void setExceptionMode(
bool throwExceptions );
138 bool exceptionMode()
const;
143 CURLcode error()
const;
148 std::string errorString()
const;
153 void *userData()
const;
158 void setUserData(
void *data );
164 template <
class ValueType>
165 bool set( CURLoption opt, ValueType value );
167 bool set( CURLoption key,
int value );
168 bool set( CURLoption key,
bool value );
169 bool set( CURLoption key, std::string
const &value );
175 template <
class ValueType>
176 ValueType getInfo( CURLINFO key, ValueType
const &defaultValue = ValueType() );
187 bool pause(
int bitmask );
207 std::string escape( std::string
const &url );
212 std::string unescape( std::string
const &url );
218 size_t send(
const char *buffer,
size_t bufferSize );
224 size_t recv(
char *buffer,
size_t bufferSize );
227 void onRead_( SimplifiedDataHandler f );
228 void onWrite_( SimplifiedDataHandler f );
229 void onHeader_( SimplifiedDataHandler f );
231 void onProgress_( SimplifiedProgressHandler f );
234 void onRead( ReadHandler f =
ReadHandler(),
void *data =
nullptr );
235 void onWrite( WriteHandler f =
WriteHandler(),
void *data =
nullptr );
236 void onHeader( WriteHandler f =
WriteHandler(),
void *data =
nullptr );
238 void onIoctl( IoctlHandler f =
IoctlHandler(),
void *data =
nullptr );
239 void onSeek( SeekHandler f =
SeekHandler(),
void *data =
nullptr );
240 void onFnMatch( FnMatchHandler f =
FnMatchHandler(),
void *data =
nullptr );
242 void onProgress( ProgressHandler f =
ProgressHandler(),
void *data =
nullptr );
243 void onXferInfo( XferInfoHandler f =
XferInfoHandler(),
void *data =
nullptr );
245 void onCloseSocket( CloseSocketHandler f =
CloseSocketHandler(),
void *data =
nullptr );
246 void onOpenSocket( OpenSocketHandler f =
OpenSocketHandler(),
void *data =
nullptr );
247 void onSockOpt( SockOptHandler f =
SockOptHandler(),
void *data =
nullptr );
249 void onChunkBegin( ChunkBeginHandler f =
ChunkBeginHandler(),
void *data =
nullptr );
250 void onChunkEnd( ChunkEndHandler f =
ChunkEndHandler(),
void *data =
nullptr );
252 void onSslContext( SslContextHandler f =
SslContextHandler(),
void *data =
nullptr );
253 void onDebug( DebugHandler f =
DebugHandler(),
void *data =
nullptr );
294 #pragma GCC diagnostic push 295 #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" 296 template <
class ValueType>
305 bool isValueAllowedNullPtr = std::is_same<ValueType, std::nullptr_t>::value &&
306 keyTypeCode != CURLOPTTYPE_LONG;
309 err = CURLE_BAD_FUNCTION_ARGUMENT;
311 err = curl_easy_setopt(
get(), key, value );
314 return handleError( err );
316 #pragma GCC diagnostic pop 320 return set( key,
static_cast<long>( value ) );
325 return set( key,
static_cast<long>( value ) );
328 inline bool Easy::set( CURLoption key, std::string
const &value )
330 return set( key, value.c_str() );
335 template <>
struct InfoTypeCode<char*> {
enum { value = CURLINFO_STRING }; };
336 template <>
struct InfoTypeCode<long> {
enum { value = CURLINFO_LONG }; };
337 template <>
struct InfoTypeCode<double> {
enum { value = CURLINFO_DOUBLE }; };
338 template <>
struct InfoTypeCode<curl_slist*> {
enum { value = CURLINFO_SLIST }; };
339 template <>
struct InfoTypeCode<curl_certinfo*> {
enum { value = CURLINFO_SLIST }; };
340 template <>
struct InfoTypeCode<curl_tlssessioninfo*> {
enum { value = CURLINFO_SLIST }; };
342 template <
class ValueType>
351 err = CURLE_BAD_FUNCTION_ARGUMENT;
353 err = curl_easy_getinfo(
get(), key, &value );
356 return handleError( err ) ? value : defaultValue;
360 inline std::string
Easy::getInfo( CURLINFO key, std::string
const &defaultValue )
362 const char* value = getInfo<char*>( key, nullptr );
363 return value ? value : defaultValue;
382 void operator = (
List const &other );
384 List( curl_slist *list =
nullptr );
385 List( std::vector<std::string>
const &values );
395 curl_slist *
get()
const;
401 curl_slist *release();
407 List &
append( std::vector<std::string>
const &values );
429 void operator = (
Form const &other );
433 Form( std::vector<curl_forms>
const &forms );
443 curl_httppost *
get()
const;
452 std::pair<curl_httppost*, curl_httppost*> release();
459 bool add( std::vector<curl_forms>
const &forms );
484 curl_version_info_data *
versionInfo( CURLversion
type = CURLVERSION_NOW );
493 Easy download( std::string
const &url, std::ostream &ostr,
bool followRedirect =
true,
bool throwExceptions =
true );
505 std::string
const &url,
506 std::string
const &username =
"",
507 std::string
const &password =
"",
508 curl_off_t size = -1,
509 bool throwExceptions =
true );
Handler< curl_opensocket_callback >::type OpenSocketHandler
Handler< curl_ssl_ctx_callback >::type SslContextHandler
Exception(char const *message)
Handler< curl_fnmatch_callback >::type FnMatchHandler
std::function< bool(char *, size_t)> SimplifiedDataHandler
std::function< bool(curl_off_t, curl_off_t, curl_off_t, curl_off_t)> SimplifiedProgressHandler
Handler< curl_chunk_end_callback >::type ChunkEndHandler
Handler< curl_debug_callback >::type DebugHandler
Easy upload(std::istream &istr, std::string const &url, std::string const &username="", std::string const &password="", curl_off_t size=-1, bool throwExceptions=true)
Handler< curl_ioctl_callback >::type IoctlHandler
curl_version_info_data * versionInfo(CURLversion type=CURLVERSION_NOW)
std::ostream & operator<<(std::ostream &stream, Easy &curlite)
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
Handler< curl_read_callback >::type ReadHandler
Handler< curl_write_callback >::type WriteHandler
Handler< curl_seek_callback >::type SeekHandler
bool global_init(long flags=CURL_GLOBAL_ALL)
std::unique_ptr< Pimpl > _impl
std::istream & operator>>(std::istream &stream, Easy &curlite)
Handler< curl_closesocket_callback >::type CloseSocketHandler
std::function< typename std::remove_pointer< FunctionPtr >::type > type
ValueType getInfo(CURLINFO key, ValueType const &defaultValue=ValueType())
Handler< curl_conv_callback >::type ConvHandler
Handler< curl_xferinfo_callback >::type XferInfoHandler
Easy download(std::string const &url, std::ostream &ostr, bool followRedirect=true, bool throwExceptions=true)
bool set(CURLoption opt, ValueType value)
static const int kCurlOptTypeInterval
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
Handler< curl_progress_callback >::type ProgressHandler
Handler< curl_chunk_bgn_callback >::type ChunkBeginHandler
Handler< curl_formget_callback >::type FormGetHandler
Handler< curl_sockopt_callback >::type SockOptHandler