#include <stdio.h> #include <stdarg.h> #include <string.h> #include <curl.h> #include <easy.h> #include <types.h> #include "../wftk_session.h" #include "../wftk_internals.h" |
adaptor_info structure is used to pass adaptor info (duh) back to the
config module when it's building an adaptor instance. Here's what it contains:
static char *names[] =
{
"init",
"free",
"info",
"send"
};
XML * CONNECT_url_init (WFTK_ADAPTOR * ad, va_list args);
XML * CONNECT_url_free (WFTK_ADAPTOR * ad, va_list args);
XML * CONNECT_url_info (WFTK_ADAPTOR * ad, va_list args);
XML * CONNECT_url_send (WFTK_ADAPTOR * ad, va_list args);
static WFTK_API_FUNC vtab[] =
{
CONNECT_url_init,
CONNECT_url_free,
CONNECT_url_info,
CONNECT_url_send
};
static struct wftk_adaptor_info _CONNECT_url_info =
{
4,
names,
vtab
};
|
struct wftk_adaptor_info * CONNECT_url_get_info ()
{
return & _CONNECT_url_info;
}
|
XML * CONNECT_url_init (WFTK_ADAPTOR * ad, va_list args) {
CURL * curl;
const char * parms;
curl_global_init (CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
ad->bindata = (void *) curl;
} else {
xml_set (ad->parms, "error", "Unable to initialize libcurl.");
}
return (XML *) 0;
}
|
XML * CONNECT_url_free (WFTK_ADAPTOR * ad, va_list args)
{
CURL * curl = (CURL *) (ad->bindata);
if (curl) curl_easy_cleanup (curl);
curl_global_cleanup ();
}
|
XML * CONNECT_url_info (WFTK_ADAPTOR * ad, va_list args) {
XML * info;
info = xml_create ("info");
xml_set (info, "type", "connect");
xml_set (info, "name", "url");
xml_set (info, "ver", "1.0.0");
xml_set (info, "compiled", __TIME__ " " __DATE__);
xml_set (info, "author", "Michael Roberts");
xml_set (info, "contact", "wftk@vivtek.com");
xml_set (info, "libcurl", curl_version());
xml_set (info, "extra_functions", "0");
return (info);
}
|
int CONNECT_url_writefunction (void * data, size_t size, size_t num, void * xml) {
XML * stash = (XML *) xml;
xml_attrncat (stash, "return", data, size * num);
return size * num;
}
XML * CONNECT_url_send (WFTK_ADAPTOR * ad, va_list args)
{
CURL * curl = (CURL *) (ad->bindata);
XML * query = (XML *) 0;
XML * data = (XML *) 0;
XML * stash;
XML * field;
char * mark;
char * mark2;
char * qmark;
int post = 0;
char * built;
XML * ret;
if (args) query = va_arg (args, XML *);
if (!query) {
xml_set (ad->parms, "error", "No query specified.");
return (XML *) 0;
}
if (args) data = va_arg (args, XML *);
if (!data) data = query; /* NULL data means to look at the query structure itself. */
stash = xml_create ("s");
xml_set (stash, "url", xml_attrval (query, "url"));
if (!strcmp (xml_attrval (query, "method"), "POST")) post = 1;
if (post) {
curl_easy_setopt (curl, CURLOPT_POST, 1L);
built = "body";
xml_set (stash, built, "");
qmark = NULL;
} else {
curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L);
built = "url";
qmark = strchr (xml_attrval (stash, built), '?');
}
field = xml_firstelem (data);
while (field) {
if (xml_is (field, "field")) {
if (qmark) xml_attrcat (stash, built, "&");
else {
if (!post) xml_attrcat (stash, built, "?");
qmark = "";
}
mark = curl_escape ((char *)xml_attrval (field, "name"), 0);
xml_attrcat (stash, built, mark);
free (mark);
xml_attrcat (stash, built, "=");
if (*xml_attrval (field, "value")) {
mark = curl_escape ((char *) xml_attrval (field, "value"), 0);
xml_attrcat (stash, built, mark);
free (mark);
} else {
mark = xml_stringcontent (field);
mark2 = curl_escape (mark, 0);
xml_attrcat (stash, built, mark2);
free (mark); free (mark2);
}
}
field = xml_nextelem (field);
}
curl_easy_setopt (curl, CURLOPT_URL, xml_attrval (stash, "url"));
if (post) curl_easy_setopt (curl, CURLOPT_POSTFIELDS, xml_attrval (stash, "body"));
xml_set (stash, "return", "");
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, CONNECT_url_writefunction);
curl_easy_setopt (curl, CURLOPT_FILE, stash);
curl_easy_perform (curl);
ret = xml_createtext (xml_attrval (stash, "return")); /* Later we'll get fancy. */
xml_free (stash);
return ret;
}
|
| This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was prepared with LPML. Try literate programming. You'll like it. |