#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/cli.h"
#include "asterisk/options.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
Go to the source code of this file.
Data Structures | |
struct | MemoryStruct |
Functions | |
static int | acf_curl_exec (struct ast_channel *chan, char *cmd, char *info, char *buf, size_t len) |
AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Load external URL") | |
AST_THREADSTORAGE_CUSTOM (curl_instance, curl_instance_init, curl_instance_cleanup) | |
static void | curl_instance_cleanup (void *data) |
static int | curl_internal (struct MemoryStruct *chunk, char *url, char *post) |
static int | load_module (void) |
static void * | myrealloc (void *ptr, size_t size) |
static int | unload_module (void) |
static size_t | WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) |
Variables | |
struct ast_custom_function | acf_curl |
static const char * | global_useragent = "asterisk-libcurl-agent/1.0" |
Definition in file func_curl.c.
static int acf_curl_exec | ( | struct ast_channel * | chan, | |
char * | cmd, | |||
char * | info, | |||
char * | buf, | |||
size_t | len | |||
) | [static] |
Definition at line 128 of file func_curl.c.
References AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, ast_log(), ast_module_user_add, ast_module_user_remove, AST_STANDARD_APP_ARGS, ast_strlen_zero(), curl_internal(), free, LOG_ERROR, LOG_WARNING, MemoryStruct::memory, and MemoryStruct::size.
00129 { 00130 struct ast_module_user *u; 00131 struct MemoryStruct chunk = { NULL, 0 }; 00132 AST_DECLARE_APP_ARGS(args, 00133 AST_APP_ARG(url); 00134 AST_APP_ARG(postdata); 00135 ); 00136 00137 *buf = '\0'; 00138 00139 if (ast_strlen_zero(info)) { 00140 ast_log(LOG_WARNING, "CURL requires an argument (URL)\n"); 00141 return -1; 00142 } 00143 00144 u = ast_module_user_add(chan); 00145 00146 AST_STANDARD_APP_ARGS(args, info); 00147 00148 if (chan) 00149 ast_autoservice_start(chan); 00150 00151 if (!curl_internal(&chunk, args.url, args.postdata)) { 00152 if (chunk.memory) { 00153 chunk.memory[chunk.size] = '\0'; 00154 if (chunk.memory[chunk.size - 1] == 10) 00155 chunk.memory[chunk.size - 1] = '\0'; 00156 00157 ast_copy_string(buf, chunk.memory, len); 00158 free(chunk.memory); 00159 } 00160 } else { 00161 ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); 00162 } 00163 00164 if (chan) 00165 ast_autoservice_stop(chan); 00166 00167 ast_module_user_remove(u); 00168 00169 return 0; 00170 }
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"Load external URL" | ||||
) |
AST_THREADSTORAGE_CUSTOM | ( | curl_instance | , | |
curl_instance_init | , | |||
curl_instance_cleanup | ||||
) |
static void curl_instance_cleanup | ( | void * | data | ) | [static] |
static int curl_internal | ( | struct MemoryStruct * | chunk, | |
char * | url, | |||
char * | post | |||
) | [static] |
Definition at line 96 of file func_curl.c.
References WriteMemoryCallback().
Referenced by acf_curl_exec().
00097 { 00098 CURL **curl; 00099 00100 if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl)))) 00101 return -1; 00102 00103 if (!*curl) { 00104 if (!(*curl = curl_easy_init())) 00105 return -1; 00106 curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1); 00107 curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180); 00108 curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); 00109 curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent); 00110 } 00111 00112 curl_easy_setopt(*curl, CURLOPT_URL, url); 00113 curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk); 00114 00115 if (post) { 00116 curl_easy_setopt(*curl, CURLOPT_POST, 1); 00117 curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post); 00118 } 00119 00120 curl_easy_perform(*curl); 00121 00122 if (post) 00123 curl_easy_setopt(*curl, CURLOPT_POST, 0); 00124 00125 return 0; 00126 }
static int load_module | ( | void | ) | [static] |
Definition at line 195 of file func_curl.c.
References ast_custom_function_register(), ast_log(), AST_MODULE_LOAD_DECLINE, and LOG_ERROR.
00196 { 00197 int res; 00198 00199 if (curl_global_init(CURL_GLOBAL_ALL)) { 00200 ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n"); 00201 return AST_MODULE_LOAD_DECLINE; 00202 } 00203 00204 res = ast_custom_function_register(&acf_curl); 00205 00206 return res; 00207 }
static void* myrealloc | ( | void * | ptr, | |
size_t | size | |||
) | [static] |
Definition at line 61 of file func_curl.c.
References ast_malloc, and ast_realloc.
Referenced by WriteMemoryCallback().
00062 { 00063 /* There might be a realloc() out there that doesn't like reallocing 00064 NULL pointers, so we take care of it here */ 00065 if (ptr) 00066 return ast_realloc(ptr, size); 00067 else 00068 return ast_malloc(size); 00069 }
static int unload_module | ( | void | ) | [static] |
Definition at line 182 of file func_curl.c.
References ast_custom_function_unregister(), and ast_module_user_hangup_all.
00183 { 00184 int res; 00185 00186 res = ast_custom_function_unregister(&acf_curl); 00187 00188 ast_module_user_hangup_all(); 00189 00190 curl_global_cleanup(); 00191 00192 return res; 00193 }
static size_t WriteMemoryCallback | ( | void * | ptr, | |
size_t | size, | |||
size_t | nmemb, | |||
void * | data | |||
) | [static] |
Definition at line 71 of file func_curl.c.
References MemoryStruct::memory, myrealloc(), and MemoryStruct::size.
Referenced by curl_internal().
00072 { 00073 register int realsize = size * nmemb; 00074 struct MemoryStruct *mem = (struct MemoryStruct *)data; 00075 00076 mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); 00077 if (mem->memory) { 00078 memcpy(&(mem->memory[mem->size]), ptr, realsize); 00079 mem->size += realsize; 00080 mem->memory[mem->size] = 0; 00081 } 00082 return realsize; 00083 }
struct ast_custom_function acf_curl |
Definition at line 172 of file func_curl.c.
const char* global_useragent = "asterisk-libcurl-agent/1.0" [static] |
Definition at line 85 of file func_curl.c.