11 #include <curl/curl.h>
12 #include <curl/easy.h>
16 static void *curl_isinit = NULL;
17 static CURL *curl = NULL;
20 static struct curl_progress {
29 } *curlprogress = NULL;
32 static struct curl_password {
37 } *curlpassword = NULL;
44 struct curl_httppost *
last;
47 static size_t bodytobuffer(
void *ptr,
size_t size,
size_t nmemb,
void *userdata) {
48 size_t bufsize = size * nmemb;
51 if (!(mem->
body = realloc(mem->
body, mem->
bsize + bufsize + 1))) {
54 memcpy(&(mem->
body[mem->
bsize]), ptr, bufsize);
55 mem->
bsize += bufsize;
60 static size_t headertobuffer(
void *ptr,
size_t size,
size_t nmemb,
void *userdata) {
61 size_t bufsize = size * nmemb;
68 mem->
hsize += bufsize;
73 static void curlfree(
void *data) {
75 curl_easy_cleanup(curl);
94 return objref(curl_isinit);
97 if (!(curl_isinit =
objalloc(
sizeof(
void *),curlfree))) {
102 if (!(curl = curl_easy_init())) {
108 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
109 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
110 curl_easy_setopt(curl, CURLOPT_COOKIEFILE,
"");
112 curl_easy_setopt(curl, CURLOPT_USERAGENT,
"libcurl-agent/1.0 [Distro Solutions]");
114 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bodytobuffer);
115 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headertobuffer);
127 static void emptybuffer(
void *data) {
128 struct curlbuf *writebuf = data;
134 if (writebuf->
body) {
135 free(writebuf->
body);
142 writebuf->
body = NULL;
157 void *auth_data = auth_data_in;
165 if (!(writebuf =
objalloc(
sizeof(*writebuf), emptybuffer))) {
171 curl_easy_setopt(curl, CURLOPT_URL, def_url);
174 curl_easy_setopt(curl, CURLOPT_WRITEDATA, writebuf);
175 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, writebuf);
179 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post->
first);
183 snprintf(userpass, 63,
"%s:%s", auth->
user, auth->
passwd);
184 curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
190 if (curlprogress && ((p_data = curlprogress->d_cb(curlprogress->data)))) {
191 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
192 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlprogress->cb);
193 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, p_data);
196 if (curlpassword && !authcb) {
197 authcb = curlpassword->authcb;
198 auth_data = curlpassword->data;
202 if (!(res = curl_easy_perform(curl))) {
203 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res);
207 if (curlprogress && curlprogress->p_cb) {
208 curlprogress->p_cb(p_data, 1);
210 if ((authcb) && ((auth = authcb((auth) ? auth->
user :
"", (auth) ? auth->
passwd :
"", auth_data)))) {
211 snprintf(userpass, 63,
"%s:%s", auth->
user, auth->
passwd);
212 curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
213 emptybuffer(writebuf);
218 if (curlprogress && curlprogress->p_cb) {
219 curlprogress->p_cb(p_data, 0);
228 curl_easy_getinfo(curl,CURLINFO_REDIRECT_URL, &url);
229 curl_easy_setopt(curl, CURLOPT_URL, url);
230 emptybuffer(writebuf);
235 curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &writebuf->
c_type);
240 }
while ((res != 200) && (i < 3));
256 if (curlprogress && curlprogress->p_cb) {
257 curlprogress->p_cb(p_data, -1);
277 return curl_sendurl(def_url, bauth, NULL, authcb, auth_data);
289 return curl_sendurl(def_url, bauth, post, authcb, auth_data);
308 static void curl_freeauth(
void *data) {
314 memset((
void *)bauth->
user, 0, strlen(bauth->
user));
315 free((
void *)bauth->
user);
318 memset((
void *)bauth->
passwd, 0, strlen(bauth->
passwd));
319 free((
void *)bauth->
passwd);
335 bauth->
user = strdup(user);
337 bauth->
user = strdup(
"");
340 bauth->
passwd = strdup(passwd);
342 bauth->
passwd = strdup(
"");
347 static void free_post(
void *data) {
350 curl_formfree(post->
first);
358 if (!(post =
objalloc(
sizeof(*post), free_post))) {
371 if (!name || !value) {
376 CURLFORM_COPYNAME, name,
377 CURLFORM_COPYCONTENTS, value,
394 esc = curl_easy_escape(curl, url, 0);
416 uesc = curl_easy_unescape(curl, url, 0, 0);
426 static void free_progress(
void *data) {
427 struct curl_progress *prg = data;
448 if (!(curlprogress =
objalloc(
sizeof(*curlprogress), free_progress))) {
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;
459 static void free_curlpassword(
void *data) {
460 struct curl_password *cpwd = data;
476 if (!(curlpassword =
objalloc(
sizeof(*curlpassword), free_curlpassword))) {
480 curlpassword->authcb = auth_cb;
481 if (data &&
objref(data)) {
482 curlpassword->data = data;
492 if (cbuf && cbuf->
c_type && !strcmp(
"application/xml", cbuf->
c_type)) {
Basic authentification structure.
void curl_setauth_cb(curl_authcb auth_cb, void *data)
Set global password callback.
struct basic_auth * curl_newauth(const char *user, const char *passwd)
Create a new auth structure with initial vallues.
int objref(void *data)
Reference a object.
struct xml_doc xml_doc
Forward decleration of structure.
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)
int objlock(void *data)
Lock the reference.
Buffer containing the result of a curl transaction.
HTTP post data structure.
struct curl_httppost * last
Last item in the list.
int(* curl_progress_func)(void *, double, double, double, double)
CURL callback function called when there is progress (CURLOPT_PROGRESSFUNCTION).
struct xml_doc * xml_loadbuf(const uint8_t *buffer, uint32_t len, int validate)
Load a buffer into XML document returning refereence.
const char * user
Username.
uint8_t * gzinflatebuf(uint8_t *buf_in, int buf_size, uint32_t *len)
Ungzip a buffer.
uint8_t * body
Body buffer.
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
void curlclose(void)
Un reference CURL. This is required for each call to curlinit().
void curl_postitem(struct curl_post *post, const char *name, const char *value)
Add a item value pair to post structure.
struct curlbuf * curl_ungzip(struct curlbuf *cbuf)
If the buffer contains GZIP data uncompress it.
struct curl_httppost * first
First item in the list.
int curlinit(void)
Initilise the CURL library.
DTS Application library API Include file.
struct curl_post * curl_newpost(void)
Create a HTTP Post data structure.
void *(* curl_progress_newdata)(void *)
Create a new progress data structure.
char * url_escape(char *url)
Escape and return the url.
char * url_unescape(char *url)
UN escape and return the url.
uint8_t * header
Header buffer.
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)
void(* curl_progress_pause)(void *, int)
Callback function to control the progress bar.
struct basic_auth *(* curl_authcb)(const char *, const char *, void *)
Callback to set the authentification ie on error 401.
int objunlock(void *data)
Unlock a reference.
const char * passwd
Password.
void curl_setprogress(curl_progress_func cb, curl_progress_pause p_cb, curl_progress_newdata d_cb, void *data)
Configure global progress handling.
int is_gzip(uint8_t *buf, int buf_size)
check a buffer if it contains gzip magic
int objunref(void *data)
Drop reference held.
struct xml_doc * curl_buf2xml(struct curlbuf *cbuf)
Create a XML document from from buffer (application/xml)