Sat Nov 1 06:29:06 2008

Asterisk developer's documentation


func_curl.c File Reference

Curl - Load a URL. More...

#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"

Include dependency graph for func_curl.c:

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

ast_custom_function acf_curl
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)

Definition in file func_curl.c.


Function Documentation

static int acf_curl_exec ( struct ast_channel chan,
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_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.

00132 {
00133    int ret = -1;
00134    struct ast_module_user *u;
00135    struct MemoryStruct chunk = { NULL, 0 };
00136    AST_DECLARE_APP_ARGS(args,
00137       AST_APP_ARG(url);
00138       AST_APP_ARG(postdata);
00139    );
00140 
00141    *buf = '\0';
00142    
00143    if (ast_strlen_zero(info)) {
00144       ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
00145       return -1;
00146    }
00147 
00148    u = ast_module_user_add(chan);
00149 
00150    AST_STANDARD_APP_ARGS(args, info);  
00151 
00152    if (chan)
00153       ast_autoservice_start(chan);
00154 
00155    if (!curl_internal(&chunk, args.url, args.postdata)) {
00156       if (chunk.memory) {
00157          chunk.memory[chunk.size] = '\0';
00158          if (chunk.memory[chunk.size - 1] == 10)
00159             chunk.memory[chunk.size - 1] = '\0';
00160 
00161          ast_copy_string(buf, chunk.memory, len);
00162          free(chunk.memory);
00163       }
00164       ret = 0;
00165    } else {
00166       ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
00167    }
00168 
00169    if (chan)
00170       ast_autoservice_stop(chan);
00171 
00172    ast_module_user_remove(u);
00173 
00174    return ret;
00175 }

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]

Definition at line 87 of file func_curl.c.

References free.

00088 {
00089    CURL **curl = data;
00090 
00091    curl_easy_cleanup(*curl);
00092 
00093    free(data);
00094 }

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

Definition at line 98 of file func_curl.c.

References WriteMemoryCallback().

Referenced by acf_curl_exec().

00099 {
00100    int ret;
00101    CURL **curl;
00102 
00103    if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
00104       return -1;
00105 
00106    if (!*curl) {
00107       if (!(*curl = curl_easy_init()))
00108          return -1;
00109       curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
00110       curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
00111       curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00112       curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
00113    }
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    ret = curl_easy_perform(*curl);
00124 
00125    if (post)
00126       curl_easy_setopt(*curl, CURLOPT_POST, 0);
00127 
00128    return ret ? -1 : 0;
00129 }

static int load_module ( void   )  [static]

Definition at line 200 of file func_curl.c.

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

00201 {
00202    int res;
00203 
00204    if (curl_global_init(CURL_GLOBAL_ALL)) {
00205       ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
00206       return AST_MODULE_LOAD_DECLINE;
00207    }  
00208 
00209    res = ast_custom_function_register(&acf_curl);
00210 
00211    return res;
00212 }

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 187 of file func_curl.c.

References acf_curl, ast_custom_function_unregister(), and ast_module_user_hangup_all.

00188 {
00189    int res;
00190 
00191    res = ast_custom_function_unregister(&acf_curl);
00192 
00193    ast_module_user_hangup_all();
00194 
00195    curl_global_cleanup();
00196    
00197    return res;
00198 }

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 }


Variable Documentation

struct ast_custom_function acf_curl

Definition at line 177 of file func_curl.c.

Referenced by load_module(), and unload_module().

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

Definition at line 85 of file func_curl.c.


Generated on Sat Nov 1 06:29:06 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1