Wed Aug 15 01:24:21 2007

Asterisk developer's documentation


func_curl.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C)  2004 - 2006, Tilghman Lesher
00005  *
00006  * Tilghman Lesher <curl-20050919@the-tilghman.com>
00007  * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
00008  *
00009  * app_curl.c is distributed with no restrictions on usage or
00010  * redistribution.
00011  *
00012  * See http://www.asterisk.org for more information about
00013  * the Asterisk project. Please do not directly contact
00014  * any of the maintainers of this project for assistance;
00015  * the project provides a web site, mailing lists and IRC
00016  * channels for your use.
00017  *
00018  */
00019 
00020 /*! \file
00021  * 
00022  * \brief Curl - Load a URL
00023  *
00024  * \author Tilghman Lesher <curl-20050919@the-tilghman.com>
00025  *
00026  * \note Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option) 
00027  * 
00028  * \ingroup functions
00029  */
00030  
00031 /*** MODULEINFO
00032    <depend>curl</depend>
00033  ***/
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 48513 $")
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <curl/curl.h>
00043 
00044 #include "asterisk/lock.h"
00045 #include "asterisk/file.h"
00046 #include "asterisk/logger.h"
00047 #include "asterisk/channel.h"
00048 #include "asterisk/pbx.h"
00049 #include "asterisk/cli.h"
00050 #include "asterisk/options.h"
00051 #include "asterisk/module.h"
00052 #include "asterisk/app.h"
00053 #include "asterisk/utils.h"
00054 #include "asterisk/threadstorage.h"
00055 
00056 struct MemoryStruct {
00057    char *memory;
00058    size_t size;
00059 };
00060 
00061 static void *myrealloc(void *ptr, size_t size)
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 }
00070 
00071 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
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 }
00084 
00085 static const char *global_useragent = "asterisk-libcurl-agent/1.0";
00086 
00087 static void curl_instance_cleanup(void *data)
00088 {
00089    CURL **curl = data;
00090 
00091    curl_easy_cleanup(*curl);
00092 }
00093 
00094 AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
00095 
00096 static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
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 }
00127 
00128 static int acf_curl_exec(struct ast_channel *chan, char *cmd, char *info, char *buf, size_t len)
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 (!curl_internal(&chunk, args.url, args.postdata)) {
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          ast_copy_string(buf, chunk.memory, len);
00155          free(chunk.memory);
00156       }
00157    } else {
00158       ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
00159    }
00160 
00161    ast_module_user_remove(u);
00162 
00163    return 0;
00164 }
00165 
00166 struct ast_custom_function acf_curl = {
00167    .name = "CURL",
00168    .synopsis = "Retrieves the contents of a URL",
00169    .syntax = "CURL(url[|post-data])",
00170    .desc =
00171    "  url       - URL to retrieve\n"
00172    "  post-data - Optional data to send as a POST (GET is default action)\n",
00173    .read = acf_curl_exec,
00174 };
00175 
00176 static int unload_module(void)
00177 {
00178    int res;
00179 
00180    res = ast_custom_function_unregister(&acf_curl);
00181 
00182    ast_module_user_hangup_all();
00183 
00184    curl_global_cleanup();
00185    
00186    return res;
00187 }
00188 
00189 static int load_module(void)
00190 {
00191    int res;
00192 
00193    if (curl_global_init(CURL_GLOBAL_ALL)) {
00194       ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
00195       return AST_MODULE_LOAD_DECLINE;
00196    }  
00197 
00198    res = ast_custom_function_register(&acf_curl);
00199 
00200    return res;
00201 }
00202 
00203 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Load external URL");
00204 

Generated on Wed Aug 15 01:24:21 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.3