Mon Mar 20 08:25:46 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

app_curl.c File Reference

Curl - App to load a URL. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "asterisk.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"

Go to the source code of this file.

Data Structures

struct  MemoryStruct

Functions

char * acf_curl_exec (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
int curl_exec (struct ast_channel *chan, void *data)
int curl_internal (struct MemoryStruct *chunk, char *url, char *post)
char * description (void)
 Provides a description of the module.
char * key ()
 Returns the ASTERISK_GPL_KEY.
int load_module (void)
 Initialize the module.
void * myrealloc (void *ptr, size_t size)
int unload_module (void)
 Cleanup all module structures, sockets, etc.
int usecount (void)
 Provides a usecount.
size_t WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data)

Variables

ast_custom_function acf_curl
char * app = "Curl"
char * descrip
 LOCAL_USER_DECL
 STANDARD_LOCAL_USER
char * synopsis = "Load an external URL"
char * tdesc = "Load external URL"


Detailed Description

Curl - App to load a URL.

Definition in file app_curl.c.


Function Documentation

char* acf_curl_exec struct ast_channel chan,
char *  cmd,
char *  data,
char *  buf,
size_t  len
[static]
 

Definition at line 167 of file app_curl.c.

References ast_log(), ast_strdupa, ast_strlen_zero(), curl_internal(), free, LOCAL_USER_ACF_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, MemoryStruct::memory, MemoryStruct::size, and strsep().

00168 {
00169    struct localuser *u;
00170    char *info, *post_data=NULL, *url;
00171    struct MemoryStruct chunk = { NULL, 0 };
00172 
00173    *buf = '\0';
00174    
00175    if (ast_strlen_zero(data)) {
00176       ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
00177       return buf;
00178    }
00179 
00180    LOCAL_USER_ACF_ADD(u);
00181 
00182    info = ast_strdupa(data);
00183    if (!info) {
00184       ast_log(LOG_ERROR, "Out of memory\n");
00185       LOCAL_USER_REMOVE(u);
00186       return buf;
00187    }
00188    
00189    url = strsep(&info, "|");
00190    post_data = info;
00191    
00192    if (! curl_internal(&chunk, url, post_data)) {
00193       if (chunk.memory) {
00194          chunk.memory[chunk.size] = '\0';
00195          if (chunk.memory[chunk.size - 1] == 10)
00196             chunk.memory[chunk.size - 1] = '\0';
00197 
00198          ast_copy_string(buf, chunk.memory, len);
00199          free(chunk.memory);
00200       }
00201    } else {
00202       ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
00203    }
00204 
00205    LOCAL_USER_REMOVE(u);
00206    return buf;
00207 }

int curl_exec struct ast_channel chan,
void *  data
[static]
 

Definition at line 119 of file app_curl.c.

References ast_log(), ast_strdupa, ast_strlen_zero(), curl_internal(), dep_warning, free, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, MemoryStruct::memory, pbx_builtin_setvar_helper(), MemoryStruct::size, and strsep().

Referenced by load_module().

00120 {
00121    int res = 0;
00122    struct localuser *u;
00123    char *info, *post_data=NULL, *url;
00124    struct MemoryStruct chunk = { NULL, 0 };
00125    static int dep_warning = 0;
00126    
00127    if (!dep_warning) {
00128       ast_log(LOG_WARNING, "The application Curl is deprecated.  Please use the CURL() function instead.\n");
00129       dep_warning = 1;
00130    }
00131 
00132    if (ast_strlen_zero(data)) {
00133       ast_log(LOG_WARNING, "Curl requires an argument (URL)\n");
00134       return -1;
00135    }
00136    
00137    LOCAL_USER_ADD(u);
00138    
00139    if ((info = ast_strdupa(data))) {
00140       url = strsep(&info, "|");
00141       post_data = info;
00142    } else {
00143       ast_log(LOG_ERROR, "Out of memory\n");
00144       LOCAL_USER_REMOVE(u);
00145       return -1;
00146    }
00147 
00148    if (! curl_internal(&chunk, url, post_data)) {
00149       if (chunk.memory) {
00150          chunk.memory[chunk.size] = '\0';
00151          if (chunk.memory[chunk.size - 1] == 10)
00152             chunk.memory[chunk.size - 1] = '\0';
00153 
00154          pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
00155 
00156          free(chunk.memory);
00157       }
00158    } else {
00159       ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
00160       res = -1;
00161    }
00162 
00163    LOCAL_USER_REMOVE(u);
00164    return res;
00165 }

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

