ddc
|
CCurl: abstract term expander using libcurl. More...
#include <CCurl.h>
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... | |
![]() | |
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... | |
![]() | |
pthread_mutex_t | m_Mutex |
CCurl: abstract term expander using libcurl.
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)
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)
typedef curl_socket_t(* CCurl::CurlOpenSocketCallback) (void *clientp, curlsocktype purpose, struct_curl_sockaddr *address) |
curl open socket callback type, should return the socket fd
typedef int(* CCurl::CurlSocketOptionCallback) (void *clientp, curl_socket_t curlfd, curlsocktype purpose) |
curl socket option callback type
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().
CCurl::~CCurl | ( | void | ) |
destructor implicitly frees m_Curl
References m_Curl.
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().
bool CCurl::perform | ( | void | ) |
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.
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().
bool CCurl::SetDefaultOptions | ( | ) |
Set default options:
|
inline |
get current URL
References m_Url, parseUnixSocketUrl(), and requestUrl().
Referenced by CCurl(), parseUnixSocketUrl(), requestUrl(), and url().
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:
References m_RequestUrl, m_UnixSocketPath, m_Url, parseUnixSocketUrl(), requestUrl(), unixSocketPath(), and url().
|
static |
guts for parsing unix socket URLs, called by url(url)
[in] | url | user-specified URL to parse |
[out] | unixPath | parsed unix socket path (empty on failure) |
[out] | requestUrl | parsed request URL (empty on failure) |
References url().
Referenced by TxCab::compile(), and url().
|
inline |
get low-level curl request URL
References m_RequestUrl.
Referenced by url().
void CCurl::requestUrl | ( | const string & | url | ) |
set low-level curl request URL (wraps CURLOPT_URL)
References m_Curl, m_RequestUrl, and url().
|
inline |
get low-level unix socket path
References m_UnixSocketPath.
Referenced by url().
void CCurl::unixSocketPath | ( | const string & | path | ) |
set unix socket path (m_UnixSocketPath); calls setUnixSocketPath()
References m_UnixSocketPath, and setUnixSocketCallbacks().
|
inline |
|
inline |
get maximum number of redirects (-1: no limit)
References failOnError(), forbidReuse(), m_Redirects, noSignal(), setDataCallback(), and verbose().
Referenced by CCurl().
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.
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().
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().
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().
void CCurl::verbose | ( | int | flag | ) |
wrap CURLOPT_VERBOSE (debug)
References m_Curl.
Referenced by redirects().
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().
|
inline |
set default response data callback: catch all response data into m_Response
References cbWriteData_(), setDataCallback(), and 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().
|
inline |
set default response data callback: catch all headers in m_Headers
References cbOpenUnixSocket_(), cbUnixSocketOption_(), cbWriteData_(), cbWriteHeader_(), setHeaderCallback(), and setUnixSocketCallbacks().
|
static |
default response data callback (cast to CurlWriteCallback for use with CURLOPT_WRITEFUNCTION)
Referenced by setDefaultDataCallback(), and setDefaultHeaderCallback().
|
static |
default response header callback
Referenced by setDefaultHeaderCallback().
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().
|
static |
open UNIX socket callback (CURLOPT_UNIX_SOCKET_PATH emulation)
References m_UnixSocketPath.
Referenced by setDefaultHeaderCallback(), and setUnixSocketCallbacks().
|
static |
setsockopt wrapper for UNIX sockets (CURLOPT_UNIX_SOCKET_PATH emulation)
Referenced by setDefaultHeaderCallback(), and setUnixSocketCallbacks().
CurlHandle CCurl::m_Curl |
curl handle
Referenced by CCurl(), failOnError(), forbidReuse(), noSignal(), perform(), perform_cached(), redirects(), requestUrl(), setDataCallback(), setHeaderCallback(), setUnixSocketCallbacks(), timeout(), verbose(), and ~CCurl().
string CCurl::m_Url |
current url (maybe UNIX-socket URL)
Referenced by perform_cached(), and url().
string CCurl::m_RequestUrl |
current request URL (passed to libcurl)
Referenced by requestUrl(), and url().
string CCurl::m_UnixSocketPath |
unix socket path (override)
Referenced by cbOpenUnixSocket_(), setUnixSocketCallbacks(), unixSocketPath(), and url().
long CCurl::m_Timeout |
current timeout (seconds; default=60)
Referenced by timeout().
long CCurl::m_Redirects |
maximum number of redirects (-1:no limit, 0:none)
Referenced by redirects().
char CCurl::m_CurlErr[CURL_ERROR_SIZE] |
string CCurl::m_Response |
response content buffer (used by default write callback)
Referenced by perform_cached(), and reset().
vector<string> CCurl::m_Headers |
response headers (used by default header callback)
Referenced by reset().