Thu Jul 9 13:41:19 2009

Asterisk developer's documentation


func_curl.c File Reference

Curl - Load a URL. More...

#include "asterisk.h"
#include <curl/curl.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/cli.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 void __init_curl_instance (void)
static void __reg_module (void)
static void __unreg_module (void)
static int acf_curl_exec (struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
static void curl_instance_cleanup (void *data)
static int curl_instance_init (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

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Load external URL" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "068e67f60f50dd9ee86464c05884a49d" , .load = load_module, .unload = unload_module, }
ast_custom_function acf_curl
static const struct ast_module_infoast_module_info = &__mod_info
static struct ast_threadstorage curl_instance = { .once = PTHREAD_ONCE_INIT, .key_init = __init_curl_instance , .custom_init = curl_instance_init , }
static const char * global_useragent = "asterisk-libcurl-agent/1.0"


Detailed Description

Curl - Load a URL.

Author:
Tilghman Lesher <curl-20050919@the-tilghman.com>
Note:
Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
ExtRef:
Depends on the CURL library - http://curl.haxx.se/

Definition in file func_curl.c.


Function Documentation

static void __init_curl_instance ( void   )  [static]

Definition at line 106 of file func_curl.c.

00109 {

static void __reg_module ( void   )  [static]

Definition at line 205 of file func_curl.c.

static void __unreg_module ( void   )  [static]

Definition at line 205 of file func_curl.c.

static int acf_curl_exec ( struct ast_channel chan,
const char *  cmd,
char *  info,
char *  buf,
size_t  len 
) [static]

Definition at line 131 of file func_curl.c.

References AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), chan, curl_internal(), LOG_ERROR, LOG_WARNING, MemoryStruct::memory, MemoryStruct::size, and url.

00132 {
00133    struct MemoryStruct chunk = { NULL, 0 };
00134    AST_DECLARE_APP_ARGS(args,
00135       AST_APP_ARG(url);
00136       AST_APP_ARG(postdata);
00137    );
00138 
00139    *buf = '\0';
00140    
00141    if (ast_strlen_zero(info)) {
00142       ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
00143       return -1;
00144    }
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          ast_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    return 0;
00168 }

static void curl_instance_cleanup ( void *  data  )  [static]

Definition at line 97 of file func_curl.c.

References ast_free.

00098 {
00099    CURL **curl = data;
00100 
00101    curl_easy_cleanup(*curl);
00102 
00103    ast_free(data);
00104 }

static int curl_instance_init ( void *  data  )  [static]

Definition at line 82 of file func_curl.c.

References WriteMemoryCallback().

00083 {
00084    CURL **curl = data;
00085 
00086    if (!(*curl = curl_easy_init()))
00087       return -1;
00088 
00089    curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
00090    curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
00091    curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00092    curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
00093 
00094    return 0;
00095 }

static int curl_internal ( struct MemoryStruct chunk,
char *  url,
char *  post 
) [static]

Definition at line 108 of file func_curl.c.

References ast_threadstorage_get(), and curl_instance.

Referenced by acf_curl_exec().

00109 {
00110    CURL **curl;
00111 
00112    if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
00113       return -1;
00114 
00115    curl_easy_setopt(*curl, CURLOPT_URL, url);
00116    curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
00117 
00118    if (post) {
00119       curl_easy_setopt(*curl, CURLOPT_POST, 1);
00120       curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
00121    }
00122 
00123    curl_easy_perform(*curl);
00124 
00125    if (post)
00126       curl_easy_setopt(*curl, CURLOPT_POST, 0);
00127 
00128    return 0;
00129 }

static int load_module ( void   )  [static]

Definition at line 191 of file func_curl.c.

References acf_curl, ast_custom_function_register, ast_log(), AST_MODULE_LOAD_DECLINE, and LOG_ERROR.

00192 {
00193    int res;
00194 
00195    if (curl_global_init(CURL_GLOBAL_ALL)) {
00196       ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
00197       return AST_MODULE_LOAD_DECLINE;
00198    }  
00199 
00200    res = ast_custom_function_register(&acf_curl);
00201 
00202    return res;
00203 }

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    return (ptr ? ast_realloc(ptr, size) : ast_malloc(size));
00064 }

static int unload_module ( void   )  [static]

Definition at line 180 of file func_curl.c.

References acf_curl, and ast_custom_function_unregister().

00181 {
00182    int res;
00183 
00184    res = ast_custom_function_unregister(&acf_curl);
00185 
00186    curl_global_cleanup();
00187    
00188    return res;
00189 }

static size_t WriteMemoryCallback ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  data 
) [static]

Definition at line 66 of file func_curl.c.

References MemoryStruct::memory, myrealloc(), and MemoryStruct::size.

Referenced by curl_instance_init().

00067 {
00068    register int realsize = size * nmemb;
00069    struct MemoryStruct *mem = (struct MemoryStruct *)data;
00070 
00071    if ((mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1))) {
00072       memcpy(&(mem->memory[mem->size]), ptr, realsize);
00073       mem->size += realsize;
00074       mem->memory[mem->size] = 0;
00075    }
00076 
00077    return realsize;
00078 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Load external URL" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "068e67f60f50dd9ee86464c05884a49d" , .load = load_module, .unload = unload_module, } [static]

Definition at line 205 of file func_curl.c.

struct ast_custom_function acf_curl

Definition at line 170 of file func_curl.c.

Referenced by load_module(), and unload_module().

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 205 of file func_curl.c.

struct ast_threadstorage curl_instance = { .once = PTHREAD_ONCE_INIT, .key_init = __init_curl_instance , .custom_init = curl_instance_init , } [static]

Definition at line 106 of file func_curl.c.

Referenced by curl_internal().

const char* global_useragent = "asterisk-libcurl-agent/1.0" [static]

Definition at line 80 of file func_curl.c.


Generated on Thu Jul 9 13:41:19 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7