Definition at line 93 of file app_curl.c.

References WriteMemoryCallback().

Referenced by acf_curl_exec(), and curl_exec().

00094 {
00095    CURL *curl;
00096 
00097    curl_global_init(CURL_GLOBAL_ALL);
00098    curl = curl_easy_init();
00099 
00100    if (!curl) {
00101       return -1;
00102    }
00103 
00104    curl_easy_setopt(curl, CURLOPT_URL, url);
00105    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00106    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
00107    curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
00108 
00109    if (post) {
00110       curl_easy_setopt(curl, CURLOPT_POST, 1);
00111       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
00112    }
00113 
00114    curl_easy_perform(curl);
00115    curl_easy_cleanup(curl);
00116    return 0;
00117 }

char* description void   ) 
 

Provides a description of the module.

Returns:
a short description of your module

Definition at line 241 of file app_curl.c.

00242 {
00243    return tdesc;
00244 }

char* key void   ) 
 

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

 char *key(void) {
         return ASTERISK_GPL_KEY;
 }

Returns:
ASTERISK_GPL_KEY

Definition at line 253 of file app_curl.c.

00254 {
00255    return ASTERISK_GPL_KEY;
00256 }

int load_module void   ) 
 

Initialize the module.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
int Always 0.

Definition at line 231 of file app_curl.c.

References acf_curl, app, ast_custom_function_register(), ast_register_application(), curl_exec(), descrip, and synopsis.

00232 {
00233    int res;
00234 
00235    res = ast_custom_function_register(&acf_curl);
00236    res |= ast_register_application(app, curl_exec, synopsis, descrip);
00237 
00238    return res;
00239 }

void* myrealloc void *  ptr,
size_t  size
[static]
 

Definition at line 69 of file app_curl.c.

References malloc, and realloc.

Referenced by WriteMemoryCallback().

00070 {
00071    /* There might be a realloc() out there that doesn't like reallocing
00072       NULL pointers, so we take care of it here */
00073    if (ptr)
00074       return realloc(ptr, size);
00075    else
00076       return malloc(size);
00077 }

int unload_module void   ) 
 

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
Zero on success, or non-zero on error.

Definition at line 219 of file app_curl.c.

References acf_curl, app, ast_custom_function_unregister(), and ast_unregister_application().

00220 {
00221    int res;
00222 
00223    res = ast_custom_function_unregister(&acf_curl);
00224    res |= ast_unregister_application(app);
00225 
00226    STANDARD_HANGUP_LOCALUSERS;
00227    
00228    return res;
00229 }

int usecount void   ) 
 

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
The module's usecount.

Definition at line 246 of file app_curl.c.

References STANDARD_USECOUNT.

00247 {
00248    int res;
00249    STANDARD_USECOUNT(res);
00250    return res;
00251 }

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

Definition at line 79 of file app_curl.c.

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

Referenced by curl_internal().

00080 {
00081    register int realsize = size * nmemb;
00082    struct MemoryStruct *mem = (struct MemoryStruct *)data;
00083 
00084    mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
00085    if (mem->memory) {
00086       memcpy(&(mem->memory[mem->size]), ptr, realsize);
00087       mem->size += realsize;
00088       mem->memory[mem->size] = 0;
00089    }
00090    return realsize;
00091 }


Variable Documentation

struct ast_custom_function acf_curl
 

Definition at line 209 of file app_curl.c.

Referenced by load_module(), and unload_module().

char* app = "Curl" [static]
 

Definition at line 46 of file app_curl.c.

Referenced by load_module(), and unload_module().

char* descrip [static]
 

Definition at line 50 of file app_curl.c.

Referenced by load_module().

LOCAL_USER_DECL
 

Definition at line 62 of file app_curl.c.

STANDARD_LOCAL_USER
 

Definition at line 60 of file app_curl.c.

char* synopsis = "Load an external URL" [static]
 

Definition at line 48 of file app_curl.c.

Referenced by load_module().

char* tdesc = "Load external URL" [static]
 

Definition at line 44 of file app_curl.c.


Generated on Mon Mar 20 08:25:46 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1