DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages

Interface to libCURL. More...

Files

file  curl.c
 CURL Interface.
 

Data Structures

struct  curl_progress
 Allow progress monitoring.
 
struct  curl_password
 CURL Authentification callback.
 
struct  curl_post
 HTTP post data structure. More...
 
struct  basic_auth
 Basic authentification structure. More...
 
struct  curlbuf
 Buffer containing the result of a curl transaction. More...
 

Typedefs

typedef struct curl_post curl_post
 Forward decleration of structure. More...
 
typedef struct basic_auth *(* curl_authcb )(const char *, const char *, void *)
 Callback to set the authentification ie on error 401. More...
 
typedef int(* curl_progress_func )(void *, double, double, double, double)
 CURL callback function called when there is progress (CURLOPT_PROGRESSFUNCTION). More...
 
typedef void(* curl_progress_pause )(void *, int)
 Callback function to control the progress bar. More...
 
typedef void *(* curl_progress_newdata )(void *)
 Create a new progress data structure. More...
 

Functions

int curlinit (void)
 Initilise the CURL library. More...
 
void curlclose (void)
 Un reference CURL. This is required for each call to curlinit(). More...
 
struct curlbufcurl_geturl (const char *def_url, struct basic_auth *bauth, curl_authcb authcb, void *auth_data)
 Fetch the URL using CURL (HTTP GET) More...
 
struct curlbufcurl_posturl (const char *def_url, struct basic_auth *bauth, struct curl_post *post, curl_authcb authcb, void *auth_data)
 Fetch the URL using CURL (HTTP POST) More...
 
struct curlbufcurl_ungzip (struct curlbuf *cbuf)
 If the buffer contains GZIP data uncompress it. More...
 
struct basic_authcurl_newauth (const char *user, const char *passwd)
 Create a new auth structure with initial vallues. More...
 
struct curl_postcurl_newpost (void)
 Create a HTTP Post data structure. More...
 
void curl_postitem (struct curl_post *post, const char *name, const char *value)
 Add a item value pair to post structure. More...
 
char * url_escape (char *url)
 Escape and return the url. More...
 
char * url_unescape (char *url)
 UN escape and return the url. More...
 
void curl_setprogress (curl_progress_func cb, curl_progress_pause p_cb, curl_progress_newdata d_cb, void *data)
 Configure global progress handling. More...
 
void curl_setauth_cb (curl_authcb auth_cb, void *data)
 Set global password callback. More...
 
struct xml_doccurl_buf2xml (struct curlbuf *cbuf)
 Create a XML document from from buffer (application/xml) More...
 

Detailed Description

Interface to libCURL.

Typedef Documentation

typedef struct basic_auth*(* curl_authcb)(const char *, const char *, void *)

Callback to set the authentification ie on error 401.

Parameters
userInitial username (currently set)
passwdInitial password (currently set)
dataReference to data passed.
Returns
New auth structure to re attempt authentification.

Definition at line 853 of file dtsapp.h.

typedef struct curl_post curl_post

Forward decleration of structure.

Definition at line 846 of file dtsapp.h.

typedef int(* curl_progress_func)(void *, double, double, double, double)

CURL callback function called when there is progress (CURLOPT_PROGRESSFUNCTION).

Parameters
clientpReference to userdata supplied.
dltotalTotal download bytes.
dlnowCurrent bytes downloaded.
ultotalTotal upload bytes.
ulnowCurrent upload bytes.
Returns
Returning a non-zero value from this callback will cause the transfer to abort.

Definition at line 862 of file dtsapp.h.

typedef void*(* curl_progress_newdata)(void *)

Create a new progress data structure.

See Also
curl_setprogress()

curl_setprogress() allows setting a default progress callback if set it will call a callback to create a new callback progress userdata for the current session.

Parameters
dataReference to userdata supplied to curl_setprogress().
Returns
Reference to userdata to be used in current session.

Definition at line 876 of file dtsapp.h.

typedef void(* curl_progress_pause)(void *, int)

Callback function to control the progress bar.

Parameters
dataReference to userdata supplied.
stateone of 0, 1 or -1 for Pause, Unpause and Close respectfully.

Definition at line 867 of file dtsapp.h.

Function Documentation

struct xml_doc* curl_buf2xml ( struct curlbuf cbuf)

Create a XML document from from buffer (application/xml)

Parameters
cbufCURL request buffer.
Returns
Reference to XML document.

Definition at line 489 of file curl.c.

References curlbuf::body, curlbuf::bsize, curlbuf::c_type, curl_ungzip(), and xml_loadbuf().

