ddc
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
CCurl Class Reference

CCurl: abstract term expander using libcurl. More...

#include <CCurl.h>

Inheritance diagram for CCurl:
Inheritance graph
[legend]
Collaboration diagram for CCurl:
Collaboration graph
[legend]

Public Types

typedef size_t(* CurlWriteDataCallback) (char *ptr, size_t size, size_t n, void *userdata)
 curl response content handler callback type; should return the number of bytes actually taken care of (== size*n on success) More...
 
typedef size_t(* CurlWriteHeaderCallback) (void *ptr, size_t size, size_t n, void *userdata)
 curl response header callback type; should return the number of bytes actually taken care of (== size*n on success) More...
 
typedef curl_socket_t(* CurlOpenSocketCallback) (void *clientp, curlsocktype purpose, struct_curl_sockaddr *address)
 curl open socket callback type, should return the socket fd More...
 
typedef int(* CurlSocketOptionCallback) (void *clientp, curl_socket_t curlfd, curlsocktype purpose)
 curl socket option callback type More...
 

Public Member Functions

 CCurl (const string &url_=string(""), long timeout_=60, long redirects_=10)
 constructor implicitly calls url(url) More...
 
 ~CCurl (void)
 destructor implicitly frees m_Curl More...
 
void reset (void)
 prepare for another perform(): clears m_CurlErr, m_Response, and m_Headers More...
 
bool perform (void)
 
bool perform_cached (CCurlCache *cache=&ddcCurlCache)
 
bool SetDefaultOptions ()
 Set default options: More...
 
const string & url (void) const
 get current URL More...
 
void url (const string &url)
 
const string & requestUrl (void) const
 get low-level curl request URL More...
 
void requestUrl (const string &url)
 set low-level curl request URL (wraps CURLOPT_URL) More...
 
const string & unixSocketPath (void) const
 get low-level unix socket path More...
 
void unixSocketPath (const string &path)
 set unix socket path (m_UnixSocketPath); calls setUnixSocketPath() More...
 
long timeout (void) const
 get current timeout (seconds) More...
 
void timeout (long seconds)
 set timeout (seconds) More...
 
long redirects (void)
 get maximum number of redirects (-1: no limit) More...
 
void redirects (long redirects_)
 set maximum number of redirects (-1: no limit, 0: disabled) More...
 
void forbidReuse (int flag)
 wrap CURLOPT_FORBID_REUSE: if true, a new connection will be established for each perform() More...
 
void noSignal (int flag)
 wrap CURLOPT_NOSIGNAL: if true, prevents CURL from using signals; may be required for multi-threaded apps More...
 
void failOnError (int flag)
 wrap CURLOPT_FAILONERR: if true, tells CURL to fail silently if the HTTP code returned is equal to or larger than 400 More...
 
void verbose (int flag)
 wrap CURLOPT_VERBOSE (debug) More...
 
void setDataCallback (CurlWriteDataCallback handler=NULL, void *data=NULL)
 set response data callback and data: wraps CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA More...
 
void setDefaultDataCallback (void)
 set default response data callback: catch all response data into m_Response More...
 
void setHeaderCallback (CurlWriteHeaderCallback handler=NULL, void *data=NULL)
 set response header callback and data: wraps CURLOPT_HEADERFUNCTION, CURLOPT_HEADERDATA More...
 
void setDefaultHeaderCallback (void)
 set default response data callback: catch all headers in m_Headers More...
 
void setUnixSocketCallbacks (void)
 setup UNIX socket callbacks for m_UnixSocketPath (for libcurl < 7.40.0) More...
 
- Public Member Functions inherited from ddcLockable
 ddcLockable ()
 
virtual ~ddcLockable ()
 
int lock () const
 
int unlock () const
 

Static Public Member Functions

static bool parseUnixSocketUrl (const string &url, string &unixPath, string &requestUrl)
 
static size_t cbWriteData_ (char *ptr, size_t size, size_t n, string *bufp)
 default response data callback (cast to CurlWriteCallback for use with CURLOPT_WRITEFUNCTION) More...
 
static size_t cbWriteHeader_ (void *ptr, size_t size, size_t n, vector< string > *bufvp)
 default response header callback More...
 
