Make curl a function

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6649 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.2-netsec
Mark Spencer 20 years ago
parent da7c542e29
commit 1097fc2d38

@ -3,7 +3,7 @@
* *
* Copyright (C) 2004 - 2005, Tilghman Lesher * Copyright (C) 2004 - 2005, Tilghman Lesher
* *
* Tilghman Lesher <curl-20041222@the-tilghman.com> * Tilghman Lesher <curl-20050919@the-tilghman.com>
* and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option) * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
* *
* app_curl.c is distributed with no restrictions on usage or * app_curl.c is distributed with no restrictions on usage or
@ -85,18 +85,50 @@ static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *da
return realsize; 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) static int curl_exec(struct ast_channel *chan, void *data)
{ {
int res = 0; int res = 0;
struct localuser *u; struct localuser *u;
CURL *curl;
char *info, *post_data=NULL, *url; char *info, *post_data=NULL, *url;
struct MemoryStruct chunk = { NULL, 0 };
static int dep_warning = 0;
if (!data || !strlen((char *)data)) { if (!data || !strlen((char *)data)) {
ast_log(LOG_WARNING, "Curl requires an argument (URL)\n"); ast_log(LOG_WARNING, "Curl requires an argument (URL)\n");
return -1; 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))) { if ((info = ast_strdupa((char *)data))) {
url = strsep(&info, "|"); url = strsep(&info, "|");
post_data = info; post_data = info;
@ -107,54 +139,86 @@ static int curl_exec(struct ast_channel *chan, void *data)
LOCAL_USER_ADD(u); LOCAL_USER_ADD(u);
curl_global_init(CURL_GLOBAL_ALL); if (! curl_internal(&chunk, url, post_data)) {
curl = curl_easy_init(); if (chunk.memory) {
chunk.memory[chunk.size] = '\0';
if (chunk.memory[chunk.size - 1] == 10)
chunk.memory[chunk.size - 1] = '\0';
if (curl) { pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
struct MemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ free(chunk.memory);
chunk.size = 0; /* no data at this point */ }
} else {
ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
res = -1;
}
curl_easy_setopt(curl, CURLOPT_URL, url); LOCAL_USER_REMOVE(u);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); return res;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); }
curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
if (post_data) { static char *acf_curl_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
curl_easy_setopt(curl, CURLOPT_POST, 1); {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); struct localuser *u;
char *info, *post_data=NULL, *url;
struct MemoryStruct chunk = { NULL, 0 };
if (!data || !strlen((char *)data)) {
ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
*buf = '\0';
return buf;
} }
curl_easy_perform(curl); if ((info = ast_strdupa((char *)data))) {
curl_easy_cleanup(curl); 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) { if (chunk.memory) {
chunk.memory[chunk.size] = '\0'; chunk.memory[chunk.size] = '\0';
if (chunk.memory[chunk.size - 1] == 10) if (chunk.memory[chunk.size - 1] == 10)
chunk.memory[chunk.size - 1] = '\0'; chunk.memory[chunk.size - 1] = '\0';
pbx_builtin_setvar_helper(chan, "CURL", chunk.memory); ast_copy_string(buf, chunk.memory, len);
free(chunk.memory); free(chunk.memory);
} }
} else { } else {
ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
res = -1; *buf = '\0';
} }
LOCAL_USER_REMOVE(u); 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) int unload_module(void)
{ {
STANDARD_HANGUP_LOCALUSERS; STANDARD_HANGUP_LOCALUSERS;
ast_custom_function_unregister(&acf_curl);
return ast_unregister_application(app); return ast_unregister_application(app);
} }
int load_module(void) int load_module(void)
{ {
ast_custom_function_register(&acf_curl);
return ast_register_application(app, curl_exec, synopsis, descrip); return ast_register_application(app, curl_exec, synopsis, descrip);
} }

Loading…
Cancel
Save