489  {
490  struct xml_doc *xmldoc = NULL;
491 
492  if (cbuf && cbuf->c_type && !strcmp("application/xml", cbuf->c_type)) {
493  curl_ungzip(cbuf);
494  xmldoc = xml_loadbuf(cbuf->body, cbuf->bsize, 1);
495  }
496  return xmldoc;
497 }
struct xml_doc xml_doc
Forward decleration of structure.
Definition: dtsapp.h:631
struct xml_doc * xml_loadbuf(const uint8_t *buffer, uint32_t len, int validate)
Load a buffer into XML document returning refereence.
Definition: libxml2.c:168
uint8_t * body
Body buffer.
Definition: dtsapp.h:836
struct curlbuf * curl_ungzip(struct curlbuf *cbuf)
If the buffer contains GZIP data uncompress it.
Definition: curl.c:295
char * c_type
Mime Type.
Definition: dtsapp.h:838
size_t bsize
Body size.
Definition: dtsapp.h:842
struct curlbuf* curl_geturl ( const char *  def_url,
struct basic_auth bauth,
curl_authcb  authcb,
void *  auth_data 
)

Fetch the URL using CURL (HTTP GET)

Note
if no authcb is specified and curl_setauth_cb() has been called this default will be used.
Parameters
def_urlURL to fetch.
bauthBasic auth structure to initilise auth.
authcbCallback if authentification is required.
auth_dataReference to userdata passed in auth callback.
Returns
CURL buffer structure.

Definition at line 276 of file curl.c.

276  {
277  return curl_sendurl(def_url, bauth, NULL, authcb, auth_data);
278 }
struct basic_auth* curl_newauth ( const char *  user,
const char *  passwd 
)

Create a new auth structure with initial vallues.

Note
if NULL is supplied its replaced with zero length string
Parameters
userOptional initial username to set.
passwdOptional initial password to set.
Returns
Reference to new authentification structure.

Definition at line 328 of file curl.c.

References objalloc(), basic_auth::passwd, and basic_auth::user.

328  {
329  struct basic_auth *bauth;
330 
331  if (!(bauth = (struct basic_auth *)objalloc(sizeof(*bauth), curl_freeauth))) {
332  return NULL;
333  }
334  if (user) {
335  bauth->user = strdup(user);
336  } else {
337  bauth->user = strdup("");
338  }
339  if (passwd) {
340  bauth->passwd = strdup(passwd);
341  } else {
342  bauth->passwd = strdup("");
343  }
344  return bauth;
345 }
Basic authentification structure.
Definition: dtsapp.h:824
const char * user
Username.
Definition: dtsapp.h:826
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
const char * passwd
Password.
Definition: dtsapp.h:828
struct curl_post* curl_newpost ( void  )

Create a HTTP Post data structure.

Returns
Reference to new structure.

Definition at line 356 of file curl.c.

References curl_post::first, curl_post::last, and objalloc().

356  {
357  struct curl_post *post;
358  if (!(post = objalloc(sizeof(*post), free_post))) {
359  return NULL;
360  }
361  post->first = NULL;
362  post->last = NULL;
363  return post;
364 }
HTTP post data structure.
Definition: curl.c:40
struct curl_httppost * last
Last item in the list.
Definition: curl.c:44
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
struct curl_httppost * first
First item in the list.
Definition: curl.c:42
void curl_postitem ( struct curl_post post,
const char *  name,
const char *  value 
)

Add a item value pair to post structure.

Parameters
postPost structure created with curl_newpost()
nameName of the pair.
valueValue of the pair.

Definition at line 370 of file curl.c.

References curl_post::first, curl_post::last, objlock(), and objunlock().

370  {
371  if (!name || !value) {
372  return;
373  }
374  objlock(post);
375  curl_formadd(&post->first, &post->last,
376  CURLFORM_COPYNAME, name,
377  CURLFORM_COPYCONTENTS, value,
378  CURLFORM_END);
379  objunlock(post);
380 }
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
struct curl_httppost * last
Last item in the list.
Definition: curl.c:44
struct curl_httppost * first
First item in the list.
Definition: curl.c:42
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct curlbuf* curl_posturl ( const char *  def_url,
struct basic_auth bauth,
struct curl_post post,
curl_authcb  authcb,
void *  auth_data 
)

Fetch the URL using CURL (HTTP POST)

Note
if no authcb is specified and curl_setauth_cb() has been called this default will be used.
Parameters
def_urlURL to fetch.
bauthBasic auth structure to initilise auth.
postReference to curl post structure.
authcbCallback if authentification is required.
auth_dataReference to userdata passed in auth callback.
Returns
CURL buffer structure.

Definition at line 288 of file curl.c.

288  {
289  return curl_sendurl(def_url, bauth, post, authcb, auth_data);
290 }
void curl_setauth_cb ( curl_authcb  auth_cb,
void *  data 
)

Set global password callback.

Note
This will only persist as long as a reference to CURL is held use curlinit() and curlclose() at application startup and shutdown.
Parameters
auth_cbAuthentification call back.
dataReference to userdata passed in callback.

Definition at line 470 of file curl.c.

References objalloc(), objref(), and objunref().