static curl_socket_t cbOpenUnixSocket_ (void *clientp, curlsocktype purpose, struct_curl_sockaddr *address)
 open UNIX socket callback (CURLOPT_UNIX_SOCKET_PATH emulation) More...
 
static int cbUnixSocketOption_ (void *clientp, curl_socket_t curlfd, curlsocktype purpose)
 setsockopt wrapper for UNIX sockets (CURLOPT_UNIX_SOCKET_PATH emulation) More...
 

Public Attributes

CurlHandle m_Curl
 curl handle More...
 
string m_Url
 current url (maybe UNIX-socket URL) More...
 
string m_RequestUrl
 current request URL (passed to libcurl) More...
 
string m_UnixSocketPath
 unix socket path (override) More...
 
long m_Timeout
 current timeout (seconds; default=60) More...
 
long m_Redirects
 maximum number of redirects (-1:no limit, 0:none) More...
 
char m_CurlErr [CURL_ERROR_SIZE]
 local curl error buffer More...
 
string m_Response
 response content buffer (used by default write callback) More...
 
vector< string > m_Headers
 response headers (used by default header callback) More...
 
- Public Attributes inherited from ddcLockable
pthread_mutex_t m_Mutex
 

Detailed Description

CCurl: abstract term expander using libcurl.

Member Typedef Documentation

◆ CurlWriteDataCallback

typedef size_t(* CCurl::CurlWriteDataCallback) (char *ptr, size_t size, size_t n, void *userdata)

curl response content handler callback type; should return the number of bytes actually taken care of (== size*n on success)

◆ CurlWriteHeaderCallback

typedef size_t(* CCurl::CurlWriteHeaderCallback) (void *ptr, size_t size, size_t n, void *userdata)

curl response header callback type; should return the number of bytes actually taken care of (== size*n on success)

◆ CurlOpenSocketCallback

typedef curl_socket_t(* CCurl::CurlOpenSocketCallback) (void *clientp, curlsocktype purpose, struct_curl_sockaddr *address)

curl open socket callback type, should return the socket fd

◆ CurlSocketOptionCallback

typedef int(* CCurl::CurlSocketOptionCallback) (void *clientp, curl_socket_t curlfd, curlsocktype purpose)

curl socket option callback type

Constructor & Destructor Documentation

◆ CCurl()

CCurl::CCurl ( const string &  url_ = string(""),
long  timeout_ = 60,
long  redirects_ = 10 
)

constructor implicitly calls url(url)

References ddcInitCurl(), m_Curl, m_CurlErr, redirects(), reset(), timeout(), and url().

Here is the call graph for this function:

◆ ~CCurl()

CCurl::~CCurl ( void  )

destructor implicitly frees m_Curl

References m_Curl.

Member Function Documentation

◆ reset()

void CCurl::reset ( void  )

prepare for another perform(): clears m_CurlErr, m_Response, and m_Headers

References m_CurlErr, m_Headers, and m_Response.

Referenced by CCurl(), perform(), and perform_cached().

Here is the caller graph for this function:

◆ perform()

bool CCurl::perform ( void  )

Get response for request in m_Curl: wrapper for curl_easy_perform

  • Returns false on error and populates m_CurlErr if the default callbacks are used
  • implicitly calls reset()

References m_Curl, m_CurlErr, and reset().

Here is the call graph for this function:

◆ perform_cached()

bool CCurl::perform_cached ( CCurlCache cache = &ddcCurlCache)

Wrapper for perform() which uses a CCurlCache to guarantee that at most 1 query is sent per process and url. Really only useful in conjunction with the default callbacks or something custom which populates m_Response with the data to be cached.

  • implicitly calls reset()
  • may call perform()
  • on completion, cache should have an entry for the current m_Url

References ddcLRUCache< Key, Val >::end(), ddcLRUCache< Key, Val >::get(), ddcLRUCache< Key, Val >::insert(), ddcLRUCache< Key, Val >::lock(), m_Curl, m_Response, m_Url, reset(), and ddcLRUCache< Key, Val >::unlock().

Referenced by TxCurl::perform().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetDefaultOptions()

bool CCurl::SetDefaultOptions ( )

Set default options:

◆ url() [1/2]

const string& CCurl::url ( void  ) const
inline

get current URL

References m_Url, parseUnixSocketUrl(), and requestUrl().

