Fri Jul 24 00:41:45 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.

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 ast_str **chunk, char *url, char *post)
static int load_module (void)
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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
ast_custom_function acf_curl
static 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 92 of file func_curl.c.

00095 {

static void __reg_module ( void   )  [static]

Definition at line 195 of file func_curl.c.

static void __unreg_module ( void   )  [static]

Definition at line 195 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 117 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_str_create(), ast_strlen_zero(), chan, curl_internal(), LOG_ERROR, LOG_WARNING, str, and url.

00118 {
00119    struct ast_str *str = ast_str_create(16);
00120    int ret = -1;
00121    AST_DECLARE_APP_ARGS(args,
00122       AST_APP_ARG(url);
00123       AST_APP_ARG(postdata);
00124    );
00125 
00126    *buf = '\0';
00127    
00128    if (ast_strlen_zero(info)) {
00129       ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
00130       ast_free(str);
00131       return -1;
00132    }
00133 
00134    AST_STANDARD_APP_ARGS(args, info);  
00135 
00136    if (chan)
00137       ast_autoservice_start(chan);
00138 
00139    if (!curl_internal(&str, args.url, args.postdata)) {
00140       if (str->used) {
00141          str->str[str->used] = '\0';
00142          if (str->str[str->used - 1] == '\n') {
00143             str->str[str->used - 1] = '\0';
00144          }
00145 
00146          ast_copy_string(buf, str->str, len);
00147       }
00148       ret = 0;
00149    } else {
00150       ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
00151    }
00152    ast_free(str);
00153 
00154    if (chan)
00155       ast_autoservice_stop(chan);
00156    
00157    return ret;
00158 }

static void curl_instance_cleanup ( void *  data  )  [static]

Definition at line 83 of file func_curl.c.

References ast_free.

00084 {
00085    CURL **curl = data;
00086 
00087    curl_easy_cleanup(*curl);
00088 
00089    ast_free(data);
00090 }

static int curl_instance_init ( void *  data  )  [static]

Definition at line 68 of file func_curl.c.

References WriteMemoryCallback().

00069 {
00070    CURL **curl = data;
00071 
00072    if (!(*curl = curl_easy_init()))
00073       return -1;
00074 
00075    curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
00076    curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
00077    curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00078    curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
00079 
00080    return 0;
00081 }

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

Definition at line 94 of file func_curl.c.

References ast_threadstorage_get(), and curl_instance.

Referenced by acf_curl_exec().

00095 {
00096    CURL **curl;
00097 
00098    if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
00099       return -1;
00100 
00101    curl_easy_setopt(*curl, CURLOPT_URL, url);
00102    curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
00103 
00104    if (post) {
00105       curl_easy_setopt(*curl, CURLOPT_POST, 1);
00106       curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
00107    }
00108 
00109    curl_easy_perform(*curl);
00110 
00111    if (post)
00112       curl_easy_setopt(*curl, CURLOPT_POST, 0);
00113 
00114    return 0;
00115 }

static int load_module ( void   )  [static]

Definition at line 179 of file func_curl.c.

References acf_curl, ast_custom_function_register, ast_load_resource(), ast_log(), ast_module_check(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and LOG_ERROR.

00180 {
00181    int res;
00182 
00183    if (!ast_module_check("res_curl.so")) {
00184       if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
00185          ast_log(LOG_ERROR, "Cannot load res_curl, so func_curl cannot be loaded\n");
00186          return AST_MODULE_LOAD_DECLINE;
00187       }
00188    }
00189 
00190    res = ast_custom_function_register(&acf_curl);
00191 
00192    return res;
00193 }

static int unload_module ( void   )  [static]

Definition at line 170 of file func_curl.c.

References acf_curl, and ast_custom_function_unregister().

00171 {
00172    int res;
00173 
00174    res = ast_custom_function_unregister(&acf_curl);
00175 
00176    return res;
00177 }

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

Definition at line 53 of file func_curl.c.

References ast_str_make_space(), and str.

Referenced by curl_instance_init().

00054 {
00055    register int realsize = size * nmemb;
00056    struct ast_str **str = (struct ast_str **)data;
00057 
00058    if (ast_str_make_space(str, (*str)->used + realsize + 1) == 0) {
00059       memcpy(&(*str)->str[(*str)->used], ptr, realsize);
00060       (*str)->used += realsize;
00061    }
00062 
00063    return realsize;
00064 }


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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 195 of file func_curl.c.

struct ast_custom_function acf_curl

Definition at line 160 of file func_curl.c.

Referenced by load_module(), and unload_module().

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 195 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 92 of file func_curl.c.

Referenced by curl_internal().

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

Definition at line 66 of file func_curl.c.


Generated on Fri Jul 24 00:41:45 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7