470  {
471  if (curlpassword) {
472  objunref(curlpassword);
473  curlpassword = NULL;
474  }
475 
476  if (!(curlpassword = objalloc(sizeof(*curlpassword), free_curlpassword))) {
477  return;
478  }
479 
480  curlpassword->authcb = auth_cb;
481  if (data && objref(data)) {
482  curlpassword->data = data;
483  }
484 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void curl_setprogress ( curl_progress_func  cb,
curl_progress_pause  p_cb,
curl_progress_newdata  d_cb,
void *  data 
)

Configure global progress handling.

Note
This will only persist as long as a reference to CURL is held use curlinit() and curlclose() at application startup and shutdown.
Parameters
cbCURL progress function callback.
p_cbCURL progress control (pause) callback.
d_cbCURL progress data allocation callback.
datainitial data passed to d_cb.
See Also
curl_progress_func()
curl_progress_pause()
curl_progress_newdata()

Definition at line 442 of file curl.c.

References objalloc(), objref(), and objunref().

442  {
443  if (curlprogress) {
444  objunref(curlprogress);
445  curlprogress = NULL;
446  }
447 
448  if (!(curlprogress = objalloc(sizeof(*curlprogress), free_progress))) {
449  return;
450  }
451  curlprogress->cb = cb;
452  curlprogress->d_cb = d_cb;
453  curlprogress->p_cb = p_cb;
454  if (data && objref(data)) {
455  curlprogress->data = data;
456  }
457 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct curlbuf* curl_ungzip ( struct curlbuf cbuf)

If the buffer contains GZIP data uncompress it.

Parameters
cbufCurl buffer to uncompress.
Returns
Pointer to cbuf with the body replaced uncompressed.

Definition at line 295 of file curl.c.

References curlbuf::body, curlbuf::bsize, gzinflatebuf(), and is_gzip().

Referenced by curl_buf2xml().

295  {
296  uint8_t *gzbuf;
297  uint32_t len;
298 
299  if (is_gzip((uint8_t *)cbuf->body, cbuf->bsize) &&
300  ((gzbuf = gzinflatebuf((uint8_t *)cbuf->body, cbuf->bsize, &len)))) {
301  free(cbuf->body);
302  cbuf->body = gzbuf;
303  cbuf->bsize = len;
304  }
305  return cbuf;
306 }
uint8_t * gzinflatebuf(uint8_t *buf_in, int buf_size, uint32_t *len)
Ungzip a buffer.
Definition: zlib.c:101
uint8_t * body
Body buffer.
Definition: dtsapp.h:836
int is_gzip(uint8_t *buf, int buf_size)
check a buffer if it contains gzip magic
Definition: zlib.c:85
size_t bsize
Body size.
Definition: dtsapp.h:842
void curlclose ( void  )

Un reference CURL. This is required for each call to curlinit().

Definition at line 122 of file curl.c.

References objunref().

122  {
123  objunref(curl_isinit);
124  curl_isinit = NULL;
125 }
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int curlinit ( void  )

Initilise the CURL library.

Note
Curl functions will initilize and unreference curl when done it is best the application hold a reference to benifit from caching. curlclose() Must be called if it has been used

Definition at line 92 of file curl.c.

References objalloc(), objlock(), objref(), objunlock(), and objunref().

Referenced by url_escape(), and url_unescape().

92  {
93  if (curl_isinit) {
94  return objref(curl_isinit);
95  }
96 
97  if (!(curl_isinit = objalloc(sizeof(void *),curlfree))) {
98  return 0;
99  }
100 
101  objlock(curl_isinit);
102  if (!(curl = curl_easy_init())) {
103  objunlock(curl_isinit);
104  objunref(curl_isinit);
105  return 0;
106  }
107 
108  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
109  curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
110  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
111 
112  curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0 [Distro Solutions]");
113 
114  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bodytobuffer);
115  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headertobuffer);
116  objunlock(curl_isinit);
117  return 1;
118 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
char* url_escape ( char *  url)

Escape and return the url.

Parameters
urlURL to escape
Returns
A malloc()'d URL that needs to be free()'d

Definition at line 385 of file curl.c.

References curlinit(), objlock(), objunlock(), and objunref().

385  {
386  char *esc;
387  char *ret = NULL;
388 
389  if (!curlinit()) {
390  return NULL;
391  }
392 
393  objlock(curl_isinit);
394  esc = curl_easy_escape(curl, url, 0);
395  if (esc) {
396  ret = strdup(esc);
397  }
398  curl_free(esc);
399  objunlock(curl_isinit);
400  objunref(curl_isinit);
401  return ret;
402 }
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int curlinit(void)
Initilise the CURL library.
Definition: curl.c:92
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
char* url_unescape ( char *  url)

UN escape and return the url.

Parameters
urlURL to un escape
Returns
A malloc()'d URL that needs to be free()'d

Definition at line 407 of file curl.c.

References curlinit(), objlock(), objunlock(), and objunref().

407  {
408  char *uesc;
409  char *ret = NULL;
410 
411  if (!curlinit()) {
412  return NULL;
413  }
414 
415  objlock(curl_isinit);
416  uesc = curl_easy_unescape(curl, url, 0, 0);
417  if (uesc) {
418  ret = strdup(uesc);
419  }
420  curl_free(uesc);
421  objunlock(curl_isinit);
422  objunref(curl_isinit);
423  return ret;
424 }
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int curlinit(void)
Initilise the CURL library.
Definition: curl.c:92
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184