Referenced by CCurl(), parseUnixSocketUrl(), requestUrl(), and url().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ url() [2/2]

void CCurl::url ( const string &  url)

set current URL (sets m_Url, calls requestUrl(URL) and unixSocketPath(PATH)); auto-detects UNIX socket URLs of the form:

  • "http:/path/to/unix/socket//uri/path" (perl LWP::Protocol::http::SocketUnixAlt style)
  • "unix:/path/to/unix/socket|http:///uri/path" (apache mod_proxy style)
  • "http+unix:/path/to/unix/socket//uri/path" (native)
  • "http+unix:/path/to/unix/socket|/uri/path" (native)

References m_RequestUrl, m_UnixSocketPath, m_Url, parseUnixSocketUrl(), requestUrl(), unixSocketPath(), and url().

Here is the call graph for this function:

◆ parseUnixSocketUrl()

bool CCurl::parseUnixSocketUrl ( const string &  url,
string &  unixPath,
string &  requestUrl 
)
static

guts for parsing unix socket URLs, called by url(url)

Parameters
[in]urluser-specified URL to parse
[out]unixPathparsed unix socket path (empty on failure)
[out]requestUrlparsed request URL (empty on failure)
Returns
true iff a unix socket URL was detected, in which case unixPath is empty and requestUrl==url

References url().

Referenced by TxCab::compile(), and url().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ requestUrl() [1/2]

const string& CCurl::requestUrl ( void  ) const
inline

get low-level curl request URL

References m_RequestUrl.

Referenced by url().

Here is the caller graph for this function:

◆ requestUrl() [2/2]

void CCurl::requestUrl ( const string &  url)

set low-level curl request URL (wraps CURLOPT_URL)

References m_Curl, m_RequestUrl, and url().

Here is the call graph for this function:

◆ unixSocketPath() [1/2]

const string& CCurl::unixSocketPath ( void  ) const
inline

get low-level unix socket path

References m_UnixSocketPath.

Referenced by url().

Here is the caller graph for this function:

◆ unixSocketPath() [2/2]

void CCurl::unixSocketPath ( const string &  path)

set unix socket path (m_UnixSocketPath); calls setUnixSocketPath()

References m_UnixSocketPath, and setUnixSocketCallbacks().

Here is the call graph for this function:

◆ timeout() [1/2]

long CCurl::timeout ( void  ) const
inline

get current timeout (seconds)

References m_Timeout.

Referenced by CCurl().

Here is the caller graph for this function:

◆ timeout() [2/2]

void CCurl::timeout ( long  seconds)

set timeout (seconds)

References m_Curl, and m_Timeout.

◆ redirects() [1/2]

long CCurl::redirects ( void  )
inline

get maximum number of redirects (-1: no limit)

References failOnError(), forbidReuse(), m_Redirects, noSignal(), setDataCallback(), and verbose().

Referenced by CCurl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redirects() [2/2]

void CCurl::redirects ( long  redirects_)

set maximum number of redirects (-1: no limit, 0: disabled)

References CURL_REDIR_POST_ALL, m_Curl, and m_Redirects.

◆ forbidReuse()

void CCurl::forbidReuse ( int  flag)

wrap CURLOPT_FORBID_REUSE: if true, a new connection will be established for each perform()

References m_Curl.

Referenced by redirects().

Here is the caller graph for this function:

◆ noSignal()

void CCurl::noSignal ( int  flag)

wrap CURLOPT_NOSIGNAL: if true, prevents CURL from using signals; may be required for multi-threaded apps

References m_Curl.

Referenced by redirects().

Here is the caller graph for this function:

◆ failOnError()

void CCurl::failOnError ( int  flag)

wrap CURLOPT_FAILONERR: if true, tells CURL to fail silently if the HTTP code returned is equal to or larger than 400

References m_Curl.

Referenced by redirects().

Here is the caller graph for this function:

◆ verbose()

void CCurl::verbose ( int  flag)

wrap CURLOPT_VERBOSE (debug)

References m_Curl.

Referenced by redirects().

Here is the caller graph for this function:

◆ setDataCallback()

void CCurl::setDataCallback ( CurlWriteDataCallback  handler = NULL,
void *  data = NULL 
)

set response data callback and data: wraps CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA

References m_Curl.

Referenced by redirects(), and setDefaultDataCallback().

