[Asterisk-cvs] asterisk/apps app_curl.c,1.10,1.11
markster
markster
Sun Sep 25 15:39:49 CDT 2005
Update of /usr/cvsroot/asterisk/apps
In directory mongoose.digium.com:/tmp/cvs-serv14142/apps
Modified Files:
app_curl.c
Log Message:
Make curl a function
Index: app_curl.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_curl.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- app_curl.c 15 Sep 2005 15:44:26 -0000 1.10
+++ app_curl.c 25 Sep 2005 19:36:48 -0000 1.11
@@ -3,7 +3,7 @@
*
* Copyright (C) 2004 - 2005, Tilghman Lesher
*
- * Tilghman Lesher <curl-20041222 at the-tilghman.com>
+ * Tilghman Lesher <curl-20050919 at the-tilghman.com>
* and Brian Wilkins <bwilkins at cfl.rr.com> (Added POST option)
*
* app_curl.c is distributed with no restrictions on usage or
@@ -85,18 +85,50 @@
return realsize;
}
+static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
+{
+ CURL *curl;
+
+ curl_global_init(CURL_GLOBAL_ALL);
+ curl = curl_easy_init();
+
+ if (!curl) {
+ return -1;
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
+ curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
+
+ if (post) {
+ curl_easy_setopt(curl, CURLOPT_POST, 1);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
+ }
+
+ curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ return 0;
+}
+
static int curl_exec(struct ast_channel *chan, void *data)
{
int res = 0;
struct localuser *u;
- CURL *curl;
char *info, *post_data=NULL, *url;
+ struct MemoryStruct chunk = { NULL, 0 };
+ static int dep_warning = 0;
if (!data || !strlen((char *)data)) {
ast_log(LOG_WARNING, "Curl requires an argument (URL)\n");
return -1;
}
+ if (!dep_warning) {
+ ast_log(LOG_WARNING, "The application Curl is deprecated. Please use the CURL() function instead.\n");
+ dep_warning = 1;
+ }
+
if ((info = ast_strdupa((char *)data))) {
url = strsep(&info, "|");
post_data = info;
@@ -107,54 +139,86 @@
LOCAL_USER_ADD(u);
- curl_global_init(CURL_GLOBAL_ALL);
- curl = curl_easy_init();
+ if (! curl_internal(&chunk, url, post_data)) {
+ if (chunk.memory) {
+ chunk.memory[chunk.size] = '\0';
+ if (chunk.memory[chunk.size - 1] == 10)
+ chunk.memory[chunk.size - 1] = '\0';
- if (curl) {
- struct MemoryStruct chunk;
+ pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
- chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
- chunk.size = 0; /* no data at this point */
+ free(chunk.memory);
+ }
+ } else {
+ ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
+ res = -1;
+ }
- curl_easy_setopt(curl, CURLOPT_URL, url);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
- curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
+ LOCAL_USER_REMOVE(u);
+ return res;
+}
- if (post_data) {
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
- }
+static char *acf_curl_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
+{
+ struct localuser *u;
+ char *info, *post_data=NULL, *url;
+ struct MemoryStruct chunk = { NULL, 0 };
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
+ if (!data || !strlen((char *)data)) {
+ ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
+ *buf = '\0';
+ return buf;
+ }
+
+ if ((info = ast_strdupa((char *)data))) {
+ url = strsep(&info, "|");
+ post_data = info;
+ } else {
+ ast_log(LOG_ERROR, "Out of memory\n");
+ *buf = '\0';
+ return buf;
+ }
+
+ LOCAL_USER_ACF_ADD(u);
+ if (! curl_internal(&chunk, url, post_data)) {
if (chunk.memory) {
chunk.memory[chunk.size] = '\0';
if (chunk.memory[chunk.size - 1] == 10)
chunk.memory[chunk.size - 1] = '\0';
- pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
-
+ ast_copy_string(buf, chunk.memory, len);
free(chunk.memory);
}
} else {
ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
- res = -1;
+ *buf = '\0';
}
LOCAL_USER_REMOVE(u);
- return res;
+ return buf;
}
+struct ast_custom_function acf_curl = {
+ .name = "CURL",
+ .synopsis = "Retrieves the contents of a URL",
+ .syntax = "CURL(url[|post-data])",
+ .desc =
+" url - URL to retrieve\n"
+" post-data - Optional data to send as a POST (GET is default action)\n",
+ .read = acf_curl_exec,
+};
+
int unload_module(void)
{
STANDARD_HANGUP_LOCALUSERS;
+ ast_custom_function_unregister(&acf_curl);
return ast_unregister_application(app);
}
int load_module(void)
{
+ ast_custom_function_register(&acf_curl);
return ast_register_application(app, curl_exec, synopsis, descrip);
}
More information about the svn-commits
mailing list