Here is the caller graph for this function:

◆ setDefaultDataCallback()

void CCurl::setDefaultDataCallback ( void  )
inline

set default response data callback: catch all response data into m_Response

References cbWriteData_(), setDataCallback(), and setHeaderCallback().

Here is the call graph for this function:

◆ setHeaderCallback()

void CCurl::setHeaderCallback ( CurlWriteHeaderCallback  handler = NULL,
void *  data = NULL 
)

set response header callback and data: wraps CURLOPT_HEADERFUNCTION, CURLOPT_HEADERDATA

References m_Curl.

Referenced by setDefaultDataCallback(), and setDefaultHeaderCallback().

Here is the caller graph for this function:

◆ setDefaultHeaderCallback()

void CCurl::setDefaultHeaderCallback ( void  )
inline

set default response data callback: catch all headers in m_Headers

References cbOpenUnixSocket_(), cbUnixSocketOption_(), cbWriteData_(), cbWriteHeader_(), setHeaderCallback(), and setUnixSocketCallbacks().

Here is the call graph for this function:

◆ cbWriteData_()

size_t CCurl::cbWriteData_ ( char *  ptr,
size_t  size,
size_t  n,
string *  bufp 
)
static

default response data callback (cast to CurlWriteCallback for use with CURLOPT_WRITEFUNCTION)

Referenced by setDefaultDataCallback(), and setDefaultHeaderCallback().

Here is the caller graph for this function:

◆ cbWriteHeader_()

size_t CCurl::cbWriteHeader_ ( void *  ptr,
size_t  size,
size_t  n,
vector< string > *  bufvp 
)
static

default response header callback

Referenced by setDefaultHeaderCallback().

Here is the caller graph for this function:

◆ setUnixSocketCallbacks()

void CCurl::setUnixSocketCallbacks ( void  )

setup UNIX socket callbacks for m_UnixSocketPath (for libcurl < 7.40.0)

References cbOpenUnixSocket_(), cbUnixSocketOption_(), errOther, m_Curl, and m_UnixSocketPath.

Referenced by setDefaultHeaderCallback(), and unixSocketPath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cbOpenUnixSocket_()

curl_socket_t CCurl::cbOpenUnixSocket_ ( void *  clientp,
curlsocktype  purpose,
struct_curl_sockaddr address 
)
static

open UNIX socket callback (CURLOPT_UNIX_SOCKET_PATH emulation)

References m_UnixSocketPath.

Referenced by setDefaultHeaderCallback(), and setUnixSocketCallbacks().

Here is the caller graph for this function:

◆ cbUnixSocketOption_()

int CCurl::cbUnixSocketOption_ ( void *  clientp,
curl_socket_t  curlfd,
curlsocktype  purpose 
)
static

setsockopt wrapper for UNIX sockets (CURLOPT_UNIX_SOCKET_PATH emulation)

Referenced by setDefaultHeaderCallback(), and setUnixSocketCallbacks().

Here is the caller graph for this function:

Member Data Documentation

◆ m_Curl

CurlHandle CCurl::m_Curl

◆ m_Url

string CCurl::m_Url

current url (maybe UNIX-socket URL)

Referenced by perform_cached(), and url().

◆ m_RequestUrl

string CCurl::m_RequestUrl

current request URL (passed to libcurl)

Referenced by requestUrl(), and url().

◆ m_UnixSocketPath

string CCurl::m_UnixSocketPath

unix socket path (override)

Referenced by cbOpenUnixSocket_(), setUnixSocketCallbacks(), unixSocketPath(), and url().

◆ m_Timeout

long CCurl::m_Timeout

current timeout (seconds; default=60)

Referenced by timeout().

◆ m_Redirects

long CCurl::m_Redirects

maximum number of redirects (-1:no limit, 0:none)

Referenced by redirects().

◆ m_CurlErr

char CCurl::m_CurlErr[CURL_ERROR_SIZE]

local curl error buffer

Referenced by CCurl(), perform(), and reset().

◆ m_Response

string CCurl::m_Response

response content buffer (used by default write callback)

Referenced by perform_cached(), and reset().

◆ m_Headers

vector<string> CCurl::m_Headers

response headers (used by default header callback)

Referenced by reset().


The documentation for this class was generated from the following files: